Class not registered error when creating Excel wor

2019-03-14 07:08发布

When I try to access an Excel spreadsheet using the following code I get a "Library not registered' error when defining the workbook object wrkbuk using C# from Visual Studio 2012 with Office 2007 (ver 12) installed

Microsoft.Office.Interop.Excel.Application excapp = new Microsoft.Office.Interop.Excel.Application();
        string bookname = @"C:\Users\Public\Documents\RECRUITMENT & SELECTION\MOVEMENTS\MOVEMENTS\Miscellaneous Documents\VacanciesREAL.xls";
        Workbook wrkbuk = excapp.Workbooks.Open(bookname); 
        Worksheet wrksht = new Worksheet();

The error details are

System.InvalidCastException was unhandled HResult=-2147467262
Message=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: Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED)). Source=mscorlib

I've created a reference to Microsoft.Office.Interop.Excel.dll in the Office 14 Primary Interop Assembly that comes with VS 2012 and also for the Office 12 version of the dll and neither fixes the problem. I've tried registering the dlls with regasm but this does not help either.

I can create the worksheet wrksht on the following line OK with the Office 14 and Office 12 dlls so the problem seems only affect workbook definition.

Dec 17 2013

Tried reinstalling Office 2007 to no avail but found this solution, which worked. It was at

http://social.msdn.microsoft.com/Forums/vstudio/en-US/d3f92da7-96d3-404b-89d0-d236800ceae5/vs-2012-rc-and-visual-studio-tools-for-office?forum=vsto

Problem is code from multiple versions of Office - I had code from Office 14, possibly coming from VS 2012 install

I suspect that VS 2012 RC has installed the Office 2013 type libraries, and that there are now duplicate versions registered. I got that specific error message

('unable to cast _Application'....'TYPE_E_LIBNOTREGISTERED')

after a messy uninstall, and then re-installing an older version of Office. I solved it by following the advice of another forum thread here which I'm sadly unable to find...basically I searched the registry for the CLSID in the error message, so, {00020970-0000-0000-C000-000000000046}. Its registry key contained two keys, one of which was called 'TypeLib', which in turn contained another CLSID for the type library. I then searched the registry, starting at the beginning again, for that second CLSID, which led me to the relevant interop type library. It had two entries....8.3 and 8.4, the bigger number corresponding to the later version of Office....which I deleted...and immediately was able to run my program.

7条回答
forever°为你锁心
2楼-- · 2019-03-14 07:47

This is an answer if you can not modify or delete the regedit records. I had the same problem, in the company where I work, there are about 30 machines with office 2010 and I could not erase or open records in the regedit. because the client machine was in a domain that would not let me. although I had administrator permissions it did not allow me to delete records in the regedit. I tried to find a solution for 1 month, and I did not find it. so I had to stop using interop and migrate to OpenXml to edit excel templates and ClosedXml to create new excel. This saved my life, because my boss was already pressing me.

Open the project/solution in Visual Studio, and open the console using the Tools > NuGet Package Manager > Package Manager Console command. and install 2 packages

  1. Install-Package DocumentFormat.OpenXml -Version 2.5.0
  2. Install-Package SpreadsheetLight

pd: OpenXml 2.8 dont work with SpreadsheetLight, better use 2.5.0

add this in the beggining

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using SpreadsheetLight;

in your button or class copy and modify

    SaveFileDialog fichero = new SaveFileDialog();
    fichero.Filter = "Excel (*.xlsx)|*.xlsx";
    if (fichero.ShowDialog() == DialogResult.OK)
    {
        SLDocument sl = new SLDocument("c:\\bin\\est1.xlsx", "Sheet1");

        sl.SetCellValue("E9", "Let's party!!!!111!!!1");

        sl.SelectWorksheet("Sheet2");
        sl.SetCellValue("C7", "Before anyone calls the popo!");

        sl.AddWorksheet("ERTRT");
        sl.SetCellValue("B4", "Who let the dogs out?");
        sl.SetCellValue("B5", "Woof!");

        sl.SaveAs(fichero.FileName);    

        MessageBox.Show("Report " + fichero.FileName + "!");
    }
查看更多
登录 后发表回答