Thursday, March 1, 2012

Generating Excel Report with Microsoft.Office.Interop.Excel.dll and C#

I was developing a page which generated Excel report from code behind and the excel report to be password protected and to be maied to the user group to which the user who clicked on report generation belongs to. I had added a reference to Microsoft Excel 12.0 Object Library under the COM tab when I did the "Add Reference".

The server machine (production server) where I deployed the application/page did not have MS Office or Excel installed. The report generation was not happening. I was getting the following error:

Retrieving the COM class factory for component with CLSID {00020820-0000-0000-C000-000000000046} failed due to the follo
wing error: 80040154.

It took little time for me to identify that it is just because the excel is not installed on the server. I took the Interop.Excel.dll dll from the GAC of my development machine to the BIN folder of the prodiction server. But this also didnt help. I was stuck with the same error. Realised that there is no other go unless I install Excel or Office on the production server.

You need to have Microsoft Excel installed in order to have access to the Introp libraries that you need to add as references to your project in order to make it work.

To use the features of a Microsoft Office application, we need to use the primary interop assembly (PIA) for the application. The primary interop assembly enables managed code to interact with a Microsoft Office application's COM-based object model.
To apply PIA that can obtain the reference to interop with excel in your program, you have to install Excel.

Here is a nice option of generating excel file without having MS office/MS excel installed on the server, by Carlos Aguilar Mares
http://www.carlosag.net/Tools/ExcelXmlWriter/
BLOG:
http://blogs.msdn.com/b/carlosag/archive/2005/08/27/whyiwroteexcelxmlwriter.aspx

No comments: