简体   繁体   中英

Run Excel on Server - C# Interop Excel

we are looking to perform following operations (in order) on an Excel template file through C# .NET.

  1. Open the Excel template
  2. Read data from SQL views and write into a sheet in the Excel template
  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. 2. Third party offerings - A free driver called Excel Package Plus seemed to fit almost all our requirements. 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 .

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. This way I can use Interop.Excel without having to install the driver on my web server. I'm not really sure if this will work as expected.

Do not use Interop on a server. 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.

Open XML SDK is the low level API to edit Offixe XML documents, including Excel. You can do all the things you can do in Office with it. It is rather complicated so I would recommend using epplus whereever you can, since that avoids many of the pitfalls of manual excel editing.

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. I think the tool was included in the sdk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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