首先安装NPOI依赖包
在控制器里面定义如下代码
private readonly IHostingEnvironment _hostingEnvironment;然后编写生成Excel的Action如下
public ExcelController(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
[HttpGet("GetExportList")]
        public void GetExportList()
        {
            string path = _hostingEnvironment.ContentRootPath+@"\file\";//路径指定项目下面的file文件夹
            //取出数据源
            var exportList = new ConfigDal().GetAllDept();
            HSSFWorkbook book = new HSSFWorkbook();
            ISheet s1 = book.CreateSheet("领料单");
            IRow r1 = s1.CreateRow(0);
            r1.CreateCell(0).SetCellValue("deptNo");
            r1.CreateCell(1).SetCellValue("deptName");
            for (int i = 0; i < exportList.Count; i++)
            {
                NPOI.SS.UserModel.IRow rt = s1.CreateRow(i + 1);
                rt.CreateCell(0).SetCellValue(exportList[i].deptNo);
                rt.CreateCell(1).SetCellValue(exportList[i].deptName);
            }
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            FileStream stream = new FileStream(path + DateTime.Now.ToString("yyyyMMddhhmmssffff") + ".xls", FileMode.CreateNew);
            book.Write(stream);
            stream.Seek(0, SeekOrigin.Begin);
            book.Close();
            stream.Close();
        }
                
- 本文标题: NETCore 使用DotNetCore.NPOI进行数据导出Excel操作
- 文章分类:【.NET/Web】
- 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
- 上一篇:PHP截图字符串长度,获取长度
- 下一篇: NETCore前后端分离,附件下载功能的实现