2016-05-17 134 views
2

我目前正在嘗試從Excel電子表格中讀取單元格,並且它在我不需要時似乎會重新格式化單元格。我希望它作爲計劃文本來完成。我已經閱讀了幾個針對這個問題的解決方案,並且已經實現了它們,但我仍然遇到同樣的問題。從Excel中讀取列,重新格式化單元格

閱讀器將日期的數字和數字變成日期。

例子:

週五2016年1月29日出來是:42398

40.00出來是:1900年2月9日上午12點00分○○秒

代碼:

string stringconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + files[0] + ";Extended Properties=\"Excel 12.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text\""; 
try { 
    OleDbConnection conn = new OleDbConnection(stringconn); 
    OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [CUAnswers$]", conn); 
    DataTable dt = new DataTable(); 
    try { 
     printdt(dt); 

我試圖

IMEX=0; 
HDR=NO; 
TypeGuessRows=1; 

這是我如何打印出的紙張

public void printdt(DataTable dt) { 
     int counter1 = 0; 
     int counter2 = 0; 
     string temp = ""; 
     foreach (DataRow dataRow in dt.Rows) { 
      foreach (var item in dataRow.ItemArray) { 
       temp += " ["+counter1+"]["+counter2+"]"+ item +", "; 
       counter2++; 
      } 
      counter1++; 
      logger.Debug(temp); 
      temp = ""; 
      counter2 = 0; 
     } 
    } 

回答

1

我也有類似的問題,但它是使用互操作來讀取Excel電子表格。這爲我工作:

var value = (range.Cells[rowCnt, columnCnt] as Range).Value2; 
string str = value as string; 
DateTime dt; 
if (DateTime.TryParse((value ?? "").ToString(), out dt)) 
{ 
    // Use the cell value as a datetime 
} 

Editted增加新的思路

我要建議保存電子表格作爲逗號分隔值。然後Excel將單元格轉換爲文本。在C#中解析CSV很容易。

這導致我想到如何以編程方式進行轉換,這在Convert xls to csv programmatically中涵蓋。也許在接受答案中的代碼是你正在尋找的:

string ExcelFilename = "c:\\ExcelFile.xls"; 
DataTable worksheets; 
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=" + ExcelFilename + ";" + @"Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""; 
using (OleDbConnection connection = new OleDbConnection(connectionString)) 
{ 
    connection.Open(); 
    worksheets = connection.GetSchema("Tables"); 
    foreach (DataRow row in worksheets.Rows) 
    { 
     // For Sheets: 0=Table_Catalog,1=Table_Schema,2=Table_Name,3=Table_Type 
     // For Columns: 0=Table_Name, 1=Column_Name, 2=Ordinal_Position 
     string SheetName = (string)row[2]; 
     OleDbCommand command = new OleDbCommand(@"SELECT * FROM [" + SheetName + "]", connection); 
     OleDbDataAdapter oleAdapter = new OleDbDataAdapter(); 
     oleAdapter.SelectCommand = command; 
     DataTable dt = new DataTable(); 
     oleAdapter.FillSchema(dt, SchemaType.Source); 
     oleAdapter.Fill(dt); 
     for (int r = 0; r < dt.Rows.Count; r++) 
     { 
      string type1 = dr[1].GetType().ToString(); 
      string type2 = dr[2].GetType().ToString(); 
      string type3 = dr[3].GetType().ToString(); 
      string type4 = dr[4].GetType().ToString(); 
      string type5 = dr[5].GetType().ToString(); 
      string type6 = dr[6].GetType().ToString(); 
      string type7 = dr[7].GetType().ToString(); 
     } 
    } 
} 
+0

這沒有幫助。我不想重新轉換它。我希望它以正確的格式出來。或者就像計劃文本一樣。 –

+0

在我看來,如果你想讀一個單元格作爲字符串,那麼它需要輸入爲一個字符串,而不是一個日期時間(或其他)。您可能需要更改電子表格並將這些單元格轉換爲文本,可能需要用單引號將值加前綴。 (這可能不是你想要的答案。) –

+0

@Sari請在我的編輯中看到新的想法。 –