NPOI的基本用法,导出Excel

时间:2022-04-20 08:28:57
void DownloadForAccountIndex(IReadOnlyList<AccountInfoView> list)
{
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("账号数");
int i = ;
// 第一行:
NPOI.SS.UserModel.IRow rowTitle = sheet.CreateRow();
rowTitle.CreateCell(i++).SetCellValue("时间");
rowTitle.CreateCell(i++).SetCellValue("累计账号数");
rowTitle.CreateCell(i++).SetCellValue("新增账号数"); int rowIndex = ;
foreach (var item in list)
{
i = ;
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();
}