poi导出excel

controller返回为map,map中存文件名

AjaxResult.success(filename);
  //创建工作薄
        Workbook wb = new XSSFWorkbook();
        //创建sheet
        Sheet sheet = wb.createSheet("运营数据报表");
        // 自动设置宽度
        sheet.autoSizeColumn(0);
        sheet.setColumnWidth(0,14000);
        sheet.setDefaultRowHeight((short) 500);
         Row titleRow = sheet.createRow(0);
         Cell cell = titleRow.createCell(i);
         cell.setCellStyle(titleStyle);
//合并单元格
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,sheet.getRow(0).getLastCellNum()-1));
  doExport(wb);//执行导出方法,下面

执行导出方法

    public AjaxResult doExport(Workbook wb){
        OutputStream out = null;
        try {

            String filename = "数据报表.xlsx";
            out = new FileOutputStream(getAbsoluteFile(filename));
            wb.write(out);
            return AjaxResult.success(filename);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("导出Excel失败,请联系网站管理员!");
        } finally {
            if (wb != null) {
                try {
                    wb.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

单元格样式

    public CellStyle createTitleStyle(Workbook wb){
        CellStyle titleStyle = wb.createCellStyle();
        // 设置单元格对齐方式,水平居左
        titleStyle.setAlignment(HorizontalAlignment.LEFT);
        //单元格边框
        titleStyle.setBorderTop(BorderStyle.THIN);
        titleStyle.setBorderLeft(BorderStyle.THIN);
        titleStyle.setBorderRight(BorderStyle.THIN);
        //titleStyle.setBorderBottom(BorderStyle.THIN);
        // 设置字体样式
        Font titleFont = wb.createFont();
        // 字体高度
        titleFont.setFontHeightInPoints((short) 16);
        // 字体样式
        titleFont.setFontName("宋体");
        titleFont.setBold(true);

        titleStyle.setFont(titleFont);

        // 设置居中样式,水平居中
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        titleInnerStyle.setRightBorderColor((short) 0);//边框黑色
        titleInnerStyle.setBorderRight(BorderStyle.THIN);
        titleInnerStyle.setBorderLeft(BorderStyle.THIN);
       titleInnerStyle.setLeftBorderColor((short) 0);//左侧白色色边框
        return titleStyle;
    }

前端:

    exportExcel() {
      const _this = this
      this.$confirm('是否确认导出数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        _this.loading = true
        return exportReport(_this.dataParam)
      }).then(response => {
        _this.loading = false
        this.download(response.msg)
      }).catch(function() {})

      /*      /!* generate workbook object from table *!/
      var xlsxParam = { raw: true } // 导出的内容只做解析,不进行格式转换
      var wb = XLSX.utils.table_to_book(document.querySelector('#exportTab'), xlsxParam)

      /!* get binary string as output *!/
      var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
      try {
        FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), '审核情况表.xlsx')
      } catch (e) {
        if (typeof console !== 'undefined') {
          console.log(e, wbout)
        }
      }
      return wbout*/
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值