Java 实现excel导出(有模板版)

//把处理好的list传入准备导出
public File exportExcel(List<> all, HttpServletResponse res) throws Exception {

        FILE_PATH = "path/模板.xlsx";
        FILE_NAME = "模板.xlsx";


        File export = new File("/" + UUID.randomUUID().toString());
        try {

            final Integer[] index = {1};
            List<RyzzUserEx> collect = all.stream().collect(Collectors.toList());
            int pageSize = 1000000;//每个sheet页的数据量
            int sheetCount = DataUtils.castInteger(Math.ceil(collect.size() * 1.0 / pageSize));

            ExcelExportConfig[] list = new ExcelExportConfig[sheetCount];
            for (int i = 0; i < sheetCount; i++) {
                int start = i * pageSize;
                int end = start + pageSize;
                if (start + pageSize > all.size()) {
                    end = all.size();
                }
                ExcelExportConfig<RyzzUserEx> excelExportConfig = new ExcelExportConfig("Sheet1",
                        "A,1", //模板开始位置
                        "N,1", //模板结束位置
                        collect.subList(start, end), RyzzUserEx.class);

                excelExportConfig.setExportSheetName("Sheet" + (i == 0 ? "1" : i + 1));
                excelExportConfig.setNeedShift(false);
                list[i] = excelExportConfig;
            }
            ExportService exportService = new ExportService(this.buildExportProperties());
            InputStream bis = null;
            //如果路径不存在则创建路径
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(filePath + FILE_NAME);//导出路径
            // 读取模板
            bis = this.getClass().getClassLoader().getResourceAsStream(FILE_PATH);
            byte[] buff = new byte[1024];
            int i = bis.read(buff);
            while (i != -1) {
                fos.write(buff, 0, i);// 写入数据
                fos.flush();
                i = bis.read(buff);

            }
            bis.close();
            fos.close();// 保存数据
            export = exportService.export(FILE_NAME, list);


            String absolutePath = export.getAbsolutePath();
            FileInputStream fileInputStream = new FileInputStream(absolutePath);
            byte[] buffOut = new byte[1024];
            int j = fileInputStream.read(buffOut);
            // 发送给客户端的数据
            OutputStream outputStream = res.getOutputStream();
            while (j != -1) {
                outputStream.write(buffOut, 0, j);
                outputStream.flush();
                j = fileInputStream.read(buffOut);
            }
            fileInputStream.close();
            outputStream.close();

            return file;

        } catch (Exception e) {
            throw e;
        } finally {
            File file = new File(filePath + FILE_NAME);
            FileUtils.deleteQuietly(file);
            FileUtils.deleteQuietly(export.getAbsoluteFile());
        }
    }

    public ExportProperties buildExportProperties() {
        ExportProperties exportProperties = new ExportProperties();
        exportProperties.setTempFolder(filePath);
        exportProperties.setTemplateFolder(filePath);
        return exportProperties;
    }
//对应excel实体类

@Data
public class RyzzKszxDto implements IEntity {
    @ExportColumnName("序号")
    private Integer index;

    @ExportColumnName("表头1")
    private String a1;

    @ExportColumnName("表头2")
    private String a2;

    @ExportColumnName("表头3")
    private String a3;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值