Reputation: 23
I'm having an issue with my asp project (C#).
I'm have the following error : Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 thrown when I try to generate an excel file from the server. I've already seen some posts about it and I dont know if they apply to me nor I could solve my issue with them. Let me show you my code and explain.
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
private Excel.Application oXL;
private Excel._Workbook oWB;
private ExcelFile f;
protected void bExcel_Click(object sender, EventArgs e)
{
if (Session["mode"] == null)
Response.Redirect(accueil);
// Set the values
oXL = new Excel.Application();
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
f = new ExcelFile((Excel._Worksheet)oWB.ActiveSheet);
oXL.Visible = true;
/* displaying data from a gridview, non revelant here (?) */
oXL.Visible = true;
f = null;
oXL = null;
oWB = null;
}
And the explanation of the error (line 225) :
Erreur source:
Ligne 224 : // Set the values
Ligne 225 : oXL = new Excel.Application();
Ligne 226 : oWB = (Excel._Workbook) oXL.Workbooks.Add(Missing.Value));
Ligne 227 : f = new ExcelFile((Excel._Worksheet)oWB.ActiveSheet);
Okay, so I'm having this issue when I try to generate the excel file from the server, but not when I run the debbugging mode with a local version (the code works). All workstations in my company have MS Office Excel installed on the computer, but not the server I am using, and my question is is there a solution by not having to install Excell on the server ?
I try to avoid installing Excel on the server because the server is not from my service, so I would like to avoid it (but if it is the only (and best/safe) solution, I will do it) if I can. I tried copying the libraries in an assembly folder on the server (since all the workstations are the same, the references shoud work ?) but I could not find the permissions I had to give to permit access to the libraries from the server (I hope I'm not explaining it too bad).
Thanks in advance,
EDIT : There is a thing that I don't understand, it's that in the previous code that was already written, the Excel application could be launched like this :
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=registrationData.xls");
// Pour les caractères spéciaux (é, à, etc...)
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
using (StringWriter sw = new StringWriter())
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
Table table = new Table();
TableRow tRow = new TableRow();
/* Adding each information of the grid using tRow.Cells.Add() */
table.RenderControl(htw);
// Response needs the Htmlwriter
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
And this was working, without having Excel installed, because (I'm not sure though) it only called the Excel application, filled it with the buffer (StringWriter). But I don't want to use a table, because a table is boring to use and I already made the file using Interop... If I could find the permissions to give to use assemblies installed on the server, knowing that all working stations have Excel, I should be able to use it the way this code uses ?
Upvotes: 2
Views: 1240
Reputation: 23
Since my internship finishes this week (yeah, a little late to find something like this), I cant work on anything new now, it's too late, so I'll just revert the changes and use the previous process of creating Excel files (that I had improved though).
I believe, the way to fix this issue could be giving permissions to use the Interop assemblies from the server to then call the users Excel program, but I couldn't set the permission (it didnt work, don't know why). Now it is to late, I'll just do some maintenance and ensure the continuity of the project.
Thanks for the answers guys.
Upvotes: 0
Reputation: 70369
Interop is not supported in sever-scenarios by MS.
There are many options to read/edit/create Excel files without Interop/installing Excel on the server:
MS provides the free OpenXML SDK V 2.0 - see http://msdn.microsoft.com/en-us/library/bb448854%28office.14%29.aspx (XLSX only)
This can read+write MS Office files (including Excel).
Another free option see http://www.codeproject.com/KB/office/OpenXML.aspx (XLSX only)
IF you need more like handling older Excel versions (like XLS, not only XLSX), rendering, creating PDFs, formulas etc. then there are different free and commercial libraries like ClosedXML (free, XLSX only), EPPlus (free, XLSX only), Aspose.Cells, SpreadsheetGear, LibXL and Flexcel etc.
Upvotes: 4