![](/img/trans.png)
[英]unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'"
[英]C#: some of machines are throwing following error: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass'
我创建了一个 C# function ,它将DataGridView
保存到 Excel ,它适用于大多数机器。 但是,有些机器会抛出以下错误:
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)).
有什么办法可以解决这个问题吗?
这是我的源代码:
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;
}
我正在使用.NET framework 4.6.1
环境。
感谢您的阅读。 祝你今天过得愉快:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.