ClosedXML SecurityException:不允许请求的注册表访问

时间:2017-04-19 00:41:28

标签: c# asp.net closedxml

我使用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

2 个答案:

答案 0 :(得分:6)

我通过删除身份模仿来解决了这个问题=" true"来自Web.Config文件。

答案 1 :(得分:1)

对于以后遇到此问题的人员:我浏览了.NET源引用,它需要访问的注册表项是HKEY_CURRENT_USER \ Software \ Microsoft \ Avalon.Graphics。授予我对特定密钥的“所有人”的读取访问权限不会引起我的想到,并且可以解决此问题。