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();
        }
相關文章
相關標籤/搜索