views:

2020

answers:

4

Background

We are developing some in-house utilities using ASP.NET 2.0. One of which is extracting some information from databases and building an Excel workbook containing a number of spreadsheets with data based on queries into the database.

Problem

The proof-of-concept prototype (a simple ASP.NET page that queries a single item from the database and opens Excel to add data to a worksheet) is working well when run locally on the development machines, happily creating and displaying an Excel spreadsheet as requested. However, when run on our server, we get the following error upon trying to instantiate Excel .

Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Solution?

We are using the PIA for Excel 2003 and we have Excel 2003 and the PIA installed on the server. Can anyone explain why this isn't working or give us some tips on how we might track the problem down?

Thanks for any assistance you can provide.

+1  A: 

Can the user that the ASP.NET Application Pool runs under have access to the application? Try logging in as that user (or change the Application Pool to run as that user) and opening Excel. If that works try running a WinForms application on the server as that user with the code that is failing.

Not sure but I think the PIA assemblies might need to be registered via regsvr32.

I suspect that if you run as Network Service, you will not be able to start Excel (no interactive login, restricted account, etc). Your ASP.NET code runs inside the application pool. You can change the user the application pool runs as through the IIS manager. If you want to check what your code is currently running as look for the w3wp process in Task Manager.

For testing, change the Application Pool to run as the user you know works with Excel.

Robert Wagner
I haven't been able to try this yet, but if it happens that the user is NOT able to run Excel, is there a way for the ASP page to access Excel as someone who can run it, even if the user accessing the page cannot?
Jeff Yates
Run the Application Pool as the user who can run Excel. Your .Net code will run with the credentials of the user defined in your application pool. I suspect if you are running as Network Service you may not be able to start Excel.
Robert Wagner
Ah, I get it. I'll try this tomorrow. Thanks!
Jeff Yates
It seems that permissions were indeed causing our main issues. Thanks.
Jeff Yates
+1  A: 

We use Aspose (commercial). Office on a server is not much fun.

  • You have to be careful about the licencing.
  • Once in a while you need to kill a hanging process.
  • Getting the rights right takes some effort.

It is called PI(t)A for a reason...

Stephan Eggermont
+1  A: 

Consider working with XLSX files (new in Office 2007, but there is a plugin for Office 2003), which are just ZIP files containing XML files which you can manipulate without the need for Excel. The (XML-based) SpreadsheetML is well documented and not too complicated to program against (you might even find a LINQ to SpreadsheetML somewhere on the web).

As was pointed out above, Excel is not really a server product and you might run into all kinds of issues when using it on a server.

Philipp Schmid
I have been thinking along these lines myself, but there are some constraints on the project that may not allow for this approach unfortunately.
Jeff Yates
+1  A: 

I think the problem is that once you deploy your application to IIS you are suddenly running inside an MTA COM Apartment. I believe that Excel is an STA component and therefore cannot be created inside the MTA. You will need to set the aspcompat option in the page you are using

<%@ page aspcompat=true %>

More Info Here

JaredPar