我正在尝试使用这些代码导出 xlsx 文件:
DataTable dataTable= new DataTable(tableName);
OleDbDataAdapter adapter = new OleDbDataAdapter(select, accessConnection);
adapter.FillSchema(dataTable, SchemaType.Source);
foreach (DataRow result in dtResult.Rows)
{
DataRow newRow = dataTable.NewRow();
foreach (DataRow drAssign in dtAssignment.Rows)
{
newRow[drAssign["Destination"].ToString()] = result[drAssign["Source"].ToString()];
}
dataTable.Rows.Add(newRow);
}
adapter.Update(dataTable);
连接字符串是
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AA\Desktop\work10.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES";
我正在尝试将 100 行导出到 xlsx 文件。当我尝试打开 excel 文件时,我得到了
'Excel can not open the test.xlsx because file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file'
错误。
将 .xlsx 扩展名更改为 .xls 后,文件正在打开。
但 xlsx 选项必须在不更改扩展名的情况下工作。
电脑安装Microsoft Access Database Engine 2010版和Office Professional Plus 2013。
我该如何解决这个问题?
最佳答案
这是因为 OLEDB 保存为二进制文件,而不是 XML,根据 https://support.microsoft.com/en-us/kb/928979
切换到 XML 库来像 ClosedXml 那样做,或者使用标准的 Excel.Interop。
关于c# - 导出扩展名为 .xlsx 的 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35012472/