日期:2014-05-17  浏览次数:20888 次

Excel转换DataTable
/// <summary>        
        /// 根据传入的Excel表输出Datatable       
        /// </summary>       
        ///  <param name="strExcelFileName">Excel表路径及文件名称</param>       
        /// <param name="strTableName">数据表名</param>       
        ///  <returns>DataTable:mytable</returns>      
        public System.Data.DataTable LeadIn(string strExcelFileName, string strSheetName)
        {
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
            string strExcelSql = string.Format("select * from [{0}$]", strSheetName);
            System.Data.DataSet ds = new DataSet();
            OleDbConnection conn = new OleDbConnection(strConn);
            try
            {
                OleDbDataAdapter adapter = new OleDbDataAdapter(strExcelSql, strConn);
                adapter.Fill(ds, "mytable");
                return ds.Tables["mytable"];
            }
            catch (System.Data.OleDb.OleDbException e)
            {
                System.Console.WriteLine("OleDbException:{0}", e);
            }
            finally