我需要修复此错误

时间:2017-10-26 03:06:10

标签: c# asp.net excel iis

我的应用程序在asp.net(4.5)webform C#中运行。在我的应用程序中,需要创建excel表。

// code to create excel
xlsWorkbook.SaveAs("C:\\" + filename, XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlsWorkbook.Close(true, filename, null);
xlsApp.Quit();
xlsWorksheet = null;
xlsWorkbook = null;
xlsApp = null;
Process.Start(@"c:\"); // Open file after download

如您所见,我在C盘中保存生成的Excel文件。我在本地运行时工作得很好。但是在发布之后,这个错误正在抛出

**

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)). 
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. 
To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.
Source Error: 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace: 
[UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +119
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +247
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
   System.Activator.CreateInstance(Type type) +11
   e2aPortal.homeUserControl.CreateTopic.ExportToExcel() +112
   e2aPortal.homeUserControl.CreateTopic.btnDownloadExcel_OnClick(Object sender, EventArgs e) +5
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9767618
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1738

**

如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

正如消息所说这是一个许可问题。您的应用程序在其安装的计算机上以Windows用户身份运行。你知道它运行的用户是什么? 您可以尝试以下方法之一: (1)授予该Windows用户写入c:驱动器根目录(或理想情况下是特定子文件夹)的权限 (2)在具有更高权限的另一个用户下运行您的应用程序 (3)在应用程序配置文件中使用一个设置,指定应在何处创建文件。 (4)写入用户的默认文件夹(C:\ Users \ username),我认为这是通过指定文件名而不将c:\放在它前面来完成的

xlsWorkbook.SaveAs(filename, XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

答案 1 :(得分:0)

对于Web应用程序,使用Excel dll不是一个好主意。 您需要配置DCOM设置,允许w3wp访问它,这可能会导致安全问题。

我注意到您的需求是创建excel文件以供下载。 您可以尝试NPOI NPOI

此项目是http://poi.apache.org/的POI Java项目的.NET版本。 POI是一个开源项目,可以帮助您读/写xls,doc,ppt文件。它有广泛的应用。