问题
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