From ad5fd4967a2fb2b66a13b9fa620e6ff787f8a776 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 17 Sep 2021 21:14:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejdk8=E7=9A=84=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/alibaba/excel/ExcelWriter.java | 65 +++++++++++++++---- .../builder/ExcelWriterSheetBuilder.java | 13 ++++ .../builder/ExcelWriterTableBuilder.java | 5 ++ .../easyexcel/test/demo/fill/FillTest.java | 20 ++++-- .../easyexcel/test/demo/write/WriteTest.java | 17 +++-- 5 files changed, 100 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/alibaba/excel/ExcelWriter.java b/src/main/java/com/alibaba/excel/ExcelWriter.java index 634606f7..bfdf2f26 100644 --- a/src/main/java/com/alibaba/excel/ExcelWriter.java +++ b/src/main/java/com/alibaba/excel/ExcelWriter.java @@ -1,6 +1,7 @@ package com.alibaba.excel; import java.util.Collection; +import java.util.function.Supplier; import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.write.ExcelBuilder; @@ -41,25 +42,31 @@ public class ExcelWriter { /** * Write data to a sheet * - * @param data - * Data to be written - * @param writeSheet - * Write to this sheet + * @param data Data to be written + * @param writeSheet Write to this sheet * @return this current writer */ public ExcelWriter write(Collection data, WriteSheet writeSheet) { return write(data, writeSheet, null); } + /** + * Write data to a sheet + * + * @param supplier Data to be written + * @param writeSheet Write to this sheet + * @return this current writer + */ + public ExcelWriter write(Supplier> supplier, WriteSheet writeSheet) { + return write(supplier.get(), writeSheet, null); + } + /** * Write value to a sheet * - * @param data - * Data to be written - * @param writeSheet - * Write to this sheet - * @param writeTable - * Write to this table + * @param data Data to be written + * @param writeSheet Write to this sheet + * @param writeTable Write to this table * @return this */ public ExcelWriter write(Collection data, WriteSheet writeSheet, WriteTable writeTable) { @@ -67,6 +74,19 @@ public class ExcelWriter { return this; } + /** + * Write value to a sheet + * + * @param supplier Data to be written + * @param writeSheet Write to this sheet + * @param writeTable Write to this table + * @return this + */ + public ExcelWriter write(Supplier> supplier, WriteSheet writeSheet, WriteTable writeTable) { + excelBuilder.addContent(supplier.get(), writeSheet, writeTable); + return this; + } + /** * Fill value to a sheet * @@ -91,6 +111,30 @@ public class ExcelWriter { return this; } + /** + * Fill value to a sheet + * + * @param supplier + * @param writeSheet + * @return + */ + public ExcelWriter fill(Supplier supplier, WriteSheet writeSheet) { + return fill(supplier.get(), null, writeSheet); + } + + /** + * Fill value to a sheet + * + * @param supplier + * @param fillConfig + * @param writeSheet + * @return + */ + public ExcelWriter fill(Supplier supplier, FillConfig fillConfig, WriteSheet writeSheet) { + excelBuilder.fill(supplier.get(), fillConfig, writeSheet); + return this; + } + /** * Close IO */ @@ -102,7 +146,6 @@ public class ExcelWriter { /** * Prevents calls to {@link #finish} from freeing the cache - * */ @Override protected void finalize() { diff --git a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java index 0ec448e2..111c54e1 100644 --- a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java +++ b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java @@ -1,6 +1,7 @@ package com.alibaba.excel.write.builder; import java.util.Collection; +import java.util.function.Supplier; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.exception.ExcelGenerateException; @@ -74,6 +75,18 @@ public class ExcelWriterSheetBuilder extends AbstractExcelWriterParameterBuilder excelWriter.finish(); } + public void doWrite(Supplier> supplier) { + doWrite(supplier.get()); + } + + public void doFill(Supplier supplier) { + doFill(supplier.get()); + } + + public void doFill(Supplier supplier, FillConfig fillConfig) { + doFill(supplier.get(), fillConfig); + } + public ExcelWriterTableBuilder table() { return table(null); } diff --git a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java index b7a90be9..279ac30a 100644 --- a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java +++ b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java @@ -1,6 +1,7 @@ package com.alibaba.excel.write.builder; import java.util.Collection; +import java.util.function.Supplier; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.exception.ExcelGenerateException; @@ -55,6 +56,10 @@ public class ExcelWriterTableBuilder extends AbstractExcelWriterParameterBuilder excelWriter.finish(); } + public void doWrite(Supplier> supplier) { + doWrite(supplier.get()); + } + @Override protected WriteTable parameter() { return writeTable; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java index ffa8280c..a08368e6 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java @@ -7,9 +7,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.junit.Ignore; -import org.junit.Test; - import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; @@ -18,11 +15,14 @@ import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.fill.FillConfig; import com.alibaba.excel.write.metadata.fill.FillWrapper; +import org.junit.Ignore; +import org.junit.Test; + /** * 写的填充写法 * - * @since 2.1.1 * @author Jiaju Zhuang + * @since 2.1.1 */ @Ignore public class FillTest { @@ -71,7 +71,17 @@ public class FillTest { // 这里 会填充到第一个sheet, 然后文件流会自动关闭 EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(data()); - // 方案2 分多次 填充 会使用文件缓存(省内存) + // 方案2 分多次 填充 会使用文件缓存(省内存) jdk8 + fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx"; + EasyExcel.write(fileName) + .withTemplate(templateFileName) + .sheet() + .doFill(() -> { + // 分页查询数据 + return data(); + }); + + // 方案3 分多次 填充 会使用文件缓存(省内存) fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx"; ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); WriteSheet writeSheet = EasyExcel.writerSheet().build(); diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java index 1bf11fe4..c2821dab 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java @@ -59,14 +59,25 @@ public class WriteTest { */ @Test public void simpleWrite() { - // 写法1 + // 写法1 JDK8+ String fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx"; // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 如果这里想使用03 则 传入excelType参数即可 - EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data()); + EasyExcel.write(fileName, DemoData.class) + .sheet("模板") + .doWrite(() -> { + // 分页查询数据 + return data(); + }); // 写法2 fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx"; + // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 + // 如果这里想使用03 则 传入excelType参数即可 + EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data()); + + // 写法3 + fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx"; // 这里 需要指定写用哪个class去写 ExcelWriter excelWriter = null; try { @@ -329,7 +340,6 @@ public class WriteTest { commentData.setRelativeLastColumnIndex(1); commentData.setRelativeLastRowIndex(1); - // 设置公式 WriteCellData formula = new WriteCellData<>(); writeCellDemoData.setFormulaData(formula); @@ -339,7 +349,6 @@ public class WriteTest { // 这里只是例子 如果真的涉及到公式 能内存算好尽量内存算好 公式能不用尽量不用 formulaData.setFormulaValue("REPLACE(123456789,1,1,2)"); - // 设置单个单元格的样式 当然样式 很多的话 也可以用注解等方式。 WriteCellData writeCellStyle = new WriteCellData<>("单元格样式"); writeCellDemoData.setWriteCellStyle(writeCellStyle);