I am using ClosedXML to export an Excel file and I can't seem to export the Excel file. Every time I click on the Button to Export the Excel File(XLSX) I get an error. See Below...
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();
}
}
I am getting this error: SecurityException: Requested registry access is not allowed.
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
I solved the issue by removing identity impersonate="true" from the Web.Config file.
For future people running into this: I looked through the .NET source reference, and the registry key that it needs access to is HKEY_CURRENT_USER\Software\Microsoft\Avalon.Graphics. Granting "Everyone" read access on that specific key has no security implications that I can think of, and solves the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With