如何更正'System.Runtime.InteropServices.COMException'的错误?

时间:2017-04-26 11:37:06

标签: c# excel visual-studio ms-office

我正在尝试将 dataGrid 导出到 excel 文件中,但我一直收到此错误:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Test.exe
Additional information: Ancien format ou bibliothèque de types non valide. 

这是我的代码:

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;

            }
        }

如何解决这个问题?

--------------------------- EDIT ------------------- ----------------

我尝试添加这个:

System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");

并且它起作用,似乎问题是由于语言冲突。

1 个答案:

答案 0 :(得分:0)

我尝试添加这个:

System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");

并且它起作用,似乎问题是由于语言冲突造成的。