简体   繁体   English

C#: some of machines are throwing following error: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass'

[英]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.

相关问题 无法将“microsoft.Office.Interop.Excel.ApplicationClass”类型的 COM 对象转换为“microsoft.Office.Interop.Excel.Application”” - unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'" InvalidCastException-无法转换类型为“ Microsoft.Office.Interop.Outlook.ApplicationClass”的COM对象 - InvalidCastException - Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' Microsoft.Office.Interop.Excel.ApplicationClass没有定义构造函数 - Microsoft.Office.Interop.Excel.ApplicationClass has no constructor defined Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' {00063001-0000-0000-C000-000000000046} HRESULT: 0x80040155 - Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' {00063001-0000-0000-C000-000000000046} HRESULT: 0x80040155 如何修复“无法转换类型为‘Microsoft.Office.Interop.Outlook.ApplicationClass’的 COM 对象/RPC 服务器不可用”? - How can I fix “Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' / The RPC server is unavailable”? Executing DTS Package from C# error Unable to cast COM object of type 'Microsoft.SqlServer.Dts.Runtime.Wrapper.ApplicationClass' - Executing DTS Package from C# error Unable to cast COM object of type 'Microsoft.SqlServer.Dts.Runtime.Wrapper.ApplicationClass' C# outlook: Unable to cast COM object of type 'System._ComObject' to Interface type 'Microsoft.Office.Interop.Outlook.MailItem.' - C# outlook : Unable to cast COM object of type 'System._ComObject' to Interface type 'Microsoft.Office.Interop.Outlook.MailItem.' 无法将“System._ComObject”类型的 COM object 转换为“microsoft.Office.Interop.Excel.Application”” - unable to cast COM object of type 'System._ComObject' to 'microsoft.Office.Interop.Excel.Application'" 无法转换“Microsoft.Office.Interop.Excel.WorksheetClass”类型的COM对象 - Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.WorksheetClass' 无法转换COM对象&#39;(Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets&#39; - Unable to cast COM object '(Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM