How to create an instance of Excel if Excel is not installed

19

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?

c#
excel
interop
comexception
asked on Stack Overflow Sep 11, 2012 by Mahender • edited Dec 14, 2015 by Hakan Fıstık

4 Answers

18

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);
    }
}
answered on Stack Overflow Sep 11, 2012 by Seth Flowers • edited Oct 16, 2018 by Seth Flowers
15

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.

answered on Stack Overflow Sep 11, 2012 by trailmax • edited Nov 22, 2017 by trailmax
0

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

answered on Stack Overflow Sep 11, 2012 by Aghilas Yakoub
0

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.

answered on Stack Overflow Sep 11, 2012 by Gromer

User contributions licensed under CC BY-SA 3.0