2012-07-19 54 views
0

我有一個函數從Excel數據導出到數據集,其是如下,將數據從excel導出到數據集的功能錯誤是什麼?

public DataSet GetDataFromExcel(string filePath) 
{ 
    string strConn; 
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + 
    "Data Source=" + filePath + ";" + 
    "Extended Properties=Excel 8.0;"; 
    DataTable dt = new DataTable(); 
    dt = null; 
    using (OleDbConnection oleDB = new OleDbConnection(strConn)) 
    { 
     oleDB.Open(); 
     dt = oleDB.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
     if (dt == null) 
      return null; 

     ArrayList arr = new ArrayList(); 
     //ListItemCollection items = new ListItemCollection(); 
     int i = 0; 

     for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++) 
     { 
      string excelSheetName; 
      string lastCharacter = ""; 

      excelSheetName = dt.Rows[rowIndex]["TABLE_NAME"].ToString(); 
      excelSheetName = excelSheetName.Replace("'", ""); 
      lastCharacter = excelSheetName.Substring(excelSheetName.Length - 1, 1); 
      if (lastCharacter == "$") 
      { 
       arr.Add(dt.Rows[rowIndex]["TABLE_NAME"].ToString()); 
       //items.Add(dt.Rows[rowIndex]["TABLE_NAME"].ToString()); 
      } 
     } 
     //if (items.Count > 1) 
     if (arr.Count > 1) 
      return null; 

     string sName; 
     string query; 

     //sName = items[0].ToString(); 
     sName = arr[0].ToString(); 
     sName = sName.Replace("'", ""); 
     sName = sName.Replace("$", ""); 

     query = ""; 
     query = String.Format("select * from [{0}$]", sName); 
     OleDbDataAdapter da = new OleDbDataAdapter(query, strConn); 
     DataSet ds = new DataSet(); 
     da.Fill(ds); 
     return ds; 
    } 
} 

我試圖將數據從具有900行我的excel工作表中的一個出口。該功能只獲得253行。但我想要所有的行。這個函數有什麼問題?你能幫我嗎?謝謝。

回答

0

下載this然後使用Microsoft.ACE.OLEDB.12.0提供程序。 Jet提供商有一個硬限制。

+0

仍然限制相同... – 2012-07-19 05:34:40