NPOI的基本用法,导出Excel
void DownloadForAccountIndex(IReadOnlyList<AccountInfoView> list) { NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(); NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("账号数"); int i = 0; // 第一行: NPOI.SS.UserModel.IRow rowTitle = sheet.CreateRow(0); rowTitle.CreateCell(i++).SetCellValue("时间"); rowTitle.CreateCell(i++).SetCellValue("累计账号数"); rowTitle.CreateCell(i++).SetCellValue("新增账号数"); int rowIndex = 1; foreach (var item in list) { i = 0; NPOI.SS.UserModel.IRow row = sheet.CreateRow(rowIndex); row.CreateCell(i++).SetCellValue(item.CreateMonth); row.CreateCell(i++).SetCellValue(item.AllCount); row.CreateCell(i++).SetCellValue(item.NewCount); rowIndex++; } // 写入到客户端 System.IO.MemoryStream ms = new System.IO.MemoryStream(); book.Write(ms); Response.AddHeader("Content-Disposition", string.Format("attachment; filename=账号数({0}).xls", DateTime.Now.ToString("yyyyMMdd"))); Response.BinaryWrite(ms.ToArray()); book = null; ms.Close(); ms.Dispose(); Response.Flush(); }