.NET/C#- EPPLUS DEMO

时间:2021-07-22 08:18:06

  强大的导出EXCEL,比NPOI更好用,更强大,可惜只有4.0版本的。

  记录一下DEMO

                var sheet = p.Workbook.Worksheets.Add("My Sheet");

                //Cells的起始索引是1
sheet.Cells[, ].Value = 1234.123;
sheet.Cells[, ].Value = ;
sheet.Cells[, ].Value = ;
sheet.Cells[, ].Value = ; sheet.Cells[, ].Style.Numberformat.Format = "#,##0.00";//这是保留两位小数 var sheet2 = p.Workbook.Worksheets.Add("My Sheet2");
sheet2.Cells[, ].Value = "jie";
sheet2.Cells[, ].Value = "xiaom";
sheet2.Cells[, ].Value = "ccx";
sheet2.Cells[, ].Value = "zhangs"; sheet2.Cells[, ].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;//水平居中
sheet2.Cells[, ].Style.Font.Bold = true;//字体为粗体
sheet2.Cells[, ].Style.Font.Size = ;//字体大小 p.SaveAs(new FileInfo(@"F:\Temp\output.xlsx"));

-------------------------------------------------

        public ActionResult ExportExcel()
{
// 写入到客户端
System.IO.MemoryStream ms = new System.IO.MemoryStream(); using (var p = new ExcelPackage(ms))
{
var sheet = p.Workbook.Worksheets.Add("My Sheet"); //Cells的起始索引是1
sheet.Cells[, ].Value = 1234.123;
sheet.Cells[, ].Value = ;
sheet.Cells[, ].Value = ;
sheet.Cells[, ].Value = ; sheet.Cells[, ].Style.Numberformat.Format = "#,##0.00";//这是保留两位小数 var sheet2 = p.Workbook.Worksheets.Add("My Sheet2");
sheet2.Cells[, ].Value = "jie";
sheet2.Cells[, ].Value = "xiaom";
sheet2.Cells[, ].Value = "ccx";
sheet2.Cells[, ].Value = "zhangs"; sheet2.Cells[, ].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;//水平居中
sheet2.Cells[, ].Style.Font.Bold = true;//字体为粗体
sheet2.Cells[, ].Style.Font.Size = ;//字体大小 //写到客户端(下载)
HttpContext.Response.Clear();
HttpContext.Response.AddHeader("content-disposition", "attachment; filename=FileFlow.xlsx");
HttpContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Response.BinaryWrite(p.GetAsByteArray());
//ep.SaveAs(Response.OutputStream); 第二种方式
HttpContext.Response.Flush();
HttpContext.Response.End(); } return null;
}