Editing an Excel document with Macros in ASP.net

Is it possible in any way to edit an excel sheet through an ASP.net page that contains macro. I have tried to open the Excel sheet and it seems to just hang rather than load the excel. Testing on a page without macros works perfectly fine?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Disclaimer: I don’t know the Excel license agreement and I don’t know if utilizing Excel in a server process violates it or not. This is purely a technical description of how to get it working. The reader is advised to check the license agreement to see if it’s allowed to do so or not. Different Office versions may have different license agreements. I used this method at several Fortune 100/500 companies and they didn’t seem to care. Go figure.

This solution works but it has some limitations and require a fair amount of control over the server where it runs. The server also needs to have lots of memory.

To start, make sure that you perform a complete installation of every single Office feature on the server so that Excel won’t try to install something if you attempt to use a feature that’s not present.

You also need to create a dedicated user account on the server that has the right privileges. I can’t tell you what exactly because in my case we controlled the server and we gave admin rights to this user.

When you have the user account, you need to log in as that user and run Excel (preferably all Office applications) at least once so that it can create its settings.

You also need to configure Excel to run under this user account when it’s created as a COM object. For this, you need to go into DCOM Config on the server and configure Launch and Activation Permissions for the Excel.Application object to use your new user account. I’m not sure if I remember correctly, but I think after this step, running Excel as an interactive user was slightly problematic.

By default, Office applications try to display various messages on the screen: warnings, questions, etc. These must be turned off because when you utilize an Office application from a web application, it runs on the server so a human user won’t be around to dismiss these messages – the Office program will just sit around indefinitely, waiting for the message to be dismissed.

You need to set (at the minimum) these properties:

DisplayAlerts = false
AskToUpdateLinks = false
AlertBeforeOverwriting = false
Interactive = false
Visible = false
FeatureInstall = 0 'msoFeatureInstallNone

to disable UI messages from Excel. If you use Excel 2010, there may be more, but I’m not familiar with that.

If you have Excel files with macros in them, you may have to disable macro security in Excel – that can’t be done programmatically, for obvious reasons.

To access Excel services, implement a manager object that will actually hold the Excel reference – don’t try to hold the Excel.Application object in the page because your page code will become very complicated and you may not be able to properly clean things up.

The object that holds the Excel reference may be a separate DLL or an out-of-process server. You must make sure, however, that when you acquire an instance of Excel on a given thread you always create a new Excel instance. The default is that an already running Excel instance will also serve other requests but that won’t work for you because the same Excel instance cannot be shared among multiple threads. Each request-processing thread in IIS must have its own Excel instance – if you share instances, you’ll have all kinds of problems. This means that your server will need to have quite a bit of memory to have many instances of Excel running. This was not an issue for me becasue we controlled the server.

If you can, try to create an out-of-proc (.exe) COM server because this way you can hold the Excel reference in a separate process. It’s possible to get it working using an in-proc (.dll) COM object but it’ll be more risky to your application pool – if Excel crashes, it’ll crash your app pool as well.

When you have an .exe server, you can pass parameters in several possible ways:

  1. Make your manager objet a COM object and pass parameters as properties.
  2. Pass parameters as command-line parameteres to the .exe as it starts up.
  3. Pass parameters in a text/binary file; pass the name of the file on the command-line.

I used all these and found the COM object option the cleanest.

In your manager object, follow these guidelines:

  • Wrap every single function that uses Excel in a try..catch block to capture any possible exception.
  • Always explicitly release all Excel objects by calling Marshal.ReleaseComObject() and then setting your variables to null as soon as you don’t need them. Always release these objects in a finally block to make sure that a failed Excel method call won’t result in a dangling COM object.
  • If you try to use any formatting features in Excel (page header, margins, etc.) you must have a printer installed and accessible to the user account that you use to run Excel. If you don’t have an active printer (preferably attached to the server), formatting-related features may not work.
  • When an error happens, close the Excel instance that you’re using. It’s not likely that you can recover from Excel-related errors and the longer you keep the instance, the longer it uses resources.
  • When you quit Excel, make sure that you guard that code against recursive calls – if your exception handlers try to shut down Excel while your code is already in the process of shutting down Excel, you’ll end up with a dead Excel instance.
  • Call GC.Collect() and GC.WaitForPendingFinalizers() right after calling the Application.Quit() method to make sure that the .NET Framework releases all Excel COM objects immediately.

Edit: John Saunders may have a point regarding the license agreement – I can’t advise about that. The projects that I did using Word/Excel were all intranet applications at large clients and the requirement to use Word/Excel was a given.

The link he provided also has some tools that may be useful, although those libraries won’t have full Excel functionality and if that’s what you need, you don’t have a choice. If you don’t need full Excel functionality, check out those libraries – they may be much simpler to use.

A few links that may be useful to people trying to implement this approach:

Method 2

The story changed a little while ago, with HPC Services for Excel.

With that, you can do Office Automation on a web server. I’m still trying to determine how it fits my situation, but you may want to check it out.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x