我正在使用ClosedXML导出Excel文件,但似乎无法导出Excel文件。每次单击导出Excel文件的按钮(XLSX)时,都会出现错误。请参阅下面的...
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dsInput);
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=" + sFileName + ".xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream, false);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
字符串
我得到这个错误:SecurityException:不允许请求的注册表访问。
Exception thrown: 'System.TypeInitializationException' in WindowsBase.dll
System.TypeInitializationException: The type initializer for
'MS.Utility.EventTrace' threw an exception. --->
System.Security.SecurityException: Requested registry access is not allowed.
at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name)
at Microsoft.Win32.Registry.GetValue(String keyName, String valueName,
Object defaultValue)
at MS.Utility.EventTrace.IsClassicETWRegistryEnabled()
at MS.Utility.EventTrace..cctor()
--- End of inner exception stack trace ---
at MS.Utility.EventTrace.EasyTraceEvent(Keyword keywords, Event eventID)
at System.IO.Packaging.Package.Open(Stream stream, FileMode packageMode,
FileAccess packageAccess, Boolean streaming)
at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.CreateCore(Stream stream)
at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Create(Stream
stream, SpreadsheetDocumentType type, Boolean autoSave)
at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Create(Stream
stream, SpreadsheetDocumentType type)
at ClosedXML.Excel.XLWorkbook.CreatePackage(Stream stream, Boolean
newStream, SpreadsheetDocumentType spreadsheetDocumentType, Boolean
validate) in C:\Git\ClosedXML\ClosedXML\Excel\XLWorkbook_Save.cs:line 111
at ClosedXML.Excel.XLWorkbook.SaveAs(Stream stream, Boolean validate) in
C:\Git\ClosedXML\ClosedXML\Excel\XLWorkbook.cs:line 547
at ExcelHelper.ToExcel(DataSet dsInput, String sFileName, HttpResponse
Response) in c:\inetpub\wwwroot\Felbro_B\App_Code\ExcelHelper.cs:line 139
型
5条答案
按热度按时间u7up0aaq1#
我通过从Web.config文件中删除identity impersonate=“true”解决了这个问题。
vc6uscn92#
对于将来遇到这种情况的人:我查看了.NET源代码引用,它需要访问的注册表项是HKEY_CURRENT_USER\Software\Microsoft\Avalon. Graphics。授予“Everyone”对该特定项的读取权限没有我能想到的安全问题,并解决了问题。
polkgigr3#
这通常是由IIS匿名身份验证引起的,如果您的iis webApplication Settingsenabledaspnet:AllowAnonymous Impersonation和IIS身份验证enabledAnonymous Authenticaion & Asp.net Impersonate,您的web应用将通过IIS中指定的用户访问HKEY_CURRENT_USER\Software\Microsoft\Avalon.Graphics*Authentications -> Anonymous Authentication -> Specific User,您应该确保此用户(IUSR)具有对注册表项的读取权限。您也可以禁用aspnet:AllowAncestiousImpersonation或禁用ASP.NET Impersonate。
但特定的注册表项并不总是存在,如果它不存在,似乎也不会发生问题。
bvpmtnay4#
它不是:
字符串
这是
型
.然后给予执行帐户的READ权限,该权限是在报表服务器配置管理器中设置的。
0s7z1bwu5#
在将我们的Web服务器从运行IIS 8.5的Windows Server 2012 R2迁移到运行IIS 10.0的2019后,我遇到了此问题。使用
OpenXML
SDK
打开Excel文件(.xlsx)导致错误,但仅在新服务器上。我使用SysInternals进程监视器确认问题是 * 注册表项**上的访问拒绝错误HKEY_USERS\S-1-5-20\SOFTWARE\Microsoft\Avalon. Graphics.
该
SID
是内置的NETWORK SERVICE帐户,我们一直使用它来支持模拟和向后端SQL Server
的代理(14年)。我想知道我是否错过了IIS中应用程序池的更改,所以我搜索了一下,找到了 * https://learn.microsoft.com/en-us/iis/manage/configuring-security/application-pool-identities *,它描述了IIS
7.0中添加的ApplicationPoolIdentity
类型(显然,我没有跟上)。更改每个应用程序池以使用该类型解决了这个问题。不需要更改注册表权限。