Click here to Skip to main content
16,016,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am looking for a fastest way for exporting datagridview content to Excel-2010.I

have already done by converting first dataset to xml then to .xlsx.But finding some

issues in this solution.Is this method is fastest?Or any other method?

I will give the code what I have already done


idssTableDataSet.WriteXml("D:\\Customers.xml");


Microsoft.Office.Interop.Excel.Application APexcel = null;
Microsoft.Office.Interop.Excel.Workbook MyBook = null;
Microsoft.Office.Interop.Excel.Worksheet MySheet = null;


APexcel = new Microsoft.Office.Interop.Excel.Application();
APexcel.Visible = false;
MyBook = APexcel.Workbooks.Open("D:\\Customers.xml", Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
MySheet = (Microsoft.Office.Interop.Excel.Worksheet)MyBook.ActiveSheet;
MyBook.SaveAs("D:\\Customers.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, null, null, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, false, false, null, null, null);


After saving the .xlsx file it is showing an error that "Excel cannot open the file because the file format or extension is not valid.Verify that the file has not been corrupted and the file extension matches the format of the file" when I am opening the .xlsx file.Please help me.

Thanks in advance
Posted

1 solution

Have a look at this exampel, it might help.

How to export DataGridView to excel file
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900