如何纠正'System.Runtime.InteropServices.COMException'的这个错误?
问题描述:
我试图的数据网格导出到Excel的文件,但我不断收到此错误:如何纠正'System.Runtime.InteropServices.COMException'的这个错误?
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;
}
}
如何纠正呢?
---------------------------编辑------------------ -----------------
我尝试添加此:
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
和它的工作,好像这个问题是由于语言冲突。
答
我尝试添加此:
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
和它的工作,好像这个问题是由于语言冲突。
检查您引用的互操作性的版本。然后检查注册表HKEY_CLASSES_ROOT - > TypeLib。查找PrimaryInteropAssemblyName并确保您具有相同的版本注册。如果你有14.0和15.0这可能是你的问题。 – Wheels73
该程序是参考MicroSoft Office Interop Excel版本12.0.0.0,我如何找到该注册表?对不起,我是新手。 – User001122
你好,完全一样。只需检查注册表。您可以在TypeLib键下搜索Microsoft.Office.Interop.Excel,使用(Ctrl F)查找 – Wheels73