Microsoft.Excel.Application - Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)
Hi,Unauthorized Access is occurred while accessing COM object (Microsoft.Excel.Application) from web server in local development environment (Windows 7 Prof, IIS 7).
C# code
private void GenerateExcelFromDataTable(DataTable dataTable, string fileName)
{
Microsoft.Office.Interop.Excel.Application excel = null; // Load Excel application
Microsoft.Office.Interop.Excel._Worksheet workSheet = null;
Microsoft.Office.Interop.Excel.Range usedrange = null;
try
{
excel = new Microsoft.Office.Interop.Excel.Application(); // --- EXCEPTION COMES HERE
excel.Workbooks.Add();
workSheet = excel.ActiveSheet;
usedrange = workSheet.UsedRange;
workSheet.Cells[1, "A"] = "ID"; workSheet.Cells[1, "A"].Font.Bold = true;
workSheet.Cells[1, "B"] = "Employee Name"; workSheet.Cells[1, "B"].Font.Bold = true;
workSheet.Cells[1, "C"] = "City"; workSheet.Cells[1, "C"].Font.Bold = true;
workSheet.Cells[1, "D"] = "Payroll"; workSheet.Cells[1, "D"].Font.Bold = true;
workSheet.Cells[1, "E"] = "Type"; workSheet.Cells[1, "E"].Font.Bold = true;
workSheet.Cells[1, "F"] = "Account"; workSheet.Cells[1, "F"].Font.Bold = true;
workSheet.Cells[1, "G"] = "Month"; workSheet.Cells[1, "G"].Font.Bold = true;
workSheet.Cells[1, "H"] = "Manager"; workSheet.Cells[1, "H"].Font.Bold = true;
for (int i = 0; i < dataTable.Rows.Count; i++)
{
for (int j = 0; j < dataTable.Columns.Count; j++)
{
workSheet.Cells[(i + 2), (j + 1)] = dataTable.Rows[i][j];
}
}
string FilePath = ConfigurationManager.AppSettings["AwardFilesFolder"] + fileName;
FileInfo fi = new FileInfo(FilePath);
if (fi.Exists)
{
fi.Delete();
}
workSheet.SaveAs(FilePath);
}
catch (Exception e)
{
}
finally
{
excel.Quit();
if (excel != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
if (workSheet != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
excel = null;
workSheet = null;
GC.Collect();
}
}
Exception: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
Tried all the ways as per suggestions received in various forums.
Most of the forums suggested following "Microsoft.Excel.Application > security settings" in DCOM settings:
1) Run -> dcomcnfg
2) Component Services -> Computers -> My Computer -> DCOM Config
3) Microsoft Excel Application -> Properties -> Security tab
4) In both the sections "Launch & Activation Permissions" ... select Customize and click Edit
5) Add the IUSRS and give "Local Launch & Local Activation permissions" - Apply
But I am getting the same exception.
I would appreciate any help.
Thanks.