I used to export data to excel in asp.net mvc using below code
Response.AppendHeader(\"content-disposition\", \"attachment;filename=ExportedHtml.xls\");
Here is our solution to this:
using OfficeOpenXml;
public class XmlService
{
// [...]
public void getXlsxFile(SomeTableObject tbl, ref byte[] bytes)
{
using (ExcelPackage pck = new ExcelPackage())
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(tbl.name);
ws.Cells["A1"].LoadFromDataTable(tbl, true);
bytes = pck.GetAsByteArray();
}
}
}
More information on EPPlus is available here and the source code above can be found at our open source repo (GPL).