Net Core swagger使用NPOI导出为excel

后台代码如下:

        public async void ExportData(string condition)
        {
            List<Model> models = modelBLL.GetModelList(condition);


            var workBook = new HSSFWorkbook();
            var sheet = workBook.CreateSheet("导出数据");
            //列宽
            var dic=new Dictionary<int,int>();

            var headerFont = workBook.CreateFont();
            headerFont.IsBold = true;


            var headerStyle = workBook.CreateCellStyle();
            headerStyle.Alignment = HorizontalAlignment.Center;
            headerStyle.SetFont(headerFont);
            headerStyle.BorderBottom = BorderStyle.Thin;
            headerStyle.BorderLeft = BorderStyle.Thin;
            headerStyle.BorderRight = BorderStyle.Thin;
            headerStyle.BorderTop = BorderStyle.Thin;


            var cellStyle = workBook.CreateCellStyle();
            cellStyle.BorderBottom = BorderStyle.Thin;
            cellStyle.BorderLeft = BorderStyle.Thin;
            cellStyle.BorderRight = BorderStyle.Thin;
            cellStyle.BorderTop = BorderStyle.Thin;


            var rowIndex = 0;
            var row = sheet.CreateRow(rowIndex);
            //设置行高
            row.Height=200*2;
            var cell = row.CreateCell(0);
            cell.SetCellValue("国家编码");
            cell.CellStyle = headerStyle;
            dic.Add(0,Encoding.Default.GetBytes(cell.StringCellValue).Length*256+200);
            cell = row.CreateCell(1);
            cell.SetCellValue("国家名称");
            cell.CellStyle = headerStyle;
            dic.Add(1,Encoding.Default.GetBytes(cell.StringCellValue).Length*256+200);
            cell = row.CreateCell(2);
            cell.SetCellValue("所在州");
            cell.CellStyle = headerStyle;
            dic.Add(2,Encoding.Default.GetBytes(cell.StringCellValue).Length*256+200);

            foreach (Model item in models)
            {
                rowIndex++;
                row = sheet.CreateRow(rowIndex);
                row.Height=200*2;
                cell = row.CreateCell(0);
                cell.SetCellValue(item.country_code);
                cell.CellStyle = cellStyle;
                if(GetColumnWidth(cell)>dic[0])
                {
                    dic[0]=GetColumnWidth(cell);
                }
                cell = row.CreateCell(1);
                cell.SetCellValue(item.country_name);
                cell.CellStyle = cellStyle;
                if(GetColumnWidth(cell)>dic[1])
                {
                    dic[1]=GetColumnWidth(cell);
                }
                cell = row.CreateCell(2);
                cell.SetCellValue(item.state);
                cell.CellStyle = cellStyle;
                if(GetColumnWidth(cell)>dic[2])
                {
                    dic[2]=GetColumnWidth(cell);
                }
            }
            //按照列数设置列宽
            for(int i=0;i<3;i++)
            {
                sheet.SetColumnWidth(i,dic[i]);
            }

            byte[] buffer = null;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                workBook.Write(memoryStream);
                buffer = memoryStream.GetBuffer();
                memoryStream.Close();
            }

            //这是最重要的部分
            this.Response.Headers.Add("content-disposition", $"attachment;filename=Model.xls");
            await this.Response.Body.WriteAsync(buffer, 0, buffer.Length);
        }
        
        public int GetColumnWidth(ICell cell)
        {
            int length=Encoding.Default.GetBytes(cell.StringCellValue).Length*256+200;
            length=length>15000?15000:length;
            return length;
        }

前端代码:

//在HTML网页加入如下代码
<iframe id="iframe" src="" height="0" width="0" style="display:none"></ifame>
//js代码
var url="/controller/ExportData?condition=condition";
$("#iframe").attr("src",url)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值