简体   繁体   English

在服务器上运行Excel-C#Interop Excel

[英]Run Excel on Server - C# Interop Excel

we are looking to perform following operations (in order) on an Excel template file through C# .NET. 我们希望通过C#.NET对Excel模板文件执行以下操作(按顺序)。

  1. Open the Excel template 打开Excel模板
  2. Read data from SQL views and write into a sheet in the Excel template 从SQL视图中读取数据并写入Excel模板中的工作表
  3. Set named range for the data copied in 为复制的数据设置命名范围
  4. Refresh Pivot table which is based on the named range 刷新基于命名范围的数据透视表
  5. Password protect and return the file as a downloadable. 密码保护并以可下载的形式返回文件。

Given below are the options considered: 1. Microsoft.Office.Interop.Excel – The driver achieves all the functionalities mentioned above, however, the driver requires Excel client tool to be installed on the server which is not the preferred option as we cannot request for Excel to be installed on all the environments. 下面是考虑的选项:1. Microsoft.Office.Interop.Excel –驱动程序实现了上述所有功能,但是,该驱动程序要求在服务器上安装Excel客户端工具,这不是首选选项,因为我们无法请求将Excel安装在所有环境中。 2. Third party offerings - A free driver called Excel Package Plus seemed to fit almost all our requirements. 2.第三方产品-名为Excel Package Plus的免费驱动程序似乎符合我们的几乎所有要求。 We were able to pretty much do everything in the above list of operations except refreshing the pivot table. 除了刷新数据透视表,我们几乎可以完成上述操作列表中的所有操作。 3. Writing a macro on the existing excel file to refresh the pivot table - The refresh will occur when the file is opened by the user causing a delay and also, the requirement is to download the file with the pivot table refreshed . 3.在现有的excel文件上编写宏以刷新数据透视表-当用户打开文件导致延迟时,刷新将发生,并且要求在刷新数据透视表的情况下下载文件。

So, clearly with all the above options, we have hit a roadblock. 因此,显然,使用上述所有选项,我们都遇到了障碍。 I was wondering if it is possible to place the excel template in one server that has excel installed and access it from the web server. 我想知道是否可以将excel模板放置在已安装excel的一台服务器中,然后从Web服务器访问它。 This way I can use Interop.Excel without having to install the driver on my web server. 这样,我可以使用Interop.Excel,而不必在Web服务器上安装驱动程序。 I'm not really sure if this will work as expected. 我不确定这是否会按预期进行。

Do not use Interop on a server. 不要在服务器上使用Interop。 I have done it, and it is a nightmare - you will have frozes processes, memory leaks and dog slow performance. 我已经做到了,这是一场噩梦-您将遇到僵局,内存泄漏和性能下降。

What I would recommend is use EPPLUS for all the stuff where you can use it, and use the Open XML SDK to open the excel file and set the named ranges. 我建议对可以使用它的所有东西都使用EPPLUS,并使用Open XML SDK打开excel文件并设置命名范围。

Open XML SDK is the low level API to edit Offixe XML documents, including Excel. Open XML SDK是用于编辑Offixe XML文档(包括Excel)的低级API。 You can do all the things you can do in Office with it. 您可以使用它来完成在Office中可以做的所有事情。 It is rather complicated so I would recommend using epplus whereever you can, since that avoids many of the pitfalls of manual excel editing. 它非常复杂,因此我建议您尽可能使用epplus,因为这样可以避免许多手动excel编辑的陷阱。

I remember a tool who could build you the C# code to build the document you pass into it - so you could pass in an excel doc with the named range and see how the tool builds the named range in code. 我记得有一个工具可以为您构建C#代码,以构建传递给它的文档-因此您可以传递带有命名范围的excel文档,并查看该工具如何在代码中构建命名范围。 I think the tool was included in the sdk 我认为该工具已包含在SDK中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM