从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