Export Excel File with .xlsx Extension

半城伤御伤魂 提交于 2019-12-10 17:35:24

问题


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

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

'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'

error.

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

But xlsx option must be worked without changing extension.

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

How can I solve this problem?


回答1:


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

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



来源:https://stackoverflow.com/questions/35012472/export-excel-file-with-xlsx-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!