不访问 IIS 中的 xls 文件

Doesn't access xls files in IIS

我正在尝试打开 xls 文件并使用网络 api 2 服务将其转换为 pdf。 当我在 vs 2013 中启动服务项目时,一切正常,但是当我将服务发布到 iis 8.0 并尝试打开 excel 文件时,出现错误:

错误:

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)


此行错误:

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

我尝试解决的问题:

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

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 是一个 COM Interop 库,它允许您与安装在用户机器上的 Excel 运行 Windows 客户端进行交互。

Microsoft 不支持 Windows 服务器 上的 Excel,如其著名的 KB257757 - Considerations for server-side Automation of Office.

所述

要读取服务器上的 Excel 文件,您应该使用可以直接 read/write Excel 文件的工具,不需要 Excel 安装在服务器上。

如果您正在阅读 Excel 2017 年以上的文件(即 .xlsx),您可以使用 Open XML SDK, or any other OpenXml-compatible library such as ExcelDataReader or ClosedXML.

如果您需要读取旧的 Excel 文件(即 .xls),您可以使用 ExcelDataReader, NPOI 或可以读取旧的 Excel 二进制文件格式的类似库.