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

时间:2021-11-21 16:58:16

I' m trying to export xlsx file with these codes:

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

The connection string is

连接字符串

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AA\Desktop\work10.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES";

I' m trying to export 100 rows to xlsx file. When I try to open the excel file, I' m getting

我正在尝试将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'

Excel无法打开测试。xlsx因为文件格式或文件扩展无效。验证文件没有被损坏,并且文件扩展名与文件的格式匹配。

error.

错误。

After changing .xlsx extension to .xls, file is opening.

将.xlsx扩展名更改为.xls后,文件将打开。

But xlsx option must be worked without changing extension.

但是xlsx选项必须在不改变扩展的情况下工作。

Microsoft Access Database Engine 2010 version and Office Professional Plus 2013 is installed at the computer.

微软Access Database Engine 2010版本和Office Professional Plus 2013安装在计算机上。

How can I solve this problem?

我如何解决这个问题?

1 个解决方案

#1


1  

It's because OLEDB is saving as a binary file, not XML, as per https://support.microsoft.com/en-us/kb/928979

这是因为OLEDB保存为一个二进制文件,而不是XML,如https://support.microsoft.com/en-us/kb/928979所示

Switch to an XML library to do it like ClosedXml or use the standard Excel.Interop.

切换到一个XML库,像关闭XML或者使用标准的Excel.Interop。

#1


1  

It's because OLEDB is saving as a binary file, not XML, as per https://support.microsoft.com/en-us/kb/928979

这是因为OLEDB保存为一个二进制文件,而不是XML,如https://support.microsoft.com/en-us/kb/928979所示

Switch to an XML library to do it like ClosedXml or use the standard Excel.Interop.

切换到一个XML库,像关闭XML或者使用标准的Excel.Interop。