Search code examples
c#asp.netexcelpermissionsnetoffice

Doesn't access xls files in IIS


i am trying open xls file and convert to pdf using web api 2 service. When i start service project in vs 2013 everyting works fine, but when i publish the service to iis 8.0 and try to open excel file i got error:

Error:

Exception has been thrown by the target of an invocation. at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at NetOffice.Invoker.MethodReturn(COMObject comObject, String name, Object[] paramsArray)


Error on this line:

var workbook = excelApplication.Workbooks.Open(excelLocalOutput, 0, false, 5, 123, 123, true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

What i tryied to solve this:

i gave "Network Service" and "IUSRS" permission to Microsoft Excel Application

i gave "Network Service" to application pool

i gave "Network Service" to published folder


Solution

  • TL;DR; NetOffice is the wrong library to do what you want. It is not meant to be used on a server, and you should not have Excel installed on the server.


    NetOffice is a COM Interop library that allows you to interact with the Excel installed on a user's machine running a Windows Client.

    Microsoft does not support Excel on Windows Server as described its famous KB257757 - Considerations for server-side Automation of Office.

    To read Excel files on a server you should use tools that can read/write Excel files directly, without requiring Excel installed on the server.

    If you are reading Excel 2017+ files (i.e. .xlsx), you could use the Open XML SDK, or any other OpenXml-compatible library such as ExcelDataReader or ClosedXML.

    If you need to read older Excel files (i.e. .xls), you could use ExcelDataReader, NPOI or similar library that can read the old Excel binary file format.