[英]C#: some of machines are throwing following error: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass'
I had created a C# function which saves DataGridView
to Excel which works well on most of machines.我创建了一个 C# function ,它将DataGridView
保存到 Excel ,它适用于大多数机器。 However, some of machines are throwing following error:但是,有些机器会抛出以下错误:
Microsoft.Office.Interop.Excel._Application. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Library not registered. (Exception from HRESULT 0x8002801D (TYPE_E_LIBNOTREGISTERED)).
Is there any way I can fix this issue?有什么办法可以解决这个问题吗?
This is my source code:这是我的源代码:
private bool dataGridView_ExportToExcel(string fileName, DataGridView dgv, string ext, bool chart)
{
CheckForIllegalCrossThreadCalls = false;
Excel.Application excelApp = new Excel.Application();
if (excelApp == null)
{
MessageBox.Show("No Excel has been installed!");
return false;
}
Excel.Workbook wb = excelApp.Workbooks.Add(true);
Excel._Worksheet workSheet = wb.Worksheets.get_Item(1) as Excel._Worksheet;
workSheet.Name = "C#";
// Print Contents
for (int r = 0; r < dgv.Rows.Count; r++)
{
for (int i = 0; i < dgv.Columns.Count; i++)
{
workSheet.Cells[r + 2, i + 1] = dgv.Rows[r].Cells[i].Value;
}
}
workSheet.Columns.AutoFit();
wb.SaveAs(fileName, Excel.XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlUserResolution, true, Type.Missing, Type.Missing, Type.Missing);
wb.Close(Type.Missing, Type.Missing, Type.Missing);
excelApp.Quit();
releaseObject(excelApp);
releaseObject(workSheet);
releaseObject(wb);
return true;
}
I am using .NET framework 4.6.1
environment.我正在使用.NET framework 4.6.1
环境。
Thank you for reading.感谢您的阅读。 have a nice day:)祝你今天过得愉快:)
It seems some of contents are missing from the windows registry key, windows 注册表项中似乎缺少一些内容,
and this can be happening when MS Excel is downgraded.这可能在 MS Excel 降级时发生。
To fix this issue, Please go to regedit and delete [HKEY_CLASSES_ROOT-->TypeLib-->{00020813-0000-0000-C000-000000000046} --> 1.9]要解决此问题,请 go 注册并删除 [HKEY_CLASSES_ROOT-->TypeLib-->{00020813-0000-0000-C000-000000000046} --> 1.9]
and see if this works.看看这是否有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.