Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 769
Mahender Avatar asked Sep 11 '12 18:09

Mahender


People also ask

How do I create an instance in Excel?

Right click on the Excel icon in the taskbar. As the menu appears, hold down the ALT key and left-click on the 'Excel' menu option. Continue to hold down the ALT key until the below window pops up. Press 'Yes' to open a new instance.

How do I find my instance of Excel?

The first way to check if there are any other instances of Excel running is to open the Task Manager. To do this, press Ctrl + Shift + Esc . Then, click on the "Processes" tab. You should see a list of all the processes that are running on your computer.

What is an Excel instance?

Each iteration of Excel.exe in Task Manager is known as one "instance". Excel has some features that are dependent on the instance it is running inside. You can see it as Excel only being able to communicate fully between files that are running in the same instance.

How do I make Excel open in a new instance of default?

Right click on the Excel icon in the taskbar. As the menu appears, hold down the Alt-key and left-click on the 'Excel' menu option. Hold down the Alt-key until the below window pops up. Press Yes to open a new instance of Excel.


2 Answers

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);     } } 
like image 66
Seth Flowers Avatar answered Sep 20 '22 16:09

Seth Flowers


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.

like image 33
trailmax Avatar answered Sep 20 '22 16:09

trailmax