2012-10-03 23 views
1

我有一個C#程序應該從一個小的四列電子表格(.xlsx)中提取數據。它讀取的第一列正常,但是當它到達第二個我得到以下錯誤:錯誤「指定的轉換無效」在從C#中的電子表格中提取時出現

「指定的轉換無效」

我已經檢查並重新檢查出的小區的格式,第一欄和第二欄之間沒有區別。以下是電子表格中第一行的值。每個值代表一個單獨的列。

121220 330004 Dual 02/22/2012

這裏是我使用的代碼。 Id變量加載得很好,它是導致問題的courseCode。

string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" 
    + "Data Source=C:\Projects\sym_AgentServices_INT\Book1.xlsx;" 
    + "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"; 
string queryString = "SELECT * FROM [CE$]"; 

try 
{ 
    OleDbDataReader reader; 
    using (OleDbConnection connection = new OleDbConnection(connectionString)) 
    { 
     OleDbCommand command = new OleDbCommand(queryString, connection); 
     connection.Open(); 
     reader = command.ExecuteReader(); 

     while (reader.Read()) 
     { 
      string Id = ""; 
      string courseCode = ""; 
      string partner = ""; 
      string effectiveDate = ""; 
      string expirationDate = ""; 

      Id = reader.GetString(0); 
      courseCode = reader.GetString(1); 
      partner = reader.GetString(2); 
      effectiveDate = reader.GetString(3); 
      expirationDate = reader.GetString(4); 
     } 
    } 
} 

編輯

我已經分析了在運行時的讀者對象,發現如下:通過擴大基地的一些System.Data.OleDb.OleDbDataReader的時間和開放_bindings財產我發現,那裏只有一個條目(「0」)。但是,如果我打開0,然後展開_columnBindings屬性,我發現值爲0 - 4.

從reader中提取數據時,我的語法可能不正確(「reader.GetString(x)」)?

FINAL EDIT

代替使用.GetString(x)的我會強烈建議使用簡單的讀取器[X]的ToString()。這解決了這個問題。

+0

什麼實際價值courseCode也是xcel電子表格中courseCode,定義爲Int,Text,... ect – MethodMan

+0

它被定義爲「一般」,與第一列相同。我在電子表格中列出的值是courseCode的實際值。沒有任何計算可以獲得該數字。 – NealR

+2

嘗試閱讀器[1] .ToString(); ? – Brett

回答

1

當我使用閱讀器[N]的ToString(),錯誤消失對我來說:

 string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=d:\temp\Book1.xlsx; Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"; 
     string queryString = "SELECT * FROM [CE$]"; 

     OleDbDataReader reader; 
     using (OleDbConnection connection = new OleDbConnection(connectionString)) 
     { 
      OleDbCommand command = new OleDbCommand(queryString, connection); 
      connection.Open(); 

      DataSet ds = new DataSet("Book1"); 

      IDataAdapter adapter = new OleDbDataAdapter(queryString, connection); 
      adapter.Fill(ds); 

      reader = command.ExecuteReader(); 

      while (reader.Read()) 
      { 
       string Id = ""; 
       string courseCode = ""; 
       string partner = ""; 
       string effectiveDate = ""; 
       string expirationDate = ""; 

       Id = reader[0].ToString(); 
       courseCode = reader[1].ToString(); 
       partner = reader[2].ToString(); 
       effectiveDate = reader[3].ToString(); 
       expirationDate = reader[4].ToString(); 

       //Id = reader.GetString(0); 
       //courseCode = reader.GetString(1); 
       //partner = reader.GetString(2); 
       //effectiveDate = reader.GetString(3); 
       //expirationDate = reader.GetString(4); 
      } 
     } 

這裏是我的測試數據集...

ID Course Code  Partner Effective Date Expiration Date 
100 5    MS  10/3/2012  10/3/2013 
200 21-400   Oracle 10/3/2012  10/3/2013 
300     Goog 10/3/2012  10/3/2013 
+0

這工作。我有另一個程序,我使用reader.GetString()方法,但我想這是不正確的。謝謝 – NealR

相關問題