java后端导出前端展示

效果图在这里插入图片描述

前端代码

exportExcelAll(){
      window.location.href = getBaseUrl() + 'Action/excelDataAll?happenDate='+this.params.happenDate;

    },

后端代码

try{

            Workbook workbook=new XSSFWorkbook();

            //创建sheet
            Sheet sheet1=workbook.createSheet("结果总数拦截记录");
            

            //写入sheet数据

            //表头
            JSONObject head=new JSONObject();
            head.put("deptname","科室名称");
            
            //表头属性
            List<String> props=Arrays.asList("deptname");

            //第一个sheet的数据
            JSONObject obj1=getDeptShjlDetails(happenDate,strDate,endDate,null,"结果总数",null,null,null,null,null,"","",gzlx);
            JSONArray arr1=obj1.getJSONArray("list");

           

            //创建单元格样式
            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setAlignment(HorizontalAlignment.CENTER);

            //组装数据
            this.setData(sheet1,props,head,arr1,cellStyle);
           

            //导出excel文件
            res.reset();
            ServletOutputStream outputStream=res.getOutputStream();
            //转为字节流
            ByteArrayOutputStream os = new ByteArrayOutputStream();

            workbook.write(os);
            workbook.close();

            byte[]  bytes = os.toByteArray();
            res.setContentType("application/pdf;charset=UTF-8");
            res.setHeader("Content-Disposition", "attachment;filename=" + new String(("全部科室拦截记录.xlsx").getBytes("utf-8"), "iso-8859-1"));
            res.setContentLength(bytes.length);
            outputStream.write(bytes, 0, bytes.length);
            outputStream.flush();


        }catch (Exception e){
            e.printStackTrace();

        }
private void setData(Sheet sheet,List<String> props,JSONObject head,JSONArray arr,CellStyle cellStyle){


        //创建表头
        Row row=sheet.createRow(0);
        for(int j=0;j<props.size();j++){
            //创建列
            Cell cell=row.createCell(j);
            cell.setCellValue(head.getString(props.get(j)));
            cell.setCellStyle(cellStyle);
        }

        //创建表数据
        for(int i=0;i<arr.size();i++){
            //创建行
            Row rowData=sheet.createRow(i+1);
            for(int j=0;j<props.size();j++){
                //创建列
                Cell cell=rowData.createCell(j);
                Record record= (Record) arr.get(i);
                cell.setCellValue(record.getString(props.get(j)));
                cell.setCellStyle(cellStyle);
            }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值