0
votes

I am writing web application using ASP.net (C#). I have one excel file on server which I want to read. When I try to read this excel file using below code then it throws exception. I have provided full rights to user (IIS User) on server for particular folder where excel file kept.

Reading Logic:

Excel.Application xlApp = null;
Excel.Workbook xlWorkbook = null;
Excel._Worksheet xlWorksheet = null;
Excel.Range range = null;

xlApp = new Excel.Application();
xlWorkbook = xlApp.Workbooks.Open("C:\inetpub\wwwroot\Uploads\data.xls);
xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(sheetName);
range = xlWorksheet.get_Range("A1", "B5");

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)).

Can you please help me to understand this error?

1

1 Answers

2
votes

Office automation is not supported from inside IIS or any other situation where the code is running outside of a user session (for example a windows service). In the linked knowledge base you can see you are getting "Message 4" in the "Problems using server-side Automation of Office" section.

If it is possible to switch from .xls to .xlsx files you can use Microsoft's Open XML SDK 2.5 for Office to open and manipulate office 2007 and newer files.

If you must use the legacy file format of .xls you will need to find a 3rd party library that can support it.