Question

I have a ASP.NET application that allows users to export their staff's absences each month to Microsoft Excel. The application is currently generating the following exception

Exception: Cannot create ActiveX component.

with the following stack trace

System.Exception: Cannot create ActiveX component. at Microsoft.VisualBasic.Interaction.CreateObject(String ProgId, String ServerName) at HR.ManagerSummary.ExportToExcel() in H:\Development\pagec\Visual Studio 2005\Projects\HR\ysnet2\Time\ManagerSummary.aspx.vb:line 935 at HR.ManagerSummary.btnExcel_Click(Object sender, EventArgs e) in H:\Development\pagec\Visual Studio 2005\Projects\HR\ysnet2\Time\ManagerSummary.aspx.vb:line 891 at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

The code fails at the CreateObject line.

    'Create the Excel object
    Dim objXL As Object = CreateObject("Excel.Application") ' New Microsoft.Office.Interop.Excel.Application
    Dim objWB As Object = objXL.Workbooks.Add
    Dim objWS As Object = objWB.Worksheets(1)

I've set permission in DCOM Config for Microsoft Excel Application for the IUSR_ account but that has not resolved the issue. Any ideas would greatly be appreciated.

Cheers

James

Was it helpful?

Solution

Following migration of our ASP.NET applications to new hardware, I investigated this issue further and came up with a solution.

To resolve the issue, open "Component Services" from Administrative Tools and navigate as follows:

  • Component Services -> Computers -> My Computer -> DCOM Config -> Microsoft Excel Application
  • Right click Microsoft Excel Application -> Properties -> Security tab
  • Click Customize for "Launch and Activation Permissions" -> Edit -> Add
  • Enter "NETWORK SERVICE", click "Check Names", click OK
  • Allow "Local Launch" and "Local Activation" for "NETWORK SERVICE" then click OK
  • Select Customize for "Access Permissions" -> Edit -> Add
  • Enter "NETWORK SERVICE", click "Check Names", click OK
  • Allow "Local Access" for for "NETWORK SERVICE" then click OK
  • Click OK

The ASP.NET application can now successfully create a Microsoft Excel worksheet containing the exported data.

OTHER TIPS

Did you registered the ActiveX on the server?

http://support.microsoft.com/kb/146219

SpreadsheetGear for .NET will let you save xls and xlsx workbooks directly to a response stream with IWorkbook.SaveToStream. It is all safe managed C# code so there is no need to run anything as admin and you will not run into the problems that Excel COM Interop bring on an ASP.NET server.

You can see some simple ASP.NET Excel Reporting samples (C# and VB) using SpreadsheetGear here and download the free trial here if you want to try it yourself.

Disclaimer: I own SpreadsheetGear LLC

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top