From 0e546c3956605ef67b7d2ad68f8815fe00cad5df Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Wed, 1 Jun 2022 17:26:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=A4=9A=E4=BD=99=E6=A1=88?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../easyexcel/test/demo/fill/FillTest.java | 129 +----------------- .../easyexcel/test/demo/read/ReadTest.java | 40 +----- .../easyexcel/test/demo/write/WriteTest.java | 107 +-------------- 3 files changed, 12 insertions(+), 264 deletions(-) diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java index 0c71753e..618a030f 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java @@ -83,28 +83,13 @@ public class FillTest { return data(); }); - // 方案3.1 分多次 填充 会使用文件缓存(省内存) 使用 try-with-resources @since 3.1.0 + // 方案3 分多次 填充 会使用文件缓存(省内存) fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx"; try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) { WriteSheet writeSheet = EasyExcel.writerSheet().build(); excelWriter.fill(data(), writeSheet); excelWriter.fill(data(), writeSheet); } - - // 方案3.2 分多次 填充 会使用文件缓存(省内存) 不使用 try-with-resources - fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx"; - ExcelWriter excelWriter = null; - try { - excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); - WriteSheet writeSheet = EasyExcel.writerSheet().build(); - excelWriter.fill(data(), writeSheet); - excelWriter.fill(data(), writeSheet); - } finally { - // 千万别忘记close 会帮忙关闭流 - if (excelWriter != null) { - excelWriter.close(); - } - } } /** @@ -120,7 +105,7 @@ public class FillTest { TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "complex.xlsx"; String fileName = TestFileUtil.getPath() + "complexFill" + System.currentTimeMillis() + ".xlsx"; - // 方案1 : 使用 try-with-resources @since 3.1.0 + // 方案1 try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) { WriteSheet writeSheet = EasyExcel.writerSheet().build(); // 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。 @@ -135,29 +120,6 @@ public class FillTest { map.put("total", 1000); excelWriter.fill(map, writeSheet); } - - // 方案2 : 不使用 try-with-resources - ExcelWriter excelWriter = null; - try { - excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); - WriteSheet writeSheet = EasyExcel.writerSheet().build(); - // 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。 - // forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用 - // 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存 - // 如果数据量大 list不是最后一行 参照下一个 - FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); - excelWriter.fill(data(), fillConfig, writeSheet); - excelWriter.fill(data(), fillConfig, writeSheet); - Map map = MapUtils.newHashMap(); - map.put("date", "2019年10月9日13:28:28"); - map.put("total", 1000); - excelWriter.fill(map, writeSheet); - } finally { - // 千万别忘记close 会帮忙关闭流 - if (excelWriter != null) { - excelWriter.close(); - } - } } /** @@ -177,7 +139,7 @@ public class FillTest { String fileName = TestFileUtil.getPath() + "complexFillWithTable" + System.currentTimeMillis() + ".xlsx"; - // 方案1 : 使用 try-with-resources @since 3.1.0 + // 方案1 try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) { WriteSheet writeSheet = EasyExcel.writerSheet().build(); // 直接写入数据 @@ -204,42 +166,6 @@ public class FillTest { // 总体上写法比较复杂 但是也没有想到好的版本 异步的去写入excel 不支持行的删除和移动,也不支持备注这种的写入,所以也排除了可以 // 新建一个 然后一点点复制过来的方案,最后导致list需要新增行的时候,后面的列的数据没法后移,后续会继续想想解决方案 } - - // 方案2 : 不使用 try-with-resources - ExcelWriter excelWriter = null; - try { - excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); - WriteSheet writeSheet = EasyExcel.writerSheet().build(); - // 直接写入数据 - excelWriter.fill(data(), writeSheet); - excelWriter.fill(data(), writeSheet); - - // 写入list之前的数据 - Map map = new HashMap(); - map.put("date", "2019年10月9日13:28:28"); - excelWriter.fill(map, writeSheet); - - // list 后面还有个统计 想办法手动写入 - // 这里偷懒直接用list 也可以用对象 - List> totalListList = ListUtils.newArrayList(); - List totalList = ListUtils.newArrayList(); - totalListList.add(totalList); - totalList.add(null); - totalList.add(null); - totalList.add(null); - // 第四列 - totalList.add("统计:1000"); - // 这里是write 别和fill 搞错了 - excelWriter.write(totalListList, writeSheet); - - // 总体上写法比较复杂 但是也没有想到好的版本 异步的去写入excel 不支持行的删除和移动,也不支持备注这种的写入,所以也排除了可以 - // 新建一个 然后一点点复制过来的方案,最后导致list需要新增行的时候,后面的列的数据没法后移,后续会继续想想解决方案 - } finally { - // 千万别忘记close 会帮忙关闭流 - if (excelWriter != null) { - excelWriter.close(); - } - } } /** @@ -255,7 +181,7 @@ public class FillTest { TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "horizontal.xlsx"; String fileName = TestFileUtil.getPath() + "horizontalFill" + System.currentTimeMillis() + ".xlsx"; - // 方案1 : 使用 try-with-resources @since 3.1.0 + // 方案1 try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) { WriteSheet writeSheet = EasyExcel.writerSheet().build(); FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); @@ -266,25 +192,6 @@ public class FillTest { map.put("date", "2019年10月9日13:28:28"); excelWriter.fill(map, writeSheet); } - - // 方案2 : 不使用 try-with-resources - ExcelWriter excelWriter = null; - try { - excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); - WriteSheet writeSheet = EasyExcel.writerSheet().build(); - FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); - excelWriter.fill(data(), fillConfig, writeSheet); - excelWriter.fill(data(), fillConfig, writeSheet); - - Map map = new HashMap(); - map.put("date", "2019年10月9日13:28:28"); - excelWriter.fill(map, writeSheet); - } finally { - // 千万别忘记close 会帮忙关闭流 - if (excelWriter != null) { - excelWriter.close(); - } - } } /** @@ -301,7 +208,7 @@ public class FillTest { String fileName = TestFileUtil.getPath() + "compositeFill" + System.currentTimeMillis() + ".xlsx"; - // 方案1 : 使用 try-with-resources @since 3.1.0 + // 方案1 try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) { WriteSheet writeSheet = EasyExcel.writerSheet().build(); FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); @@ -319,32 +226,6 @@ public class FillTest { excelWriter.fill(map, writeSheet); } - - // 方案2 : 不使用 try-with-resources - ExcelWriter excelWriter = null; - try { - excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); - WriteSheet writeSheet = EasyExcel.writerSheet().build(); - FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); - // 如果有多个list 模板上必须有{前缀.} 这里的前缀就是 data1,然后多个list必须用 FillWrapper包裹 - excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet); - excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet); - excelWriter.fill(new FillWrapper("data2", data()), writeSheet); - excelWriter.fill(new FillWrapper("data2", data()), writeSheet); - excelWriter.fill(new FillWrapper("data3", data()), writeSheet); - excelWriter.fill(new FillWrapper("data3", data()), writeSheet); - - Map map = new HashMap(); - //map.put("date", "2019年10月9日13:28:28"); - map.put("date", new Date()); - - excelWriter.fill(map, writeSheet); - } finally { - // 千万别忘记close 会帮忙关闭流 - if (excelWriter != null) { - excelWriter.close(); - } - } } private List data() { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java index 87d4481f..527ca6dc 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java @@ -98,7 +98,7 @@ public class ReadTest { // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭 EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead(); - // 写法4: 使用 try-with-resources @since 3.1.0 + // 写法4 fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; // 一个文件一个reader try (ExcelReader excelReader = EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).build()) { @@ -107,23 +107,6 @@ public class ReadTest { // 读取一个sheet excelReader.read(readSheet); } - - // 写法5: 不使用 try-with-resources - fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; - // 一个文件一个reader - ExcelReader excelReader = null; - try { - excelReader = EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).build(); - // 构建一个sheet 这里可以指定名字或者no - ReadSheet readSheet = EasyExcel.readSheet(0).build(); - // 读取一个sheet - excelReader.read(readSheet); - } finally { - if (excelReader != null) { - // 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的 - excelReader.close(); - } - } } /** @@ -162,7 +145,7 @@ public class ReadTest { // 读取部分sheet fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; - // 写法1: 使用 try-with-resources @since 3.1.0 + // 写法1 try (ExcelReader excelReader = EasyExcel.read(fileName).build()) { // 这里为了简单 所以注册了 同样的head 和Listener 自己使用功能必须不同的Listener ReadSheet readSheet1 = @@ -172,25 +155,6 @@ public class ReadTest { // 这里注意 一定要把sheet1 sheet2 一起传进去,不然有个问题就是03版的excel 会读取多次,浪费性能 excelReader.read(readSheet1, readSheet2); } - - // 写法2: 不使用 try-with-resources - ExcelReader excelReader = null; - try { - excelReader = EasyExcel.read(fileName).build(); - - // 这里为了简单 所以注册了 同样的head 和Listener 自己使用功能必须不同的Listener - ReadSheet readSheet1 = - EasyExcel.readSheet(0).head(DemoData.class).registerReadListener(new DemoDataListener()).build(); - ReadSheet readSheet2 = - EasyExcel.readSheet(1).head(DemoData.class).registerReadListener(new DemoDataListener()).build(); - // 这里注意 一定要把sheet1 sheet2 一起传进去,不然有个问题就是03版的excel 会读取多次,浪费性能 - excelReader.read(readSheet1, readSheet2); - } finally { - if (excelReader != null) { - // 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的 - excelReader.close(); - } - } } /** diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java index f7d015ca..607a91be 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java @@ -86,28 +86,13 @@ public class WriteTest { // 如果这里想使用03 则 传入excelType参数即可 EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data()); - // 写法3:使用 try-with-resources @since 3.1.0 + // 写法3 fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx"; // 这里 需要指定写用哪个class去写 try (ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build()) { WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); excelWriter.write(data(), writeSheet); } - - // 写法4: 不使用 try-with-resources - fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx"; - // 这里 需要指定写用哪个class去写 - ExcelWriter excelWriter = null; - try { - excelWriter = EasyExcel.write(fileName, DemoData.class).build(); - WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); - excelWriter.write(data(), writeSheet); - } finally { - // 千万别忘记close 会帮忙关闭流 - if (excelWriter != null) { - excelWriter.close(); - } - } } /** @@ -185,7 +170,7 @@ public class WriteTest { */ @Test public void repeatedWrite() { - // 方法1.1: 如果写到同一个sheet 使用 try-with-resources @since 3.1.0 + // 方法1: 如果写到同一个sheet String fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; // 这里 需要指定写用哪个class去写 try (ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build()) { @@ -199,28 +184,7 @@ public class WriteTest { } } - // 方法1.2: 如果写到同一个sheet 不使用 try-with-resources - fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; - ExcelWriter writer = null; - try { - // 这里 需要指定写用哪个class去写 - writer = EasyExcel.write(fileName, DemoData.class).build(); - // 这里注意 如果同一个sheet只要创建一次 - WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); - // 去调用写入,这里我调用了五次,实际使用时根据数据库分页的总的页数来 - for (int i = 0; i < 5; i++) { - // 分页去数据库查询数据 这里可以去数据库查询每一页的数据 - List data = data(); - writer.write(data, writeSheet); - } - } finally { - // 千万别忘记close 会帮忙关闭流 - if (writer != null) { - writer.close(); - } - } - - // 方法2.1: 如果写到不同的sheet 同一个对象 使用 try-with-resources @since 3.1.0 + // 方法2: 如果写到不同的sheet 同一个对象 fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; // 这里 指定文件 try (ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build()) { @@ -234,27 +198,7 @@ public class WriteTest { } } - // 方法2.2: 如果写到不同的sheet 同一个对象 不使用 try-with-resources - fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; - try { - // 这里 指定文件 - writer = EasyExcel.write(fileName, DemoData.class).build(); - // 去调用写入,这里我调用了五次,实际使用时根据数据库分页的总的页数来。这里最终会写到5个sheet里面 - for (int i = 0; i < 5; i++) { - // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样 - WriteSheet writeSheet = EasyExcel.writerSheet(i, "模板" + i).build(); - // 分页去数据库查询数据 这里可以去数据库查询每一页的数据 - List data = data(); - writer.write(data, writeSheet); - } - } finally { - // 千万别忘记close 会帮忙关闭流 - if (writer != null) { - writer.close(); - } - } - - // 方法3.1 如果写到不同的sheet 不同的对象 使用 try-with-resources @since 3.1.0 + // 方法3 如果写到不同的sheet 不同的对象 fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; // 这里 指定文件 try (ExcelWriter excelWriter = EasyExcel.write(fileName).build()) { @@ -269,26 +213,6 @@ public class WriteTest { } } - // 方法3.2 如果写到不同的sheet 不同的对象 不使用 try-with-resources - fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; - try { - // 这里 指定文件 - writer = EasyExcel.write(fileName).build(); - // 去调用写入,这里我调用了五次,实际使用时根据数据库分页的总的页数来。这里最终会写到5个sheet里面 - for (int i = 0; i < 5; i++) { - // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。这里注意DemoData.class 可以每次都变,我这里为了方便 所以用的同一个class - // 实际上可以一直变 - WriteSheet writeSheet = EasyExcel.writerSheet(i, "模板" + i).head(DemoData.class).build(); - // 分页去数据库查询数据 这里可以去数据库查询每一页的数据 - List data = data(); - writer.write(data, writeSheet); - } - } finally { - // 千万别忘记close 会帮忙关闭流 - if (writer != null) { - writer.close(); - } - } } /** @@ -649,7 +573,7 @@ public class WriteTest { @Test public void tableWrite() { String fileName = TestFileUtil.getPath() + "tableWrite" + System.currentTimeMillis() + ".xlsx"; - // 方法1 这里直接写多个table的案例了,如果只有一个 也可以直一行代码搞定,参照其他案, 使用 try-with-resources @since 3.1.0 + // 方法1 这里直接写多个table的案例了,如果只有一个 也可以直一行代码搞定,参照其他案 // 这里 需要指定写用哪个class去写 try (ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build()) { // 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了 @@ -662,27 +586,6 @@ public class WriteTest { // 第二次写如也会创建头,然后在第一次的后面写入数据 excelWriter.write(data(), writeSheet, writeTable1); } - - // 方法2 这里直接写多个table的案例了,如果只有一个 也可以直一行代码搞定,参照其他案, 不使用 try-with-resources - // 这里 需要指定写用哪个class去写 - ExcelWriter excelWriter = null; - try { - excelWriter = EasyExcel.write(fileName, DemoData.class).build(); - // 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了 - WriteSheet writeSheet = EasyExcel.writerSheet("模板").needHead(Boolean.FALSE).build(); - // 这里必须指定需要头,table 会继承sheet的配置,sheet配置了不需要,table 默认也是不需要 - WriteTable writeTable0 = EasyExcel.writerTable(0).needHead(Boolean.TRUE).build(); - WriteTable writeTable1 = EasyExcel.writerTable(1).needHead(Boolean.TRUE).build(); - // 第一次写入会创建头 - excelWriter.write(data(), writeSheet, writeTable0); - // 第二次写如也会创建头,然后在第一次的后面写入数据 - excelWriter.write(data(), writeSheet, writeTable1); - } finally { - // 千万别忘记close 会帮忙关闭流 - if (excelWriter != null) { - excelWriter.close(); - } - } } /**