4
votes

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
Could you please add the code in which you export the data to Excel?Archimaredes
What version of Excel are you using?Archimaredes
I' m using Office Professional Plus 2013 and also Microsoft Access Database Engine 2010 version is installed to the computer.Anil Kocabiyik
Take a look at DocumentFormat.OpenXml instead since you have the data in c#. Google it or look at this tutorial for example dispatchertimer.com/tutorial/…Daniel Stackenland

1 Answers

1
votes

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.