Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am exporting an excel sheet using C# code which is perfectly working locally. But when i deployed into the server it returning me the error said "retrieving the com class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to following error:80040154". I am using asp.net 4.5 and included the reference of Microsoft.Office.Interop version 14.0.0, I googled the error and found there must be Office com in my server machine so i installed office 2003 but no luck still giving the same error.
Can anyone let me know what software should i install in server machine.?
Please help me its very urgent.
Below the code:

public string ExportExcelFormat()
{
string retError = "";
try
{
System.Data.DataTable dtExcel = new DataTable();

dtExcel.Columns.Add("Name", typeof(string));
dtExcel.Columns.Add("Dept", typeof(string));
dtExcel.Columns.Add("Req", typeof(string));


retError= CreateWorkbook(dtExcel, "");
}
catch { }
return retError;
}

public string CreateWorkbook(System.Data.DataTable dtExcelData, String path1)
{
string retError = "";
int rowindex = 0;
int columnindex = 1;
try
{
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application app;
Microsoft.Office.Interop.Excel.Worksheet wksheet;
Microsoft.Office.Interop.Excel.Workbook wkbook;
app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;
wkbook = app.Workbooks.Add(true);
wksheet = (Microsoft.Office.Interop.Excel.Worksheet)wkbook.ActiveSheet;
string[] ddl_itemReq = { "R", "S" };
string[] ddl_item = { "Answers", "Autos", "Finance" };
Microsoft.Office.Interop.Excel.Range xlsRange;
int lastRow = wksheet.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, Type.Missing).Row;
Microsoft.Office.Interop.Excel.Range last = wksheet.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
xlsRange = wksheet.get_Range("A2", "A2");

try
{
for (int i = 0; i < dtExcelData.Columns.Count; i++)
{
wksheet.Cells[1, i + 1] = dtExcelData.Columns[i].ColumnName;
}

foreach (DataRow row in dtExcelData.Rows)
{
rowindex++;
columnindex = 0;
foreach (DataColumn col in dtExcelData.Columns)
{
columnindex++;
wksheet.Cells[rowindex + 1, columnindex] = row[col.ColumnName];
}
}
Microsoft.Office.Interop.Excel.DropDowns xlDropDowns;
Microsoft.Office.Interop.Excel.DropDown xlDropDown;
xlDropDowns = ((Microsoft.Office.Interop.Excel.DropDowns)(wksheet.DropDowns(oMissing)));
xlDropDown = xlDropDowns.Add((double)xlsRange.Left, (double)xlsRange.Top, (double)xlsRange.Width, (double)xlsRange.Height, true);

for (int i = 2; i < 4; i++)
{
Microsoft.Office.Interop.Excel.DropDown xlDrop1;
xlsRange = wksheet.get_Range("B"+i, "B"+i);
xlDropDown = xlDropDowns.Add((double)xlsRange.Left, (double)xlsRange.Top, (double)xlsRange.Width, (double)xlsRange.Height, true);

for (int j = 0; j < ddl_item.Count(); j++)
{
var getIng = ddl_item[j];
xlDropDown.AddItem(getIng, j + 1);
}

xlsRange = wksheet.get_Range("C" + i, "C" + i);
xlDropDown = xlDropDowns.Add((double)xlsRange.Left, (double)xlsRange.Top, (double)xlsRange.Width, (double)xlsRange.Height, true);

for (int j = 0; j < ddl_itemReq.Count(); j++)
{
xlDropDown.AddItem(ddl_itemReq[j], j + 1);
}
}
app.Visible = true;
}
catch (Exception ex1)
{
retError = ex1.Message;
}
app.UserControl = true;
}
catch (Exception ex1) { retError = ex1.Message; }
return retError;
}

Need an urgent help.

Thank you.
Posted

1 solution

Error 80040154 means Office is not installed on the server.

You should also read the following Microsoft knowledgebase article:

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.


There are various ways to create Excel spreadsheets on the server without using Office interop. For example:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900