C\#상용 도구 클래스-Excel 작업 클래스

다음으로 이동:http://www.cnblogs.com/zfanlong1314/p/3916047.html
 
 
///      ——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
    }
}

좋은 웹페이지 즐겨찾기