關於Excel的讀取

Excel 鏈接字符串sql

 string xlsConnFormat = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";

一、HDR表示要把第一行做爲數據仍是做爲列名,做爲數據用HDR=NO,做爲列名用HDR=YES;數據庫

二、經過IMEX=1來把混合型做爲文本型讀取,避免null值。ide

注意:把一個 Excel 文件看作一個數據庫,一個sheet看作一張表。語法 "SELECT * FROM [sheet1$]",表單要使用"[]"和"$"。sqlserver

 

使用 OLEDB 讀取不一樣版本 Excel 數據的鏈接字符串設置ui

用OLEDB經過設置鏈接字符串能夠像讀取sqlserver同樣將excel中的數據讀取出來,可是excel2003和excel2007/2010的鏈接字符串是不一樣的 spa

/// <summary>    
   /// 把數據從Excel裝載到DataTable    
   /// </summary>    
   /// <param name="pathName">帶路徑的Excel文件名</param>    
   /// <param name="sheetName">工做表名</param>
   /// <param name="tbContainer">將數據存入的DataTable</param>
   /// <returns></returns> 
   public DataTable ExcelToDataTable(string pathName, string sheetName)
   {
       DataTable tbContainer = new DataTable();
       string strConn = string.Empty;
       if (string.IsNullOrEmpty(sheetName))
       { 
         sheetName = "Sheet1";
       } 
       FileInfo file = new FileInfo(pathName);
       if (!file.Exists) 
       { 
         throw new Exception("文件不存在");
       } 
       string extension = file.Extension;
       switch (extension)
       {
         case ".xls":
              strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
              break;
         case ".xlsx":
              strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathName + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";
              break;  
        default: 
              strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
              break; 
       }       
     //連接Excel
     OleDbConnection cnnxls = new OleDbConnection(strConn);
     //讀取Excel裏面有 表Sheet1
     OleDbDataAdapter oda = new OleDbDataAdapter(string.Format("select * from [{0}$]", sheetName), cnnxls);   
     DataSet ds = new DataSet();  
     //將Excel裏面有表內容裝載到內存表中!
     oda.Fill(tbContainer); 
     return tbContainer; 
   } 

 讀取Excel文件時,可能一個文件中會有多個Sheet,所以獲取Sheet的名稱是很是有用的。excel

//根據Excel物理路徑獲取Excel文件中全部表名  
 public static String[] GetExcelSheetNames(string excelFile)  
 { 
          OleDbConnection objConn = null;  
          System.Data.DataTable dt = null;    
             try 
             {  
                 //string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + excelFile + ";Extended Properties='Excel 8.0; HDR=NO; IMEX=1'"; //此鏈接只能操做Excel2007以前(.xls)文件  
                    string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + excelFile + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'"; //此鏈接能夠操做.xls與.xlsx文件  
                objConn = new OleDbConnection(strConn);  
                 objConn.Open();  
                 dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);  
                 if (dt == null)  
                 {  
                     return null;  
                 }  
                 String[] excelSheets = new String[dt.Rows.Count];  
                 int i = 0;  
                 foreach (DataRow row in dt.Rows)  
                 {  
                     excelSheets[i] = row["TABLE_NAME"].ToString();  
                    i++;  
                 }  
                  return excelSheets;  
             }  
             catch 
             {  
                 return null;  
             }  
             finally 
             {  
                 if (objConn != null)  
                 {  
                     objConn.Close();  
                     objConn.Dispose();  
                 }  
                 if (dt != null)  
                 {  
                    dt.Dispose();  
                 }  
            }  
 }  
相關文章
相關標籤/搜索