【问题标题】:I need to fix this error我需要修复这个错误
【发布时间】:2017-10-26 12:01:22
【问题描述】:

我的应用程序在 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

如您所见,我将生成的 excel 文件保存在 C 盘中。当我在本地运行时,这可以完美地工作。但是发布后,这个错误正在抛出

**

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

**

如何解决这个问题?

【问题讨论】:

  • 您是否检查过生产机器的 IIS 中的文件/文件夹权限?按照类似问题中的建议尝试使用LocalSystemstackoverflow.com/questions/17785063/…
  • 应用程序池在无法访问服务器上所有内容的帐户下运行。这是出于安全原因和充分的理由。如果您希望用户能够访问这些文件,那么您需要授予应用程序池帐户访问文件位置的权限。如果是 Intranet 并且您正在使用窗口身份验证,则使用模拟,以便您知道谁对文件做了什么。

标签: c# asp.net excel iis


【解决方案1】:

正如消息所说,这是一个权限问题。您的应用程序在安装它的机器上以 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);

【讨论】:

    【解决方案2】:

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

    我注意到您的需求是创建用于下载的 excel 文件。 你可以试试NPOINPOI

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-09
      • 2011-04-23
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      相关资源
      最近更新 更多