easyexcel生成文件分页写数据

添加依赖

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.6</version>
        </dependency>

代码实现

public static void main(String[] args) {
        try {
            // 内容样式策略
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            // 垂直居中,水平居中
            contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
            contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
            contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
            contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
            contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
            // 生成csv文件文件名
            HorizontalCellStyleStrategy contentStyleStrategy = new HorizontalCellStyleStrategy(contentWriteCellStyle, contentWriteCellStyle);
//            String finalFileName = CustomFileUtils.generateCsvFileName(fileName);
            // 最终csv文件全路径
            String path = "D:\\TEST.xlsx";
            // 创建生成后的csv文件对象
            File file = new File(path);
            // 获取父目录并创建
            File parentFile = file.getParentFile();
            parentFile.mkdirs();
            // 创建新文件
            Files.createFile(file.toPath());
            // 文件包装到转输出流
            OutputStream os = openOutputStream(file, false);
            ExcelWriterBuilder write = EasyExcel.write(os, TestExcel.class);
            ExcelWriter build = write.autoCloseStream(true)
                    .automaticMergeHead(true) // 这里一定要设置为true,否则在无法实现表头列合并
                    .excelType(ExcelTypeEnum.XLSX)
                    .file(os)
                    .head(TestExcel.class)
                    .registerWriteHandler(contentStyleStrategy)
                    .build();
            // 工作簿编号
            int sheetNum = 1;
            // 分页数
            // 每页查询数量
            int pageSize = 10;
            for (int pageNo = 0; pageNo<pageSize; pageNo++) {
                // 这里可以自己通过分页查询数据库来取数据
                List<TestExcel> list = new ArrayList<>();
                TestExcel testExcel = new TestExcel();
                testExcel.setName("测试"+pageNo);
                testExcel.setRemark("测试"+pageNo);
                list.add(testExcel);
                WriteSheet writeSheet = EasyExcel.writerSheet("sheet"+sheetNum).head(TestExcel.class).build();
                build.write(list, writeSheet);
                if (pageNo == pageSize-1) {
                    build.finish();
                    break;
                }
            }
            build.finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static FileOutputStream openOutputStream(final File file, final boolean append) throws IOException {
        if (file.exists()) {
            if (file.isDirectory()) {
                throw new IOException("File '" + file + "' exists but is a directory");
            }
            if (file.canWrite() == false) {
                throw new IOException("File '" + file + "' cannot be written to");
            }
        } else {
            final File parent = file.getParentFile();
            if (parent != null) {
                if (!parent.mkdirs() && !parent.isDirectory()) {
                    throw new IOException("Directory '" + parent + "' could not be created");
                }
            }
        }
        return new FileOutputStream(file, append);
    }
@Data
public class TestExcel  {

    @ExcelProperty("姓名")
    private String name;

    @ExcelProperty("住址")
    private String remark;

}

参考 https://www.cnblogs.com/nuccch/p/16586627.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值