c# - 导出扩展名为 .xlsx 的 Excel 文件

标签 c# excel oledb xlsx oledbdataadapter

我正在尝试使用这些代码导出 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/

相关文章:

c# - 无法使用 oledb 从 csv 读取所有列

c# - 使用 Azure Kinect DK 将 In32Rect 坐标转换为 Span<BGRA>

c# - 如何创建一系列字母和数字的字符串组合?

python - 如何自动缩小图表的范围?

excel日期格式不起作用

c# - OLEDB 连接字符串中的 IMEX 是什么?

c# - OleDB 连接到 Microsoft Access 数据文件的间歇性 "System resource exceeded"异常

c# - Linq - 如何组合两个枚举

c# - C# 中的 override 和 new 关键字有什么区别?

c# - 从 c# 应用程序设置 Excel power view 设置