MVC通过NPOI.dll导出word文档

下载地址:http://download.csdn.net/detail/lilinoscar/9424645

1.首先项目添加并引用:NPOI.dll   

2.控制器引用NPOI:

          using NPOI.HSSF.UserModel;
          using NPOI.SS.UserModel;

3.代码:

    public ActionResult ExportData()
        {
            HSSFWorkbook workbook = new HSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("sheet1");
            IRow row = null;
            ICell cell = null;
            row = sheet.CreateRow(0);
            cell = row.CreateCell(0);
            cell.SetCellValue("数字");
            for (int i = 0; i <= 12; i++)
            {
                row = sheet.CreateRow(i);
                cell = row.CreateCell(0);
                cell.SetCellValue(i);
            }
            string fileName ="20160101.xls";
            MemoryStream ms = new MemoryStream();
            workbook.Write(ms);
            return File(ms.ToArray(), "application/ms-excel", fileName);
        }


你可能感兴趣的:(mvc,导出word,NPOI,MVC4,mvc导出word)