I'm trying to export dataGrid into an excel file but I keep getting this error:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Test.exe
Additional information: Ancien format ou bibliothèque de types non valide.
Here's my code:
Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application(); //this generates the error
Workbook wb = Excel.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)Excel.ActiveSheet;
Excel.Visible = true;
ws.Cells[1, 1] = "Column1";
ws.Cells[1, 2] = "Column2";
ws.Cells[1, 3] = "Column3";
ws.Cells[1, 4] = "Column4";
ws.Cells[1, 5] = "Column5";
for (int j=2; j <= dataGridView1.Rows.Count; j++)
{
for(int i= 2; i <= 5; i++)
{
ws.Cells[j, i] = dataGridView1.Rows[j-2].Cells[i-1].Value;
}
}
How to correct this?
---------------------------EDIT-----------------------------------
I tried adding this :
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
and it worked, it seems like the problem was due to a language conflict.