.NET using NPOI (2) - Export Data

       Previous blog reports, installed NPOI, and create an Excel, this tell us about how to export the data you want. Directly on the code:

public ActionResult Index()
{
    //声明工作表
    HSSFWorkbook wk = new HSSFWorkbook();
    ISheet sheet = wk.CreateSheet("工作汇报1");

    //设置表头
    IRow cells = sheet.CreateRow(0);
    cells.CreateCell(0).SetCellValue("编号");
    cells.CreateCell(1).SetCellValue("任务名称");
    cells.CreateCell(2).SetCellValue("核心内容");
    //添加10行数据
    for (int i = 0; i < 10; i++)
    {
        IRow rowtemp = sheet.CreateRow(i + 1); //循环行
        rowtemp.CreateCell(0).SetCellValue(i + 1);
        rowtemp.CreateCell(1).SetCellValue("任务名称"+i);
        rowtemp.CreateCell(2).SetCellValue("此任务主要是处理..."+i);
    }
    //导出excel,名字可自定义
    MemoryStream memoryStream = new MemoryStream();
    wk.Write(memoryStream);
    memoryStream.Seek(0, SeekOrigin.Begin);
    return File(memoryStream, "application/vnd.ms-excel", "工作汇报.xls");
}

       We take a look at the guide out the effect of:

       More than we realized export Excel data with NPOI function,

Published 109 original articles · won praise 42 · views 570 000 +

Guess you like

Origin blog.csdn.net/sinat_28984567/article/details/94358981