C\#상용 도구 클래스-Excel 작업 클래스
/// ——Excel
/// <para> ------------------------------------------------</para>
/// <para> CreateConnection: Excel EXCEL OleConnection </para>
/// <para> ExecuteDataSet: SQL , DataSet </para>
/// <para> ExecuteDataTable: SQL , DataTable </para>
/// <para> ExecuteDataAdapter: , DataSet 。</para>
/// <para> ExecuteNonQuery: , -1[ :INSERT、DELETE、UPDATE SET ]。</para>
/// <para> ExecuteScalar: , null</para>
/// <para> ExecuteDataReader: </para>
/// <para> GetWorkBookName: Excel </para>
using System;
using System.Configuration;
using System.Web;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace Utils
{
/// <summary>
/// <para> </para>
/// ——Excel
/// <para> ------------------------------------------------</para>
/// <para> CreateConnection: Excel EXCEL OleConnection </para>
/// <para> ExecuteDataSet: SQL , DataSet </para>
/// <para> ExecuteDataTable: SQL , DataTable </para>
/// <para> ExecuteDataAdapter: , DataSet 。</para>
/// <para> ExecuteNonQuery: , -1[ :INSERT、DELETE、UPDATE SET ]。</para>
/// <para> ExecuteScalar: , null</para>
/// <para> ExecuteDataReader: </para>
/// <para> GetWorkBookName: Excel </para>
/// </summary>
public class ExcelHelper
{
private ExcelHelper() { }
#region EXCEL
/// <summary>
/// EXCEL
/// </summary>
public enum ExcelVerion
{
/// <summary>
/// Excel97-2003
/// </summary>
Excel2003,
/// <summary>
/// Excel2007
/// </summary>
Excel2007
}
#endregion
#region EXCEL OleDbConnectin
/// <summary>
/// EXCEL OleDbConnectin
/// </summary>
/// <param name="ExcelFilePath">EXCEL </param>
/// <param name="Verion">Excel :97-2003 2007, </param>
/// <returns>OleDbConnection </returns>
public static OleDbConnection CreateConnection(string ExcelFilePath,ExcelVerion Verion)
{
OleDbConnection Connection = null;
string strConnection = string.Empty;
try
{
switch (Verion)
{
case ExcelVerion.Excel2003: // Excel97-2003
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=" + HttpContext.Current.Server.MapPath(ExcelFilePath) + ";Extended Properties=Excel 8.0";
break;
case ExcelVerion.Excel2007: // Excel2007
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';data source=" + ExcelFilePath;
break;
}
if(!string.IsNullOrEmpty(strConnection)) Connection = new OleDbConnection(strConnection);
}
catch (Exception)
{
}
return Connection;
}
#endregion
#region OleDbCommand
/// <summary>
/// OleDbCommand
/// </summary>
/// <param name="CommandText">SQL </param>
/// <param name="Connection"> OleDbConnection</param>
/// <param name="OleDbParameters"> </param>
/// <returns></returns>
private static OleDbCommand CreateCommand(string CommandText, OleDbConnection Connection, params System.Data.OleDb.OleDbParameter[] OleDbParameters)
{
if (Connection.State == ConnectionState.Closed)
Connection.Open();
OleDbCommand comm = new OleDbCommand(CommandText, Connection);
if (OleDbParameters != null)
{
foreach (OleDbParameter parm in OleDbParameters)
{
comm.Parameters.Add(parm);
}
}
return comm;
}
#endregion
#region SQL , DataSet
/// <summary>
/// SQL , DataSet
/// </summary>
/// <param name="Connection">OleDbConnection </param>
/// <param name="CommandText">SQL </param>
/// <param name="OleDbParameters">OleDbParameter </param>
/// <returns>DataSet </returns>
public static DataSet ExecuteDataSet(OleDbConnection Connection, string CommandText, params OleDbParameter[] OleDbParameters)
{
DataSet ds = new DataSet();
try
{
OleDbCommand comm = CreateCommand(CommandText, Connection, OleDbParameters);
OleDbDataAdapter da = new OleDbDataAdapter(comm);
da.Fill(ds);
}
catch (Exception)
{
}
finally
{
if (Connection.State == ConnectionState.Open) Connection.Close();
}
return ds;
}
#endregion
#region SQL , DataTable
/// <summary>
/// SQL , DataTable
/// </summary>
/// <param name="Connection">OleDbConnection </param>
/// <param name="CommandText">SQL </param>
/// <param name="OleDbParameters">OleDbParameter </param>
/// <returns>DataSet </returns>
public static DataTable ExecuteDataTable(OleDbConnection Connection, string CommandText, params OleDbParameter[] OleDbParameters)
{
DataTable Dt = null;
try
{
OleDbCommand comm = CreateCommand(CommandText, Connection, OleDbParameters);
OleDbDataAdapter da = new OleDbDataAdapter(comm);
DataSet Ds = new DataSet();
da.Fill(Ds);
Dt = Ds.Tables[0];
}
catch (Exception)
{
}
finally
{
if (Connection.State == ConnectionState.Open) Connection.Close();
}
return Dt;
}
#endregion
#region , DataSet 。
/// <summary>
/// , DataSet 。
/// </summary>
/// <param name="Connection">OleDbConnection </param>
/// <param name="CommandText">SQL </param>
/// <param name="OleDbParameters">OleDbParameter </param>
/// <returns></returns>
public static OleDbDataAdapter ExecuteDataAdapter(OleDbConnection Connection, string CommandText, params System.Data.OleDb.OleDbParameter[] OleDbParameters)
{
OleDbDataAdapter Da = null;
try
{
OleDbCommand comm = CreateCommand(CommandText, Connection, OleDbParameters);
Da = new OleDbDataAdapter(comm);
OleDbCommandBuilder cb = new OleDbCommandBuilder(Da);
}
catch (Exception)
{
}
finally
{
if (Connection.State == ConnectionState.Open) Connection.Close();
}
return Da;
}
#endregion
#region , -1[ :INSERT、DELETE、UPDATE SET ]。
/// <summary>
/// , -1[ :INSERT、DELETE、UPDATE SET ]。
/// </summary>
/// <param name="Connection">OleDbConnection </param>
/// <param name="CommandText">SQL </param>
/// <param name="OleDbParameters">OleDbParameter </param>
/// <returns> </returns>
public static int ExecuteNonQuery(OleDbConnection Connection, string CommandText, params System.Data.OleDb.OleDbParameter[] OleDbParameters)
{
int i = -1;
try
{
if (Connection.State == ConnectionState.Closed) Connection.Open();
OleDbCommand comm = CreateCommand(CommandText, Connection, OleDbParameters);
i = comm.ExecuteNonQuery();
}
catch (Exception)
{
}
finally
{
if (Connection.State == ConnectionState.Open) Connection.Close();
}
return i;
}
#endregion
#region , null
/// <summary>
/// , null
/// </summary>
/// <param name="Connection">OleDbConnection </param>
/// <param name="CommandText">SQL </param>
/// <param name="OleDbParameters">OleDbParameter </param>
/// <returns> </returns>
public static object ExecuteScalar(OleDbConnection Connection, string CommandText, params System.Data.OleDb.OleDbParameter[] OleDbParameters)
{
object Result = null;
try
{
OleDbCommand comm = CreateCommand(CommandText, Connection, OleDbParameters);
Result = comm.ExecuteScalar();
}
catch (Exception)
{
}
finally
{
if (Connection.State == ConnectionState.Open) Connection.Close();
}
return Result;
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="Connection">OleDbConnection </param>
/// <param name="CommandText">SQL </param>
/// <param name="OleDbParameters">OleDbParameter </param>
/// <returns>DataReader </returns>
public static OleDbDataReader ExecuteDataReader(OleDbConnection Connection, string CommandText, params System.Data.OleDb.OleDbParameter[] OleDbParameters)
{
OleDbDataReader Odr = null;
try
{
OleDbCommand comm = CreateCommand(CommandText, Connection, OleDbParameters);
Odr = comm.ExecuteReader();
}
catch (Exception)
{
}
finally
{
if (Connection.State == ConnectionState.Open) Connection.Close();
}
return Odr;
}
#endregion
#region Excel
/// <summary>
/// Excel
/// </summary>
/// <param name="Connection">OleDbConnection </param>
/// <returns></returns>
public static DataTable GetWorkBookName(OleDbConnection Connection)
{
DataTable Dt = null;
try
{
if (Connection.State == ConnectionState.Closed) Connection.Open();
Dt = Connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
}
catch (Exception)
{
}
finally
{
if (Connection.State == ConnectionState.Open) Connection.Close();
}
return Dt;
}
#endregion
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.