diff --git a/README.md b/README.md index 3a22ff64..dfc6f145 100644 --- a/README.md +++ b/README.md @@ -41,15 +41,13 @@ Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都 com.alibaba easyexcel - 3.0.4 + 3.0.5 ``` ## 广告位 ### 阿里巴巴新零售事业部招募 阿里巴巴新零售事业部--诚招JAVA资深开发、技术专家。有意向可以微信联系,简历可以发我邮箱jipengfei.jpf@alibaba-inc.com -### 阿里巴巴&滴滴内推 -有入职滴滴、阿里需求的可以联系群主帮忙内推(会帮忙审核简历并提出修改意见) ### easyexcel人员招募 由于工作较忙,有意愿做开源的同学可以报名,主要负责群里回答&issue处理,当然也可以做一些PR. 由于开源没有任何物质回报,然后现在的维护同学也是课余时间维护的,所以想加入的同学需要持之以恒,而不是一时兴起. diff --git a/pom.xml b/pom.xml index 89f4c341..3aa5fa65 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.alibaba easyexcel - 3.0.4 + 3.0.5 jar easyexcel diff --git a/src/main/java/com/alibaba/excel/event/AnalysisEventListener.java b/src/main/java/com/alibaba/excel/event/AnalysisEventListener.java index bc94d75e..96ccc659 100644 --- a/src/main/java/com/alibaba/excel/event/AnalysisEventListener.java +++ b/src/main/java/com/alibaba/excel/event/AnalysisEventListener.java @@ -3,7 +3,6 @@ package com.alibaba.excel.event; import java.util.Map; import com.alibaba.excel.context.AnalysisContext; -import com.alibaba.excel.metadata.CellExtra; import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.util.ConverterUtils; @@ -28,31 +27,4 @@ public abstract class AnalysisEventListener implements ReadListener { */ public void invokeHeadMap(Map headMap, AnalysisContext context) {} - /** - * The current method is called when extra information is returned - * - * @param extra - * extra information - * @param context - * analysis context - */ - @Override - public void extra(CellExtra extra, AnalysisContext context) {} - - /** - * All listeners receive this method when any one Listener does an error report. If an exception is thrown here, the - * entire read will terminate. - * - * @param exception - * @param context - */ - @Override - public void onException(Exception exception, AnalysisContext context) throws Exception { - throw exception; - } - - @Override - public boolean hasNext(AnalysisContext context) { - return true; - } } diff --git a/src/main/java/com/alibaba/excel/read/listener/IgnoreExceptionReadListener.java b/src/main/java/com/alibaba/excel/read/listener/IgnoreExceptionReadListener.java new file mode 100644 index 00000000..d539ad56 --- /dev/null +++ b/src/main/java/com/alibaba/excel/read/listener/IgnoreExceptionReadListener.java @@ -0,0 +1,23 @@ +package com.alibaba.excel.read.listener; + +import com.alibaba.excel.context.AnalysisContext; + +/** + * Interface to listen for read results + * + * @author Jiaju Zhuang + */ +public interface IgnoreExceptionReadListener extends ReadListener { + + /** + * All listeners receive this method when any one Listener does an error report. If an exception is thrown here, the + * entire read will terminate. + * + * @param exception + * @param context + * @throws Exception + */ + @Override + default void onException(Exception exception, AnalysisContext context) throws Exception {} + +} diff --git a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java index 5c8bf4bb..057919e9 100644 --- a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java +++ b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java @@ -23,7 +23,7 @@ import org.apache.commons.collections4.CollectionUtils; * * @author jipengfei */ -public class ModelBuildEventListener implements ReadListener>> { +public class ModelBuildEventListener implements IgnoreExceptionReadListener>> { @Override public void invokeHead(Map> cellDataMap, AnalysisContext context) { diff --git a/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java b/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java index 0795a9e4..becc1748 100644 --- a/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java +++ b/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java @@ -21,7 +21,7 @@ public class PageReadListener implements ReadListener { /** * Temporary storage of data */ - private List cachedData = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); + private List cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); /** * consumer */ @@ -33,17 +33,17 @@ public class PageReadListener implements ReadListener { @Override public void invoke(T data, AnalysisContext context) { - cachedData.add(data); - if (cachedData.size() >= BATCH_COUNT) { - consumer.accept(cachedData); - cachedData = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); + cachedDataList.add(data); + if (cachedDataList.size() >= BATCH_COUNT) { + consumer.accept(cachedDataList); + cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); } } @Override public void doAfterAllAnalysed(AnalysisContext context) { - if (CollectionUtils.isNotEmpty(cachedData)) { - consumer.accept(cachedData); + if (CollectionUtils.isNotEmpty(cachedDataList)) { + consumer.accept(cachedDataList); } } diff --git a/src/main/java/com/alibaba/excel/read/listener/ReadListener.java b/src/main/java/com/alibaba/excel/read/listener/ReadListener.java index d951d1a6..b56f8a56 100644 --- a/src/main/java/com/alibaba/excel/read/listener/ReadListener.java +++ b/src/main/java/com/alibaba/excel/read/listener/ReadListener.java @@ -21,7 +21,9 @@ public interface ReadListener extends Listener { * @param context * @throws Exception */ - default void onException(Exception exception, AnalysisContext context) throws Exception {} + default void onException(Exception exception, AnalysisContext context) throws Exception { + throw exception; + } /** * When analysis one head row trigger invoke function. diff --git a/src/main/java/com/alibaba/excel/util/DateUtils.java b/src/main/java/com/alibaba/excel/util/DateUtils.java index 4a297cf7..22d7f3d9 100644 --- a/src/main/java/com/alibaba/excel/util/DateUtils.java +++ b/src/main/java/com/alibaba/excel/util/DateUtils.java @@ -216,9 +216,9 @@ public class DateUtils { isDateCache = MapUtils.newHashMap(); DATE_THREAD_LOCAL.set(isDateCache); } else { - Boolean isDateCachedData = isDateCache.get(formatIndex); - if (isDateCachedData != null) { - return isDateCachedData; + Boolean isDatecachedDataList = isDateCache.get(formatIndex); + if (isDatecachedDataList != null) { + return isDatecachedDataList; } } boolean isDate = isADateFormatUncached(formatIndex, formatString); diff --git a/src/main/java/com/alibaba/excel/write/handler/DefaultWriteHandlerLoader.java b/src/main/java/com/alibaba/excel/write/handler/DefaultWriteHandlerLoader.java index 4d24d48f..18b96e21 100644 --- a/src/main/java/com/alibaba/excel/write/handler/DefaultWriteHandlerLoader.java +++ b/src/main/java/com/alibaba/excel/write/handler/DefaultWriteHandlerLoader.java @@ -49,6 +49,7 @@ public class DefaultWriteHandlerLoader { break; case CSV: handlerList.add(new DefaultRowWriteHandler()); + handlerList.add(new FillStyleCellWriteHandler()); break; default: break; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataTest.java index c7fc8205..fb1ef815 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataTest.java @@ -9,6 +9,7 @@ import java.util.List; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; +import org.junit.Assert; import org.junit.BeforeClass; import org.junit.FixMethodOrder; import org.junit.Test; @@ -22,13 +23,17 @@ public class ExceptionDataTest { private static File file07; private static File file03; + private static File fileException07; + private static File fileException03; private static File fileCsv; @BeforeClass public static void init() { - file07 = TestFileUtil.createNewFile("simple07.xlsx"); - file03 = TestFileUtil.createNewFile("simple03.xls"); - fileCsv = TestFileUtil.createNewFile("simpleCsv.csv"); + file07 = TestFileUtil.createNewFile("exception.xlsx"); + file03 = TestFileUtil.createNewFile("exception.xls"); + fileCsv = TestFileUtil.createNewFile("exception.csv"); + fileException07 = TestFileUtil.createNewFile("exceptionThrow.xlsx"); + fileException03 = TestFileUtil.createNewFile("exceptionThrow.xls"); } @Test @@ -46,6 +51,24 @@ public class ExceptionDataTest { readAndWrite(fileCsv); } + @Test + public void t11ReadAndWrite07() throws Exception { + readAndWriteException(fileException07); + } + + @Test + public void t12ReadAndWrite03() throws Exception { + readAndWriteException(fileException03); + } + + private void readAndWriteException(File file) throws Exception { + EasyExcel.write(new FileOutputStream(file), ExceptionData.class).sheet().doWrite(data()); + ArithmeticException exception = Assert.assertThrows(ArithmeticException.class, () -> EasyExcel.read( + new FileInputStream(file), ExceptionData.class, + new ExceptionThrowDataListener()).sheet().doRead()); + Assert.assertEquals("/ by zero",exception.getMessage()); + } + private void readAndWrite(File file) throws Exception { EasyExcel.write(new FileOutputStream(file), ExceptionData.class).sheet().doWrite(data()); EasyExcel.read(new FileInputStream(file), ExceptionData.class, new ExceptionDataListener()).sheet().doRead(); diff --git a/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionThrowDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionThrowDataListener.java new file mode 100644 index 00000000..01420c50 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionThrowDataListener.java @@ -0,0 +1,30 @@ +package com.alibaba.easyexcel.test.core.exception; + +import java.util.ArrayList; +import java.util.List; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.read.listener.ReadListener; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Jiaju Zhuang + */ +public class ExceptionThrowDataListener implements ReadListener { + private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionData.class); + List list = new ArrayList(); + + @Override + public void invoke(ExceptionData data, AnalysisContext context) { + list.add(data); + if (list.size() == 5) { + int i = 5 / 0; + } + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + } +} 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 baca2232..d0f90fe9 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 @@ -1,7 +1,6 @@ package com.alibaba.easyexcel.test.demo.fill; import java.io.File; -import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -11,6 +10,8 @@ import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.enums.WriteDirectionEnum; +import com.alibaba.excel.util.ListUtils; +import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.fill.FillConfig; import com.alibaba.excel.write.metadata.fill.FillWrapper; @@ -48,7 +49,7 @@ public class FillTest { // 方案2 根据Map填充 fileName = TestFileUtil.getPath() + "simpleFill" + System.currentTimeMillis() + ".xlsx"; // 这里 会填充到第一个sheet, 然后文件流会自动关闭 - Map map = new HashMap(); + Map map = MapUtils.newHashMap(); map.put("name", "张三"); map.put("number", 5.2); EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(map); @@ -114,7 +115,7 @@ public class FillTest { FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); excelWriter.fill(data(), fillConfig, writeSheet); excelWriter.fill(data(), fillConfig, writeSheet); - Map map = new HashMap(); + Map map = MapUtils.newHashMap(); map.put("date", "2019年10月9日13:28:28"); map.put("total", 1000); excelWriter.fill(map, writeSheet); @@ -150,8 +151,8 @@ public class FillTest { // list 后面还有个统计 想办法手动写入 // 这里偷懒直接用list 也可以用对象 - List> totalListList = new ArrayList>(); - List totalList = new ArrayList(); + List> totalListList = ListUtils.newArrayList(); + List totalList = ListUtils.newArrayList(); totalListList.add(totalList); totalList.add(null); totalList.add(null); @@ -227,7 +228,7 @@ public class FillTest { } private List data() { - List list = new ArrayList(); + List list = ListUtils.newArrayList(); for (int i = 0; i < 10; i++) { FillData fillData = new FillData(); list.add(fillData); diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataDemoHeadDataListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataDemoHeadDataListener.java index aba35665..b1ef3d7e 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataDemoHeadDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataDemoHeadDataListener.java @@ -1,48 +1,48 @@ package com.alibaba.easyexcel.test.demo.read; -import java.util.ArrayList; import java.util.List; import com.alibaba.excel.context.AnalysisContext; -import com.alibaba.excel.event.AnalysisEventListener; +import com.alibaba.excel.read.listener.ReadListener; +import com.alibaba.excel.util.ListUtils; import com.alibaba.fastjson.JSON; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import lombok.extern.slf4j.Slf4j; /** * 读取头 * * @author Jiaju Zhuang */ -public class CellDataDemoHeadDataListener extends AnalysisEventListener { - private static final Logger LOGGER = LoggerFactory.getLogger(CellDataDemoHeadDataListener.class); +@Slf4j +public class CellDataDemoHeadDataListener implements ReadListener { /** * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 */ private static final int BATCH_COUNT = 100; - List list = new ArrayList(); + + private List cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); @Override public void invoke(CellDataReadDemoData data, AnalysisContext context) { - LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); - if (list.size() >= BATCH_COUNT) { + log.info("解析到一条数据:{}", JSON.toJSONString(data)); + if (cachedDataList.size() >= BATCH_COUNT) { saveData(); - list.clear(); + cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); } } @Override public void doAfterAllAnalysed(AnalysisContext context) { saveData(); - LOGGER.info("所有数据解析完成!"); + log.info("所有数据解析完成!"); } /** * 加上存储数据库 */ private void saveData() { - LOGGER.info("{}条数据,开始存储数据库!", list.size()); - LOGGER.info("存储数据库成功!"); + log.info("{}条数据,开始存储数据库!", cachedDataList.size()); + log.info("存储数据库成功!"); } } diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterDataListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterDataListener.java index d4717468..9dabbc86 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterDataListener.java @@ -1,49 +1,49 @@ package com.alibaba.easyexcel.test.demo.read; -import java.util.ArrayList; import java.util.List; import com.alibaba.excel.context.AnalysisContext; -import com.alibaba.excel.event.AnalysisEventListener; +import com.alibaba.excel.read.listener.ReadListener; +import com.alibaba.excel.util.ListUtils; import com.alibaba.fastjson.JSON; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import lombok.extern.slf4j.Slf4j; /** * 模板的读取类 * * @author Jiaju Zhuang */ -public class ConverterDataListener extends AnalysisEventListener { - private static final Logger LOGGER = LoggerFactory.getLogger(ConverterDataListener.class); +@Slf4j +public class ConverterDataListener implements ReadListener { + /** * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 */ private static final int BATCH_COUNT = 5; - List list = new ArrayList(); + private List cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); @Override public void invoke(ConverterData data, AnalysisContext context) { - LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); - list.add(data); - if (list.size() >= BATCH_COUNT) { + log.info("解析到一条数据:{}", JSON.toJSONString(data)); + cachedDataList.add(data); + if (cachedDataList.size() >= BATCH_COUNT) { saveData(); - list.clear(); + cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); } } @Override public void doAfterAllAnalysed(AnalysisContext context) { saveData(); - LOGGER.info("所有数据解析完成!"); + log.info("所有数据解析完成!"); } /** * 加上存储数据库 */ private void saveData() { - LOGGER.info("{}条数据,开始存储数据库!", list.size()); - LOGGER.info("存储数据库成功!"); + log.info("{}条数据,开始存储数据库!", cachedDataList.size()); + log.info("存储数据库成功!"); } } diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/CustomStringStringConverter.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/CustomStringStringConverter.java index 406b2896..dabe57a8 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/CustomStringStringConverter.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/CustomStringStringConverter.java @@ -1,11 +1,10 @@ package com.alibaba.easyexcel.test.demo.read; import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.converters.ReadConverterContext; +import com.alibaba.excel.converters.WriteConverterContext; import com.alibaba.excel.enums.CellDataTypeEnum; -import com.alibaba.excel.metadata.GlobalConfiguration; -import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.data.WriteCellData; -import com.alibaba.excel.metadata.property.ExcelContentProperty; /** * String and string converter @@ -26,35 +25,22 @@ public class CustomStringStringConverter implements Converter { /** * 这里读的时候会调用 * - * @param cellData - * NotNull - * @param contentProperty - * Nullable - * @param globalConfiguration - * NotNull + * @param context * @return */ @Override - public String convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, - GlobalConfiguration globalConfiguration) { - return "自定义:" + cellData.getStringValue(); + public String convertToJavaData(ReadConverterContext context) { + return "自定义:" + context.getReadCellData().getStringValue(); } /** * 这里是写的时候会调用 不用管 * - * @param value - * NotNull - * @param contentProperty - * Nullable - * @param globalConfiguration - * NotNull * @return */ @Override - public WriteCellData convertToExcelData(String value, ExcelContentProperty contentProperty, - GlobalConfiguration globalConfiguration) { - return new WriteCellData<>(value); + public WriteCellData convertToExcelData(WriteConverterContext context) { + return new WriteCellData<>(context.getValue()); } } diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java index 4764f717..80d27b26 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java @@ -1,14 +1,13 @@ package com.alibaba.easyexcel.test.demo.read; -import java.util.ArrayList; import java.util.List; import com.alibaba.excel.context.AnalysisContext; -import com.alibaba.excel.event.AnalysisEventListener; +import com.alibaba.excel.read.listener.ReadListener; +import com.alibaba.excel.util.ListUtils; import com.alibaba.fastjson.JSON; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import lombok.extern.slf4j.Slf4j; /** * 模板的读取类 @@ -16,8 +15,9 @@ import org.slf4j.LoggerFactory; * @author Jiaju Zhuang */ // 有个很重要的点 DemoDataListener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去 -public class DemoDataListener extends AnalysisEventListener { - private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class); +@Slf4j +public class DemoDataListener implements ReadListener { + /** * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 */ @@ -25,7 +25,7 @@ public class DemoDataListener extends AnalysisEventListener { /** * 缓存的数据 */ - private List list = new ArrayList<>(BATCH_COUNT); + private List cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); /** * 假设这个是一个DAO,当然有业务逻辑这个也可以是一个service。当然如果不用存储这个对象没用。 */ @@ -53,13 +53,13 @@ public class DemoDataListener extends AnalysisEventListener { */ @Override public void invoke(DemoData data, AnalysisContext context) { - LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); - list.add(data); + log.info("解析到一条数据:{}", JSON.toJSONString(data)); + cachedDataList.add(data); // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM - if (list.size() >= BATCH_COUNT) { + if (cachedDataList.size() >= BATCH_COUNT) { saveData(); // 存储完成清理 list - list = new ArrayList<>(BATCH_COUNT); + cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); } } @@ -72,15 +72,15 @@ public class DemoDataListener extends AnalysisEventListener { public void doAfterAllAnalysed(AnalysisContext context) { // 这里也要保存数据,确保最后遗留的数据也存储到数据库 saveData(); - LOGGER.info("所有数据解析完成!"); + log.info("所有数据解析完成!"); } /** * 加上存储数据库 */ private void saveData() { - LOGGER.info("{}条数据,开始存储数据库!", list.size()); - demoDAO.save(list); - LOGGER.info("存储数据库成功!"); + log.info("{}条数据,开始存储数据库!", cachedDataList.size()); + demoDAO.save(cachedDataList); + log.info("存储数据库成功!"); } } diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExceptionListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExceptionListener.java index b6dfdb4e..26d1c8de 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExceptionListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExceptionListener.java @@ -1,29 +1,30 @@ package com.alibaba.easyexcel.test.demo.read; -import java.util.ArrayList; import java.util.List; import java.util.Map; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.excel.context.AnalysisContext; -import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.exception.ExcelDataConvertException; +import com.alibaba.excel.metadata.data.ReadCellData; +import com.alibaba.excel.read.listener.ReadListener; +import com.alibaba.excel.util.ListUtils; import com.alibaba.fastjson.JSON; +import lombok.extern.slf4j.Slf4j; + /** * 读取转换异常 * * @author Jiaju Zhuang */ -public class DemoExceptionListener extends AnalysisEventListener { - private static final Logger LOGGER = LoggerFactory.getLogger(DemoExceptionListener.class); +@Slf4j +public class DemoExceptionListener implements ReadListener { /** * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 */ private static final int BATCH_COUNT = 5; - List list = new ArrayList(); + + private List cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); /** * 在转换异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则 继续读取下一行。 @@ -34,12 +35,12 @@ public class DemoExceptionListener extends AnalysisEventListener headMap, AnalysisContext context) { - LOGGER.info("解析到一条头数据:{}", JSON.toJSONString(headMap)); + public void invokeHead(Map> headMap, AnalysisContext context) { + log.info("解析到一条头数据:{}", JSON.toJSONString(headMap)); } @Override public void invoke(ExceptionDemoData data, AnalysisContext context) { - LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); - if (list.size() >= BATCH_COUNT) { + log.info("解析到一条数据:{}", JSON.toJSONString(data)); + if (cachedDataList.size() >= BATCH_COUNT) { saveData(); - list.clear(); + cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); } } @Override public void doAfterAllAnalysed(AnalysisContext context) { saveData(); - LOGGER.info("所有数据解析完成!"); + log.info("所有数据解析完成!"); } /** * 加上存储数据库 */ private void saveData() { - LOGGER.info("{}条数据,开始存储数据库!", list.size()); - LOGGER.info("存储数据库成功!"); + log.info("{}条数据,开始存储数据库!", cachedDataList.size()); + log.info("存储数据库成功!"); } } diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraListener.java index df2afc36..e9dcc199 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraListener.java @@ -1,21 +1,20 @@ package com.alibaba.easyexcel.test.demo.read; import com.alibaba.excel.context.AnalysisContext; -import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.metadata.CellExtra; +import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.fastjson.JSON; +import lombok.extern.slf4j.Slf4j; import org.junit.Assert; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * 读取单元格的批注 * * @author Jiaju Zhuang **/ -public class DemoExtraListener extends AnalysisEventListener { - private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class); +@Slf4j +public class DemoExtraListener implements ReadListener { @Override public void invoke(DemoExtraData data, AnalysisContext context) {} @@ -25,18 +24,18 @@ public class DemoExtraListener extends AnalysisEventListener { @Override public void extra(CellExtra extra, AnalysisContext context) { - LOGGER.info("读取到了一条额外信息:{}", JSON.toJSONString(extra)); + log.info("读取到了一条额外信息:{}", JSON.toJSONString(extra)); switch (extra.getType()) { case COMMENT: - LOGGER.info("额外信息是批注,在rowIndex:{},columnIndex;{},内容是:{}", extra.getRowIndex(), extra.getColumnIndex(), + log.info("额外信息是批注,在rowIndex:{},columnIndex;{},内容是:{}", extra.getRowIndex(), extra.getColumnIndex(), extra.getText()); break; case HYPERLINK: if ("Sheet1!A1".equals(extra.getText())) { - LOGGER.info("额外信息是超链接,在rowIndex:{},columnIndex;{},内容是:{}", extra.getRowIndex(), + log.info("额外信息是超链接,在rowIndex:{},columnIndex;{},内容是:{}", extra.getRowIndex(), extra.getColumnIndex(), extra.getText()); } else if ("Sheet2!A1".equals(extra.getText())) { - LOGGER.info( + log.info( "额外信息是超链接,而且覆盖了一个区间,在firstRowIndex:{},firstColumnIndex;{},lastRowIndex:{},lastColumnIndex:{}," + "内容是:{}", extra.getFirstRowIndex(), extra.getFirstColumnIndex(), extra.getLastRowIndex(), @@ -46,7 +45,7 @@ public class DemoExtraListener extends AnalysisEventListener { } break; case MERGE: - LOGGER.info( + log.info( "额外信息是超链接,而且覆盖了一个区间,在firstRowIndex:{},firstColumnIndex;{},lastRowIndex:{},lastColumnIndex:{}", extra.getFirstRowIndex(), extra.getFirstColumnIndex(), extra.getLastRowIndex(), extra.getLastColumnIndex()); diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoHeadDataListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoHeadDataListener.java index 61bd834b..ffde2b2c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoHeadDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoHeadDataListener.java @@ -1,29 +1,29 @@ package com.alibaba.easyexcel.test.demo.read; -import java.util.ArrayList; import java.util.List; import java.util.Map; import com.alibaba.excel.context.AnalysisContext; -import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.exception.ExcelDataConvertException; +import com.alibaba.excel.metadata.data.ReadCellData; +import com.alibaba.excel.read.listener.ReadListener; +import com.alibaba.excel.util.ListUtils; import com.alibaba.fastjson.JSON; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import lombok.extern.slf4j.Slf4j; /** * 读取头 * * @author Jiaju Zhuang */ -public class DemoHeadDataListener extends AnalysisEventListener { - private static final Logger LOGGER = LoggerFactory.getLogger(DemoHeadDataListener.class); +@Slf4j +public class DemoHeadDataListener implements ReadListener { /** * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 */ private static final int BATCH_COUNT = 5; - List list = new ArrayList(); + private List cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); /** * 在转换异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则 继续读取下一行。 @@ -34,10 +34,10 @@ public class DemoHeadDataListener extends AnalysisEventListener { */ @Override public void onException(Exception exception, AnalysisContext context) { - LOGGER.error("解析失败,但是继续解析下一行:{}", exception.getMessage()); + log.error("解析失败,但是继续解析下一行:{}", exception.getMessage()); if (exception instanceof ExcelDataConvertException) { ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException)exception; - LOGGER.error("第{}行,第{}列解析异常,数据为:{}", excelDataConvertException.getRowIndex(), + log.error("第{}行,第{}列解析异常,数据为:{}", excelDataConvertException.getRowIndex(), excelDataConvertException.getColumnIndex(), excelDataConvertException.getCellData()); } } @@ -49,30 +49,33 @@ public class DemoHeadDataListener extends AnalysisEventListener { * @param context */ @Override - public void invokeHeadMap(Map headMap, AnalysisContext context) { - LOGGER.info("解析到一条头数据:{}", JSON.toJSONString(headMap)); + public void invokeHead(Map> headMap, AnalysisContext context) { + log.info("解析到一条头数据:{}", JSON.toJSONString(headMap)); + // 如果想转成成 Map + // 方案1: 不要implements ReadListener 而是 extends AnalysisEventListener + // 方案2: 调用 ConverterUtils.convertToStringMap(headMap, context) 自动会转换 } @Override public void invoke(DemoData data, AnalysisContext context) { - LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); - if (list.size() >= BATCH_COUNT) { + log.info("解析到一条数据:{}", JSON.toJSONString(data)); + if (cachedDataList.size() >= BATCH_COUNT) { saveData(); - list.clear(); + cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); } } @Override public void doAfterAllAnalysed(AnalysisContext context) { saveData(); - LOGGER.info("所有数据解析完成!"); + log.info("所有数据解析完成!"); } /** * 加上存储数据库 */ private void saveData() { - LOGGER.info("{}条数据,开始存储数据库!", list.size()); - LOGGER.info("存储数据库成功!"); + log.info("{}条数据,开始存储数据库!", cachedDataList.size()); + log.info("存储数据库成功!"); } } diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameDataListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameDataListener.java index b8526ef1..d00d44df 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameDataListener.java @@ -1,49 +1,48 @@ package com.alibaba.easyexcel.test.demo.read; -import java.util.ArrayList; import java.util.List; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; +import com.alibaba.excel.util.ListUtils; import com.alibaba.fastjson.JSON; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import lombok.extern.slf4j.Slf4j; /** * 模板的读取类 * * @author Jiaju Zhuang */ +@Slf4j public class IndexOrNameDataListener extends AnalysisEventListener { - private static final Logger LOGGER = LoggerFactory.getLogger(IndexOrNameDataListener.class); /** * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 */ private static final int BATCH_COUNT = 5; - List list = new ArrayList(); + private List cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); @Override public void invoke(IndexOrNameData data, AnalysisContext context) { - LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); - list.add(data); - if (list.size() >= BATCH_COUNT) { + log.info("解析到一条数据:{}", JSON.toJSONString(data)); + cachedDataList.add(data); + if (cachedDataList.size() >= BATCH_COUNT) { saveData(); - list.clear(); + cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); } } @Override public void doAfterAllAnalysed(AnalysisContext context) { saveData(); - LOGGER.info("所有数据解析完成!"); + log.info("所有数据解析完成!"); } /** * 加上存储数据库 */ private void saveData() { - LOGGER.info("{}条数据,开始存储数据库!", list.size()); - LOGGER.info("存储数据库成功!"); + log.info("{}条数据,开始存储数据库!", cachedDataList.size()); + log.info("存储数据库成功!"); } } diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/NoModelDataListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/NoModelDataListener.java index 761b50f4..13de38d2 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/NoModelDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/NoModelDataListener.java @@ -1,50 +1,49 @@ package com.alibaba.easyexcel.test.demo.read; -import java.util.ArrayList; import java.util.List; import java.util.Map; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; +import com.alibaba.excel.util.ListUtils; import com.alibaba.fastjson.JSON; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import lombok.extern.slf4j.Slf4j; /** * 直接用map接收数据 * * @author Jiaju Zhuang */ +@Slf4j public class NoModelDataListener extends AnalysisEventListener> { - private static final Logger LOGGER = LoggerFactory.getLogger(NoModelDataListener.class); /** * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 */ private static final int BATCH_COUNT = 5; - List> list = new ArrayList>(); + private List> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); @Override public void invoke(Map data, AnalysisContext context) { - LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); - list.add(data); - if (list.size() >= BATCH_COUNT) { + log.info("解析到一条数据:{}", JSON.toJSONString(data)); + cachedDataList.add(data); + if (cachedDataList.size() >= BATCH_COUNT) { saveData(); - list.clear(); + cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); } } @Override public void doAfterAllAnalysed(AnalysisContext context) { saveData(); - LOGGER.info("所有数据解析完成!"); + log.info("所有数据解析完成!"); } /** * 加上存储数据库 */ private void saveData() { - LOGGER.info("{}条数据,开始存储数据库!", list.size()); - LOGGER.info("存储数据库成功!"); + log.info("{}条数据,开始存储数据库!", cachedDataList.size()); + log.info("存储数据库成功!"); } } diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java index 5fe67e4a..a97d2c1e 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java @@ -1,7 +1,6 @@ package com.alibaba.easyexcel.test.demo.read; import java.io.File; -import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -17,12 +16,12 @@ import com.alibaba.excel.enums.CellExtraTypeEnum; import com.alibaba.excel.read.listener.PageReadListener; import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.metadata.ReadSheet; +import com.alibaba.excel.util.ListUtils; import com.alibaba.fastjson.JSON; +import lombok.extern.slf4j.Slf4j; import org.junit.Ignore; import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * 读的常见写法 @@ -30,10 +29,9 @@ import org.slf4j.LoggerFactory; * @author Jiaju Zhuang */ @Ignore +@Slf4j public class ReadTest { - private static final Logger LOGGER = LoggerFactory.getLogger(ReadTest.class); - /** * 最简单的读 *

@@ -52,7 +50,7 @@ public class ReadTest { // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行 EasyExcel.read(fileName, DemoData.class, new PageReadListener(dataList -> { for (DemoData demoData : dataList) { - LOGGER.info("读取到一条数据{}", JSON.toJSONString(demoData)); + log.info("读取到一条数据{}", JSON.toJSONString(demoData)); } })).sheet().doRead(); @@ -68,15 +66,15 @@ public class ReadTest { /** *临时存储 */ - private List cachedData = new ArrayList<>(BATCH_COUNT); + private List cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); @Override public void invoke(DemoData data, AnalysisContext context) { - cachedData.add(data); - if (cachedData.size() >= BATCH_COUNT) { + cachedDataList.add(data); + if (cachedDataList.size() >= BATCH_COUNT) { saveData(); // 存储完成清理 list - cachedData = new ArrayList<>(BATCH_COUNT); + cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); } } @@ -89,8 +87,8 @@ public class ReadTest { * 加上存储数据库 */ private void saveData() { - LOGGER.info("{}条数据,开始存储数据库!", cachedData.size()); - LOGGER.info("存储数据库成功!"); + log.info("{}条数据,开始存储数据库!", cachedDataList.size()); + log.info("存储数据库成功!"); } }).sheet().doRead(); @@ -304,14 +302,14 @@ public class ReadTest { // 这里 需要指定读用哪个class去读,然后读取第一个sheet 同步读取会自动finish List list = EasyExcel.read(fileName).head(DemoData.class).sheet().doReadSync(); for (DemoData data : list) { - LOGGER.info("读取到数据:{}", JSON.toJSONString(data)); + log.info("读取到数据:{}", JSON.toJSONString(data)); } // 这里 也可以不指定class,返回一个list,然后读取第一个sheet 同步读取会自动finish List> listMap = EasyExcel.read(fileName).sheet().doReadSync(); for (Map data : listMap) { // 返回每条数据的键值对 表示所在的列 和所在列的值 - LOGGER.info("读取到数据:{}", JSON.toJSONString(data)); + log.info("读取到数据:{}", JSON.toJSONString(data)); } } diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java index 936e2f17..54218355 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java @@ -1,29 +1,27 @@ package com.alibaba.easyexcel.test.demo.web; -import java.util.ArrayList; import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.excel.context.AnalysisContext; -import com.alibaba.excel.event.AnalysisEventListener; +import com.alibaba.excel.read.listener.ReadListener; +import com.alibaba.excel.util.ListUtils; import com.alibaba.fastjson.JSON; +import lombok.extern.slf4j.Slf4j; + /** * 模板的读取类 * * @author Jiaju Zhuang */ // 有个很重要的点 DemoDataListener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去 -public class UploadDataListener extends AnalysisEventListener { - private static final Logger LOGGER = - LoggerFactory.getLogger(com.alibaba.easyexcel.test.demo.read.DemoDataListener.class); +@Slf4j +public class UploadDataListener implements ReadListener { /** * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 */ private static final int BATCH_COUNT = 5; - List list = new ArrayList(); + private List cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); /** * 假设这个是一个DAO,当然有业务逻辑这个也可以是一个service。当然如果不用存储这个对象没用。 */ @@ -46,19 +44,18 @@ public class UploadDataListener extends AnalysisEventListener { /** * 这个每一条数据解析都会来调用 * - * @param data - * one row value. Is is same as {@link AnalysisContext#readRowHolder()} + * @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()} * @param context */ @Override public void invoke(UploadData data, AnalysisContext context) { - LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); - list.add(data); + log.info("解析到一条数据:{}", JSON.toJSONString(data)); + cachedDataList.add(data); // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM - if (list.size() >= BATCH_COUNT) { + if (cachedDataList.size() >= BATCH_COUNT) { saveData(); // 存储完成清理 list - list.clear(); + cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); } } @@ -71,15 +68,15 @@ public class UploadDataListener extends AnalysisEventListener { public void doAfterAllAnalysed(AnalysisContext context) { // 这里也要保存数据,确保最后遗留的数据也存储到数据库 saveData(); - LOGGER.info("所有数据解析完成!"); + log.info("所有数据解析完成!"); } /** * 加上存储数据库 */ private void saveData() { - LOGGER.info("{}条数据,开始存储数据库!", list.size()); - uploadDAO.save(list); - LOGGER.info("存储数据库成功!"); + log.info("{}条数据,开始存储数据库!", cachedDataList.size()); + uploadDAO.save(cachedDataList); + log.info("存储数据库成功!"); } } diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java index af2dfce5..6fd79db4 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java @@ -2,15 +2,15 @@ package com.alibaba.easyexcel.test.demo.web; import java.io.IOException; import java.net.URLEncoder; -import java.util.ArrayList; import java.util.Date; -import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletResponse; import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.util.ListUtils; +import com.alibaba.excel.util.MapUtils; import com.alibaba.fastjson.JSON; import org.springframework.beans.factory.annotation.Autowired; @@ -49,12 +49,7 @@ public class WebTest { String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); - ArrayList> list = new ArrayList<>(); - ArrayList sublist = new ArrayList(); - sublist.add("t1"); - sublist.add("t2"); - list.add(sublist); - EasyExcel.write(response.getOutputStream()).sheet("模板").doWrite(list); + EasyExcel.write(response.getOutputStream()).sheet("模板").doWrite(data()); } /** @@ -79,7 +74,7 @@ public class WebTest { response.reset(); response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); - Map map = new HashMap(); + Map map = MapUtils.newHashMap(); map.put("status", "failure"); map.put("message", "下载文件失败" + e.getMessage()); response.getWriter().println(JSON.toJSONString(map)); @@ -103,7 +98,7 @@ public class WebTest { } private List data() { - List list = new ArrayList(); + List list = ListUtils.newArrayList(); for (int i = 0; i < 10; i++) { DownloadData data = new DownloadData(); data.setString("字符串" + 0); diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomStringStringConverter.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomStringStringConverter.java index 5ea599be..fde0f96a 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomStringStringConverter.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomStringStringConverter.java @@ -1,11 +1,10 @@ package com.alibaba.easyexcel.test.demo.write; import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.converters.ReadConverterContext; +import com.alibaba.excel.converters.WriteConverterContext; import com.alibaba.excel.enums.CellDataTypeEnum; -import com.alibaba.excel.metadata.GlobalConfiguration; -import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.data.WriteCellData; -import com.alibaba.excel.metadata.property.ExcelContentProperty; /** * String and string converter @@ -26,29 +25,21 @@ public class CustomStringStringConverter implements Converter { /** * 这里是读的时候会调用 不用管 * - * @param cellData NotNull - * @param contentProperty Nullable - * @param globalConfiguration NotNull * @return */ @Override - public String convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, - GlobalConfiguration globalConfiguration) { - return cellData.getStringValue(); + public String convertToJavaData(ReadConverterContext context) { + return context.getReadCellData().getStringValue(); } /** * 这里是写的时候会调用 不用管 * - * @param value NotNull - * @param contentProperty Nullable - * @param globalConfiguration NotNull * @return */ @Override - public WriteCellData convertToExcelData(String value, ExcelContentProperty contentProperty, - GlobalConfiguration globalConfiguration) { - return new WriteCellData<>("自定义:" + value); + public WriteCellData convertToExcelData(WriteConverterContext context) { + return new WriteCellData<>("自定义:" + context.getValue()); } } 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 7af059f3..463d60c3 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 @@ -29,6 +29,7 @@ import com.alibaba.excel.metadata.data.RichTextStringData; import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.util.BooleanUtils; import com.alibaba.excel.util.FileUtils; +import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import com.alibaba.excel.write.merge.LoopMergeStrategy; @@ -263,18 +264,16 @@ public class WriteTest { @Test public void imageWrite() throws Exception { String fileName = TestFileUtil.getPath() + "imageWrite" + System.currentTimeMillis() + ".xlsx"; - // 如果使用流 记得关闭 - InputStream inputStream = null; - try { - List list = new ArrayList<>(); + + String imagePath = TestFileUtil.getPath() + "converter" + File.separator + "img.jpg"; + try (InputStream inputStream = FileUtils.openInputStream(new File(imagePath))) { + List list = ListUtils.newArrayList(); ImageDemoData imageDemoData = new ImageDemoData(); list.add(imageDemoData); - String imagePath = TestFileUtil.getPath() + "converter" + File.separator + "img.jpg"; // 放入五种类型的图片 实际使用只要选一种即可 imageDemoData.setByteArray(FileUtils.readFileToByteArray(new File(imagePath))); imageDemoData.setFile(new File(imagePath)); imageDemoData.setString(imagePath); - inputStream = FileUtils.openInputStream(new File(imagePath)); imageDemoData.setInputStream(inputStream); imageDemoData.setUrl(new URL( "https://raw.githubusercontent.com/alibaba/easyexcel/master/src/test/resources/converter/img.jpg")); @@ -328,10 +327,6 @@ public class WriteTest { // 写入数据 EasyExcel.write(fileName, ImageDemoData.class).sheet().doWrite(list); - } finally { - if (inputStream != null) { - inputStream.close(); - } } } @@ -733,7 +728,7 @@ public class WriteTest { } private List dataLong() { - List list = new ArrayList<>(); + List list = ListUtils.newArrayList(); for (int i = 0; i < 10; i++) { LongestMatchColumnWidthData data = new LongestMatchColumnWidthData(); data.setString("测试很长的字符串测试很长的字符串测试很长的字符串" + i); @@ -745,12 +740,12 @@ public class WriteTest { } private List> variableTitleHead() { - List> list = new ArrayList<>(); - List head0 = new ArrayList<>(); + List> list = ListUtils.newArrayList(); + List head0 = ListUtils.newArrayList(); head0.add("string" + System.currentTimeMillis()); - List head1 = new ArrayList<>(); + List head1 = ListUtils.newArrayList(); head1.add("number" + System.currentTimeMillis()); - List head2 = new ArrayList<>(); + List head2 = ListUtils.newArrayList(); head2.add("date" + System.currentTimeMillis()); list.add(head0); list.add(head1); @@ -759,12 +754,12 @@ public class WriteTest { } private List> head() { - List> list = new ArrayList>(); - List head0 = new ArrayList(); + List> list = ListUtils.newArrayList(); + List head0 = ListUtils.newArrayList(); head0.add("字符串" + System.currentTimeMillis()); - List head1 = new ArrayList(); + List head1 = ListUtils.newArrayList(); head1.add("数字" + System.currentTimeMillis()); - List head2 = new ArrayList(); + List head2 = ListUtils.newArrayList(); head2.add("日期" + System.currentTimeMillis()); list.add(head0); list.add(head1); @@ -773,9 +768,9 @@ public class WriteTest { } private List> dataList() { - List> list = new ArrayList<>(); + List> list = ListUtils.newArrayList(); for (int i = 0; i < 10; i++) { - List data = new ArrayList<>(); + List data = ListUtils.newArrayList(); data.add("字符串" + i); data.add(new Date()); data.add(0.56); @@ -785,7 +780,7 @@ public class WriteTest { } private List data() { - List list = new ArrayList<>(); + List list = ListUtils.newArrayList(); for (int i = 0; i < 10; i++) { DemoData data = new DemoData(); data.setString("字符串" + i); diff --git a/update.md b/update.md index 8a695624..30e9fd0e 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,6 @@ +# 3.0.5 +* 修复`ReadListener` 转换异常不抛出的问题 + # 3.0.4 * 调整读写默认大小,防止大批量写的时候可能会full gc * `fill`的情况新增 `afterRowDispose`事件