強大的導出EXCEL,比NPOI更好用,更強大,惋惜只有4.0版本的。app
記錄一下DEMO字體
var sheet = p.Workbook.Worksheets.Add("My Sheet"); //Cells的起始索引是1 sheet.Cells[1, 1].Value = 1234.123; sheet.Cells[2, 1].Value = 1; sheet.Cells[3, 1].Value = 2; sheet.Cells[4, 1].Value = 3; sheet.Cells[1, 1].Style.Numberformat.Format = "#,##0.00";//這是保留兩位小數 var sheet2 = p.Workbook.Worksheets.Add("My Sheet2"); sheet2.Cells[1, 1].Value = "jie"; sheet2.Cells[2, 1].Value = "xiaom"; sheet2.Cells[3, 1].Value = "ccx"; sheet2.Cells[4, 1].Value = "zhangs"; sheet2.Cells[1, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;//水平居中 sheet2.Cells[1, 1].Style.Font.Bold = true;//字體爲粗體 sheet2.Cells[1, 1].Style.Font.Size = 22;//字體大小 p.SaveAs(new FileInfo(@"F:\Temp\output.xlsx"));
-------------------------------------------------spa
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[1, 1].Value = 1234.123; sheet.Cells[2, 1].Value = 1; sheet.Cells[3, 1].Value = 2; sheet.Cells[4, 1].Value = 3; sheet.Cells[1, 1].Style.Numberformat.Format = "#,##0.00";//這是保留兩位小數 var sheet2 = p.Workbook.Worksheets.Add("My Sheet2"); sheet2.Cells[1, 1].Value = "jie"; sheet2.Cells[2, 1].Value = "xiaom"; sheet2.Cells[3, 1].Value = "ccx"; sheet2.Cells[4, 1].Value = "zhangs"; sheet2.Cells[1, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;//水平居中 sheet2.Cells[1, 1].Style.Font.Bold = true;//字體爲粗體 sheet2.Cells[1, 1].Style.Font.Size = 22;//字體大小 //寫到客戶端(下載) 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; }