最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】C#程序excel处理出错:Additional information: Unable to cast COM object of type ‘Microsoft.Office.Interop.Excel.ApplicationClass’ to interface type ‘Microsoft.Office.Interop.Excel._Application’

C# crifan 12825浏览 0评论

【问题】

之前的C#代码,处理excel正常的,结果现在出错了:

An unhandled exception of type ‘System.InvalidCastException’ occurred in FiverrComScraper.exe

Additional information: Unable to cast COM object of type ‘Microsoft.Office.Interop.Excel.ApplicationClass’ to interface type ‘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: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).

如图:

Unable to cast COM object of type Microsoft.Office.Interop.Excel.ApplicationClass to interface type Microsoft.Office.Interop.Excel._Application

相关代码:

    public void dgvExportToExcel(  DataGridView dgvValue,
                                            string excelFullFilename,
                                            bool isAutoFit = true,
                                            bool isHeaderBold = true,
                                            List<int> omitRowIdxList = null,
                                            List<int> omitColumnIdxList = null,
                                            List<int> useTagColumnIdxList = null)
    {
        Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
                
        object misValue = System.Reflection.Missing.Value;
        xlApp = new Excel.ApplicationClass();
        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

 

【解决过程】

1.参考:

How to solve “Unable to cast COM object of type Microsoft.Office.Interop.Excel.ApplicationClass’ to interface type ‘Microsoft.Office.Interop.Excel._Application’”

和:

C#与excel互操作的错误无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制

都谈到的是,删除对应的注册表项,就可以了。

但是我此处觉得奇怪的是:

我的当前的win7,之前代码可以正常运行,

最后也没有重新安装office,为何会导致代码无法工作了?

难道是安装了其他的什么东西影响到了excel?

2.先不管,参考上面的,去注册表中看看。

先去导出,以便备份:

export 1.7 from reg

再去删除:

delete v17 excel reg

重新运行代码试试,就可以了:

now excel save can run ok

 

【总结】

还是有点诡异,抽空再深究深层次原因。

暂时是把:

对应的注册表部分:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.7]
"PrimaryInteropAssemblyName"="Microsoft.Office.Interop.Excel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"
@="Microsoft Excel 14.0 Object Library"

[HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.7\0]

[HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.7\0\win32]
@="C:\\Program Files (x86)\\Microsoft Office\\Office14\\EXCEL.EXE"

[HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.7\FLAGS]
@="0"

[HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.7\HELPDIR]
@="[{00020813-0000-0000-C000-000000000046}]"

删除掉,即可暂时解决此问题。

转载请注明:在路上 » 【已解决】C#程序excel处理出错:Additional information: Unable to cast COM object of type ‘Microsoft.Office.Interop.Excel.ApplicationClass’ to interface type ‘Microsoft.Office.Interop.Excel._Application’

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 解决了我遇到的问题,谢谢亲
    cgw5年前 (2018-12-19)回复
85 queries in 0.162 seconds, using 22.14MB memory