【EASYPOI导出excel ExcelExportUtil.exportExcel】

前端可使用form请求后端

(新写法可new FormData进行处理,此处未展示)

        $scope.id = 0;
        $scope.exportOceanTariff = function () {
            var body = $('body');
            var iframe = $("<iframe>");
            iframe.attr('id', 'frameFile' + $scope.id);
            iframe.attr('name', 'frameFile' + $scope.id);
            iframe.attr('style', 'display:none');
            body.append(iframe);

            var form = $("<form>");
            form.attr('style', 'display:none');
            form.attr('target', 'frameFile' + $scope.id);
            form.attr('method', 'post');
            form.attr('action', '/**exportUrl**');
            var input = $('<input>');
            input.attr('type', 'hidden');
            input.attr('name', 'oceanTariff');
            input.attr('value', JSON.stringify($scope.search));
            form.append(input);
            body.append(form);
            form.submit();
            $('#frameFile' + $scope.id).load(function () {
                var response = JSON.parse($(iframe[0].contentWindow.document.body).text());
                if (response.success === false) {
                    
                }
            })
            form.remove();
            $scope.id = $scope.id + 1;
        }

多sheet

//添加表
List<Map<String, Object>> sheetsList = new ArrayList<>();
示例:crete sheet

    /**
     * create sheet
     * @param sheetName sheetName
     * @param dataList dataList
     * @return Map《String, Object》
     */
    public Map<String, Object> createSheet(String sheetName, List<ExportVo> dataList) {
        if (StringUtils.isBlank(sheetName) || CollectionUtils.isEmpty(dataList)) {
            return Maps.newHashMap();
        }

        // 准备导出模板
        ExportParams exportParams = new ExportParams();
        exportParams.setSheetName(sheetName);
        exportParams.setHeight((short) 8);
        exportParams.setStyle(ExcelExportStylerDefaultImpl.class);

        Map<String, Object> map = Maps.newHashMap();
        map.put("data", dataList);
        map.put("entity", ExportVo.class);
        map.put("title", exportParams);

        return map;
    }

service

public void exportData(String data, HttpServletResponse response)

sheetsList.add(createMySheet)
将自定义sheetsList整理完毕后写入response即可


            String fileName = "mySheet.xls";
            fileName = URLEncoder.encode(fileName, "UTF-8");
            // 设置响应输出的头类型
            // 设置contentType与fileName编码一致
            response.setContentType("application/vnd.ms-excel;chartset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            //创建excel文件
            Workbook workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
            //通过response输出流写入response
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值