25
votes

In my C# app, with the help of Excel Interop dll (as reference) i am reading/writing excel files. If I move this program to system where office/excel is not installed (think of clean machine), i am hitting with below error.

System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

Above error is expected since there is no excel on target machine.
My question is, is there any other way to use my program apart from registering Interop dll on target machine?

4

4 Answers

25
votes

My question is, is there any other way to use my program apart from registering Interop dll on target machine?

You are asking if there is any way to use your program, which uses Excel interop, when Excel is not installed on the computer running your program. The short answer is no. The longer answer is yes, if you are willing to refactor your program to not use interop.

You can use the OOXml SDK provided by Microsoft if the version of Excel you are targeting is 2007 and up. You can also use a third party library such as Aspose if you are willing to spend a little bit of money.

An example of using the OOXml SDK for inserting a spreadsheet into an excel file can be found in the microsoft docs.

// Given a document name, inserts a new worksheet.
public static void InsertWorksheet(string docName)
{
    // Open the document for editing.
    using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true))
    {
        // Add a blank WorksheetPart.
        WorksheetPart newWorksheetPart = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
        newWorksheetPart.Worksheet = new Worksheet(new SheetData());

        Sheets sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>();
        string relationshipId = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart);

        // Get a unique ID for the new worksheet.
        uint sheetId = 1;
        if (sheets.Elements<Sheet>().Count() > 0)
        {
            sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
        }

        // Give the new worksheet a name.
        string sheetName = "Sheet" + sheetId;

        // Append the new worksheet and associate it with the workbook.
        Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
        sheets.Append(sheet);
    }
}
16
votes

Even Microsoft does not recommends usein Interop libraries on a server. So best to find some alternative framework to do Excel for you. I have successfully used Npoi in the past for that purpose.

I know this is not an answer to your exception. But honestly, Interop is a path to an endless trouble and cryptic exception messages.

Update 2017: I have not used NPOI for a while now and moved all my projects to EPPlus insted - library based on OpenXML that creates you modern xlsx files.

0
votes

You can register your COM with RegSrv32

Link : http://msdn.microsoft.com/en-us/library/ms859484.aspx

You are obliged to register your COM before using

0
votes

From all the research I did a while ago when I needed to interact with an Excel file on a server, you have to install Office.