This project has moved and is read-only. For the latest updates, please go here.

Open EPPlus-generated excel on-screen without file io permissions

Mar 17, 2016 at 12:36 AM
I am trying to create an excel doc from a datatable and open it in excel for a user. Done in a webpage on a button click and I pass in a pre-populated datatable. The below code works excellent on my local machine, and in my company's development environment (which is full trust): when button clicked, I get a save or open dialog, which is what I want. I choose Open, and it opens in excel. The problem is it gives this error when I use it in our production environment (which is medium trust): System.Security.SecurityException Exception Message: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

I am not trying to create a file (an in fact cannot due to medium trust environment I am restricted to), just open the created excel on-screen in excel so they can view it and choose to save-as if they wish. Is there a way to do this which will work in a medium trust environment? I do not have access to alter any permissions /settings on the web server so cannot give FileIOPermission to get this to work.

Here is the code:

ExcelPackage p = new ExcelPackage();
ExcelWorksheet workSheet = p.Workbook.Worksheets[1];
workSheet.Cells[1,1].LoadFromDataTable(dt, true);
var range = workSheet.Cells[1,1,dt.Rows.Count,dt.Columns.Count];
var table = workSheet.Tables.Add(range, "results");
table.ShowTotal = false;
table.TableStyle = TableStyles.Medium4;
workSheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
workSheet.View.ShowGridLines = false;
//Also tried this instead of above line with same error: p.SaveAs(Response.OutputStream);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".xlsx");