從xlsx文件讀取日期時出現了一些問題,我的意思是它沒有讀取單元格中的所有信息,例如我有在單元格這樣的信息:C#Excel Interop:無法讀取excel中的所有數據
"4876112","3456151712","345627712","3125HPC21017500011","34131HPC210349014112","35134HPC210273008212"
,當我檢查,看看數據表,我只看到了這行信息
"4876112","3456151712","345627712",
這是我的代碼:
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = excelApp.Workbooks.Open(input, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
string sheetname = "";
foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in workbook.Sheets)
{
sheetname = sheet.Name;
break;
}
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", input);
string query = String.Format("select * from [{0}$]", sheetname);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
tabela = ds.Tables[0];
我輸入並將Excel的單元格形成文本並正常工作。我可以編程方式將所有單元格格式更改爲文本。 謝謝!
爲什麼你使用OleDB讀取excel數據,當你已經使用Interop for Excel?您可以從「工作表」中讀取數據。 – TcKs
注意:'sheetname = workbook.Sheets [0] .Name'會更簡單(而不是'foreach'和'break')嗎? – Filburt