Export to Excel in ASP.Net Core 2.0

前端 未结 3 687
既然无缘
既然无缘 2021-02-07 23:13

I used to export data to excel in asp.net mvc using below code

    Response.AppendHeader(\"content-disposition\", \"attachment;filename=ExportedHtml.xls\");
            


        
3条回答
  •  渐次进展
    2021-02-08 00:10

    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).

提交回复
热议问题