Writing an Excel file in EPPlus

前端 未结 3 1746
青春惊慌失措
青春惊慌失措 2021-01-31 17:45

I have been stuck on this for days and despite all of the help out there, none of these solutions have been working for me. What I want to do is create an excel file using the E

3条回答
  •  梦谈多话
    2021-01-31 18:14

    It's best if you worked with DataSets and/or DataTables. Once you have that, ideally straight from your stored procedure with proper column names for headers, you can use the following method:

    ws.Cells.LoadFromDataTable(, true, OfficeOpenXml.Table.TableStyles.Light8);

    .. which will produce a beautiful excelsheet with a nice table!

    Now to serve your file, assuming you have an ExcelPackage object as in your code above called pck..

    Response.Clear();
    
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("Content-Disposition", "attachment;filename=" + sFilename);
    
    Response.BinaryWrite(pck.GetAsByteArray());
    Response.End();
    

提交回复
热议问题