diff --git a/README.md b/README.md index 004f12c7..fe7d634a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,11 @@ +# 🔥作者最新开源项目[Chat2DB](https://github.com/chat2db/Chat2DB) + +- GitHub地址 [https://github.com/chat2db/Chat2DB](https://github.com/chat2db/Chat2DB) +- 官 网 地 址 [https://chat2db.ai](https://chat2db.ai) + +crete datasource + + EasyExcel ====================== [![Build Status](https://github.com/alibaba/easyexcel/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/alibaba/easyexcel/actions/workflows/ci.yml?query=branch%3Amaster) @@ -28,7 +36,7 @@ easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析 com.alibaba easyexcel - 3.2.1 + 3.3.2 ``` @@ -37,33 +45,6 @@ easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析 开源不易,如果觉得EasyExcel对您的工作还是有帮助的话,请帮忙在github star 的右上角点个⭐Star,您的支持是使EasyExcel变得更好最大的动力。 -# 人员招聘 - -由于工作较忙,需要招聘一些同学加入`EasyExcel`开源,一起跟着`EasyExcel`成长。 - -## 你将获得 - -* 认识一帮热爱开源的小伙伴 -* 你写的代码可以被无数人看到,提升自我编码能力 -* 可能会有一定的物质奖励(在和公司申请,不一定能过) - -## 你的工作 - -* 群&`issue`答疑 -* 做一些代码的`PR`合并去修复bug -* 讨论`EasyExcel`规划 - -## 招聘要求 - -* 3年以上Java编程经验,基础扎实 -* 对技术比较热爱,并且喜欢阅读源码 -* 自驱力强,能主动的研究一些问题 -* 需要持之以恒,开源需要长期维护 - -## 联系方式 - -直接加钉钉群,联系是仪即可 - # 如何获取帮助 ## 优先建议自己通过文档来解决问题 @@ -83,30 +64,16 @@ easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析 不管`github`、`gitee`都会定期有人回答您的问题,比较紧急可以在提完`issue`以后在钉钉群艾特群主并发送`issue`地址帮忙解决。 `QQ` 公司不让用,有时候也会去看,但是核心肯定还是在钉钉。 -## 也可以加入钉钉&QQ群来解决问题 - -加入钉钉或QQ群,看完公告可以获得帮助 。 -比较推荐钉钉群,`QQ` 公司不让用,当然QQ群也会有热心网友帮忙解决。 -[QQ1群(已满): 662022184](https://jq.qq.com/?_wv=1027&k=1T21jJxh) -[QQ2群(已满): 1097936804](https://jq.qq.com/?_wv=1027&k=j5zEy6Xl) -[QQ3群(已满): 453928496](https://qm.qq.com/cgi-bin/qm/qr?k=e2ULsA5A0GldhV2CXJ8sIbAyu9I6qqs7&jump_from=webapi) -[QQ4群(已满): 496594404](https://qm.qq.com/cgi-bin/qm/qr?k=e_aVG1Q7gi0PJUBkbrUGAgbeO3kUEInK&jump_from=webapi) -[QQ5群(已满): 451925680](https://jq.qq.com/?_wv=1027&k=6VHhvxyf) -[QQ6群: 784741035](https://jq.qq.com/?_wv=1027&k=BbLBIo9P) -[钉钉1群(已满): 21960511](https://qr.dingtalk.com/action/joingroup?code=v1,k1,cchz6k12ci9B08NNqhNRFGXocNVHrZtW0kaOtTKg/Rk=&_dt_no_comment=1&origin=11) -[钉钉2群(已满): 32796397](https://qr.dingtalk.com/action/joingroup?code=v1,k1,jyU9GtEuNU5S0QTyklqYcYJ8qDZtUuTPMM7uPZTS8Hs=&_dt_no_comment=1&origin=11) -[钉钉3群(已满): 33797247](https://qr.dingtalk.com/action/joingroup?code=v1,k1,3UGlEScTGQaHpW2cIRo+gkxJ9EVZ5fz26M6nW3uFP30=&_dt_no_comment=1&origin=11) -[钉钉4群(已满): 33491624](https://qr.dingtalk.com/action/joingroup?code=v1,k1,V14Pb65Too70rQkEaJ9ohb6lZBZbtp6jIL/q9EWh9vA=&_dt_no_comment=1&origin=11) -[钉钉5群(已满): 32134498](https://h5.dingtalk.com/circle/healthCheckin.html?dtaction=os&corpId=dingb9fa1325d9dccc3ecac589edd02f1650&5233a=71a83&cbdbhh=qwertyuiop) -[钉钉6群(已满): 34707941](https://h5.dingtalk.com/circle/healthCheckin.html?dtaction=os&corpId=dingcf68008a1d443ac012d5427bdb061b7a&6ae36c3d-0c80-4=22398493-6c2a-4&cbdbhh=qwertyuiop) -[钉钉7群(已满): 35235427](https://h5.dingtalk.com/circle/healthCheckin.html?dtaction=os&corpId=ding532b9018c06c7fc8660273c4b78e6440&167fb=ed003&cbdbhh=qwertyuiop) -[钉钉8群(已满): 44752220](https://h5.dingtalk.com/circle/healthCheckin.html?dtaction=os&corpId=dingea96808beee421693fd4ba7542d6e5da&0380092a-fa46=a6a40905-7951&cbdbhh=qwertyuiop) -[钉钉9群(已满): 11045002277](https://h5.dingtalk.com/circle/healthCheckin.html?dtaction=os&corpId=dinge338d2215891c0459c13cd6b2cb108a6&6972d=b92f9&cbdbhh=qwertyuiop) -[钉钉10群: 27360019755](https://qr.dingtalk.com/action/joingroup?code=v1,k1,v25LHn2liWmrWUKlkhIzOTcK7s7onp/sZP8mO5oIYSs=&_dt_no_comment=1&origin=11) +## 关注作者:程序员小獭 + +可以加群交流 + +![qrcode_for_gh_c43212c8d0ed_258](https://github.com/alibaba/easyexcel/assets/22975773/13ceff34-d547-421b-b9a9-04f388792099) + # 维护者 -姬朋飞(玉霄)、庄家钜、怀宇 +姬朋飞(玉霄)、庄家钜 # 快速开始 diff --git a/README_EN.md b/README_EN.md index 619f4161..10135fed 100644 --- a/README_EN.md +++ b/README_EN.md @@ -122,7 +122,7 @@ DEMO:[https://github.com/alibaba/easyexcel/blob/master/src/test/java/com/aliba * 1. Create an entity object, refer to{@link DownloadData}. * Each property of the entity object corresponds to a specific field of Excel * 2. Specify the returned properties - * 3. Invoke wirte function, then the OutputStream is automatically closed when it ends. + * 3. Invoke write function, then the OutputStream is automatically closed when it ends. *

*/ @GetMapping("download") diff --git a/easyexcel-core/pom.xml b/easyexcel-core/pom.xml index 93d013cc..f8179003 100644 --- a/easyexcel-core/pom.xml +++ b/easyexcel-core/pom.xml @@ -36,5 +36,9 @@ org.ehcache ehcache + + commons-io + commons-io + diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java index 05d4fa84..8421a160 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java @@ -1,8 +1,5 @@ package com.alibaba.excel.analysis; -import java.io.InputStream; -import java.util.List; - import com.alibaba.excel.analysis.csv.CsvExcelReadExecutor; import com.alibaba.excel.analysis.v03.XlsSaxAnalyser; import com.alibaba.excel.analysis.v07.XlsxSaxAnalyser; @@ -22,11 +19,11 @@ import com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder; import com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder; import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder; import com.alibaba.excel.support.ExcelTypeEnum; +import com.alibaba.excel.util.ClassUtils; import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.util.NumberDataFormatterUtils; import com.alibaba.excel.util.StringUtils; - import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.poifs.crypt.Decryptor; @@ -36,6 +33,9 @@ import org.apache.poi.util.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.InputStream; +import java.util.List; + /** * @author jipengfei */ @@ -158,16 +158,16 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { } try { if ((readWorkbookHolder instanceof XlsxReadWorkbookHolder) - && ((XlsxReadWorkbookHolder)readWorkbookHolder).getOpcPackage() != null) { - ((XlsxReadWorkbookHolder)readWorkbookHolder).getOpcPackage().revert(); + && ((XlsxReadWorkbookHolder) readWorkbookHolder).getOpcPackage() != null) { + ((XlsxReadWorkbookHolder) readWorkbookHolder).getOpcPackage().revert(); } } catch (Throwable t) { throwable = t; } try { if ((readWorkbookHolder instanceof XlsReadWorkbookHolder) - && ((XlsReadWorkbookHolder)readWorkbookHolder).getPoifsFileSystem() != null) { - ((XlsReadWorkbookHolder)readWorkbookHolder).getPoifsFileSystem().close(); + && ((XlsReadWorkbookHolder) readWorkbookHolder).getPoifsFileSystem() != null) { + ((XlsReadWorkbookHolder) readWorkbookHolder).getPoifsFileSystem().close(); } } catch (Throwable t) { throwable = t; @@ -177,8 +177,9 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { // https://github.com/alibaba/easyexcel/issues/2309 try { if ((readWorkbookHolder instanceof CsvReadWorkbookHolder) - && ((CsvReadWorkbookHolder)readWorkbookHolder).getCsvParser() != null) { - ((CsvReadWorkbookHolder)readWorkbookHolder).getCsvParser().close(); + && ((CsvReadWorkbookHolder) readWorkbookHolder).getCsvParser() != null + && analysisContext.readWorkbookHolder().getAutoCloseStream()) { + ((CsvReadWorkbookHolder) readWorkbookHolder).getCsvParser().close(); } } catch (Throwable t) { throwable = t; @@ -212,6 +213,7 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { private void removeThreadLocalCache() { NumberDataFormatterUtils.removeThreadLocalCache(); DateUtils.removeThreadLocalCache(); + ClassUtils.removeThreadLocalCache(); } private void clearEncrypt03() { diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java index a1b7625d..6afabe34 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java @@ -1,6 +1,7 @@ package com.alibaba.excel.analysis.csv; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.nio.file.Files; import java.util.ArrayList; @@ -11,6 +12,7 @@ import java.util.Map; import com.alibaba.excel.analysis.ExcelReadExecutor; import com.alibaba.excel.context.csv.CsvReadContext; +import com.alibaba.excel.enums.ByteOrderMarkEnum; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.RowTypeEnum; import com.alibaba.excel.exception.ExcelAnalysisException; @@ -27,6 +29,7 @@ import org.apache.commons.collections4.MapUtils; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; +import org.apache.commons.io.input.BOMInputStream; /** * read executor @@ -82,17 +85,26 @@ public class CsvExcelReadExecutor implements ExcelReadExecutor { private CSVParser csvParser() throws IOException { CsvReadWorkbookHolder csvReadWorkbookHolder = csvReadContext.csvReadWorkbookHolder(); CSVFormat csvFormat = csvReadWorkbookHolder.getCsvFormat(); - + ByteOrderMarkEnum byteOrderMark = ByteOrderMarkEnum.valueOfByCharsetName( + csvReadContext.csvReadWorkbookHolder().getCharset().name()); if (csvReadWorkbookHolder.getMandatoryUseInputStream()) { - return csvFormat.parse( - new InputStreamReader(csvReadWorkbookHolder.getInputStream(), csvReadWorkbookHolder.getCharset())); + return buildCsvParser(csvFormat, csvReadWorkbookHolder.getInputStream(), byteOrderMark); } if (csvReadWorkbookHolder.getFile() != null) { - return csvFormat.parse(new InputStreamReader(Files.newInputStream(csvReadWorkbookHolder.getFile().toPath()), - csvReadWorkbookHolder.getCharset())); + return buildCsvParser(csvFormat, Files.newInputStream(csvReadWorkbookHolder.getFile().toPath()), + byteOrderMark); + } + return buildCsvParser(csvFormat, csvReadWorkbookHolder.getInputStream(), byteOrderMark); + } + + private CSVParser buildCsvParser(CSVFormat csvFormat, InputStream inputStream, ByteOrderMarkEnum byteOrderMark) + throws IOException { + if (byteOrderMark == null) { + return csvFormat.parse( + new InputStreamReader(inputStream, csvReadContext.csvReadWorkbookHolder().getCharset())); } - return csvFormat.parse( - new InputStreamReader(csvReadWorkbookHolder.getInputStream(), csvReadWorkbookHolder.getCharset())); + return csvFormat.parse(new InputStreamReader(new BOMInputStream(inputStream, byteOrderMark.getByteOrderMark()), + csvReadContext.csvReadWorkbookHolder().getCharset())); } private void dealRecord(CSVRecord record, int rowIndex) { diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java index 203db6c7..509430f8 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java @@ -1,11 +1,31 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + package com.alibaba.excel.analysis.v07.handlers.sax; -import org.xml.sax.Attributes; -import org.xml.sax.helpers.DefaultHandler; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import com.alibaba.excel.cache.ReadCache; import com.alibaba.excel.constant.ExcelXmlConstants; +import org.xml.sax.Attributes; +import org.xml.sax.helpers.DefaultHandler; + /** * Sax read sharedStringsTable.xml * @@ -13,6 +33,8 @@ import com.alibaba.excel.constant.ExcelXmlConstants; */ public class SharedStringsTableHandler extends DefaultHandler { + private static final Pattern UTF_PATTTERN = Pattern.compile("_x([0-9A-Fa-f]{4})_"); + /** * The final piece of data */ @@ -86,7 +108,7 @@ public class SharedStringsTableHandler extends DefaultHandler { if (currentData == null) { readCache.put(null); } else { - readCache.put(currentData.toString()); + readCache.put(utfDecode(currentData.toString())); } break; case ExcelXmlConstants.SHAREDSTRINGS_RPH_TAG: @@ -109,4 +131,51 @@ public class SharedStringsTableHandler extends DefaultHandler { } currentElementData.append(ch, start, length); } + + /** + * from poi XSSFRichTextString + * + * @param value the string to decode + * @return the decoded string or null if the input string is null + *

+ * For all characters which cannot be represented in XML as defined by the XML 1.0 specification, + * the characters are escaped using the Unicode numerical character representation escape character + * format _xHHHH_, where H represents a hexadecimal character in the character's value. + *

+ * Example: The Unicode character 0D is invalid in an XML 1.0 document, + * so it shall be escaped as _x000D_. + *

+ * See section 3.18.9 in the OOXML spec. + * @see org.apache.poi.xssf.usermodel.XSSFRichTextString#utfDecode(String) + */ + static String utfDecode(String value) { + if (value == null || !value.contains("_x")) { + return value; + } + + StringBuilder buf = new StringBuilder(); + Matcher m = UTF_PATTTERN.matcher(value); + int idx = 0; + while (m.find()) { + int pos = m.start(); + if (pos > idx) { + buf.append(value, idx, pos); + } + + String code = m.group(1); + int icode = Integer.decode("0x" + code); + buf.append((char)icode); + + idx = m.end(); + } + + // small optimization: don't go via StringBuilder if not necessary, + // the encodings are very rare, so we should almost always go via this shortcut. + if (idx == 0) { + return value; + } + + buf.append(value.substring(idx)); + return buf.toString(); + } } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/easyexcel-core/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index db892989..9fff8449 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -265,7 +265,7 @@ public class WriteContextImpl implements WriteContext { Head head = entry.getValue(); int columnIndex = entry.getKey(); ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null, - currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), head.getFieldName()); + currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), head.getFieldName(), currentWriteHolder); CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(this, row, rowIndex, head, columnIndex, relativeRowIndex, Boolean.TRUE, excelContentProperty); @@ -427,6 +427,7 @@ public class WriteContextImpl implements WriteContext { private void removeThreadLocalCache() { NumberDataFormatterUtils.removeThreadLocalCache(); DateUtils.removeThreadLocalCache(); + ClassUtils.removeThreadLocalCache(); } @Override diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java b/easyexcel-core/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java index 1b04d240..8c371932 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java @@ -115,8 +115,6 @@ public class DefaultConverterLoader { putAllConverter(new StringNumberConverter()); putAllConverter(new StringStringConverter()); putAllConverter(new StringErrorConverter()); - - putAllConverter(new BigIntegerStringConverter()); } private static void initDefaultWriteConverter() { @@ -154,7 +152,6 @@ public class DefaultConverterLoader { putWriteStringConverter(new LongStringConverter()); putWriteStringConverter(new ShortStringConverter()); putWriteStringConverter(new StringStringConverter()); - putWriteStringConverter(new BigIntegerStringConverter()); } /** diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/enums/ByteOrderMarkEnum.java b/easyexcel-core/src/main/java/com/alibaba/excel/enums/ByteOrderMarkEnum.java new file mode 100644 index 00000000..7c069099 --- /dev/null +++ b/easyexcel-core/src/main/java/com/alibaba/excel/enums/ByteOrderMarkEnum.java @@ -0,0 +1,66 @@ +package com.alibaba.excel.enums; + +import java.nio.charset.Charset; +import java.util.Map; + +import com.alibaba.excel.util.MapUtils; + +import lombok.Getter; +import org.apache.commons.io.ByteOrderMark; + +/** + * byte order mark + * + * @author Jiaju Zhuang + */ +@Getter +public enum ByteOrderMarkEnum { + + /** + * UTF_8 + */ + UTF_8(ByteOrderMark.UTF_8), + /** + * UTF_16BE + */ + UTF_16BE(ByteOrderMark.UTF_16BE), + /** + * UTF_16LE + */ + UTF_16LE(ByteOrderMark.UTF_16LE), + /** + * UTF_32BE + */ + UTF_32BE(ByteOrderMark.UTF_32BE), + /** + * UTF_32LE + */ + UTF_32LE(ByteOrderMark.UTF_32LE), + + ; + + final ByteOrderMark byteOrderMark; + final String stringPrefix; + + ByteOrderMarkEnum(ByteOrderMark byteOrderMark) { + this.byteOrderMark = byteOrderMark; + Charset charset = Charset.forName(byteOrderMark.getCharsetName()); + this.stringPrefix = new String(byteOrderMark.getBytes(), charset); + } + + /** + * store character aliases corresponding to `ByteOrderMark` prefix + */ + private static final Map CHARSET_BYTE_ORDER_MARK_MAP = MapUtils.newHashMap(); + + static { + for (ByteOrderMarkEnum value : ByteOrderMarkEnum.values()) { + CHARSET_BYTE_ORDER_MARK_MAP.put(value.getByteOrderMark().getCharsetName(), value); + } + } + + public static ByteOrderMarkEnum valueOfByCharsetName(String charsetName) { + return CHARSET_BYTE_ORDER_MARK_MAP.get(charsetName); + } + +} diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/enums/CacheLocationEnum.java b/easyexcel-core/src/main/java/com/alibaba/excel/enums/CacheLocationEnum.java new file mode 100644 index 00000000..e2deed52 --- /dev/null +++ b/easyexcel-core/src/main/java/com/alibaba/excel/enums/CacheLocationEnum.java @@ -0,0 +1,23 @@ +package com.alibaba.excel.enums; + +/** + * cache locaciton + * + * @author Jiaju Zhuang + **/ +public enum CacheLocationEnum { + /** + * The cache will be stored in {@code ThreadLocal}, and will be cleared when the excel read and write is completed. + */ + THREAD_LOCAL, + + /** + * The cache will not be cleared unless the app is stopped. + */ + MEMORY, + + /** + * No caching.It may lose some of performance. + */ + NONE; +} diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java b/easyexcel-core/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java index 839a8b4b..ac97c03b 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java @@ -83,6 +83,15 @@ public abstract class AbstractHolder implements ConfigurationHolder { globalConfiguration.setLocale(basicParameter.getLocale()); } + if (basicParameter.getFiledCacheLocation() == null) { + if (prentAbstractHolder != null) { + globalConfiguration.setFiledCacheLocation( + prentAbstractHolder.getGlobalConfiguration().getFiledCacheLocation()); + } + } else { + globalConfiguration.setFiledCacheLocation(basicParameter.getFiledCacheLocation()); + } + } @Override diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/metadata/AbstractParameterBuilder.java b/easyexcel-core/src/main/java/com/alibaba/excel/metadata/AbstractParameterBuilder.java index 319c5ab6..24211211 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/metadata/AbstractParameterBuilder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/metadata/AbstractParameterBuilder.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.Locale; import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CacheLocationEnum; import com.alibaba.excel.util.ListUtils; /** @@ -73,6 +74,18 @@ public abstract class AbstractParameterBuilder sortedFieldMap; + + /** + * Fields using the index attribute + */ + private Map indexFieldMap; +} diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/metadata/FieldWrapper.java b/easyexcel-core/src/main/java/com/alibaba/excel/metadata/FieldWrapper.java new file mode 100644 index 00000000..bfe9321e --- /dev/null +++ b/easyexcel-core/src/main/java/com/alibaba/excel/metadata/FieldWrapper.java @@ -0,0 +1,43 @@ +package com.alibaba.excel.metadata; + +import java.lang.reflect.Field; +import java.util.Map; +import java.util.Set; + +import com.alibaba.excel.annotation.ExcelProperty; + +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +/** + * filed wrapper + * + * @author Jiaju Zhuang + */ +@Getter +@Setter +@EqualsAndHashCode +@AllArgsConstructor +@NoArgsConstructor +public class FieldWrapper { + + /** + * field + */ + private Field field; + + /** + * The field name matching cglib + */ + private String fieldName; + + /** + * The name of the sheet header. + * + * @see ExcelProperty + */ + private String[] heads; +} diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java b/easyexcel-core/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java index 5cafe90c..fe823dad 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java @@ -2,6 +2,8 @@ package com.alibaba.excel.metadata; import java.util.Locale; +import com.alibaba.excel.enums.CacheLocationEnum; + import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; @@ -32,6 +34,7 @@ public class GlobalConfiguration { * used when formatting dates and numbers. */ private Locale locale; + /** * Whether to use scientific Format. * @@ -39,10 +42,18 @@ public class GlobalConfiguration { */ private Boolean useScientificFormat; + /** + * The cache used when parsing fields such as head. + * + * default is THREAD_LOCAL. + */ + private CacheLocationEnum filedCacheLocation; + public GlobalConfiguration() { this.autoTrim = Boolean.TRUE; this.use1904windowing = Boolean.FALSE; this.locale = Locale.getDefault(); this.useScientificFormat = Boolean.FALSE; + this.filedCacheLocation = CacheLocationEnum.THREAD_LOCAL; } } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java b/easyexcel-core/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java index 317569e0..5181583c 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java @@ -3,24 +3,30 @@ package com.alibaba.excel.metadata.csv; import java.io.Closeable; import java.io.IOException; import java.math.BigDecimal; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; import com.alibaba.excel.constant.BuiltinFormats; +import com.alibaba.excel.enums.ByteOrderMarkEnum; import com.alibaba.excel.enums.NumericCellTypeEnum; import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.ListUtils; +import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.NumberDataFormatterUtils; import com.alibaba.excel.util.StringUtils; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; +import org.apache.commons.io.ByteOrderMark; import org.apache.poi.ss.usermodel.AutoFilter; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellRange; @@ -53,6 +59,7 @@ import org.apache.poi.ss.util.PaneInformation; @Setter @EqualsAndHashCode public class CsvSheet implements Sheet, Closeable { + /** * workbook */ @@ -111,6 +118,13 @@ public class CsvSheet implements Sheet, Closeable { } rowCache = ListUtils.newArrayListWithExpectedSize(rowCacheCount); try { + if (csvWorkbook.getWithBom()) { + ByteOrderMarkEnum byteOrderMark = ByteOrderMarkEnum.valueOfByCharsetName( + csvWorkbook.getCharset().name()); + if (byteOrderMark != null) { + out.append(byteOrderMark.getStringPrefix()); + } + } csvPrinter = csvFormat.print(out); } catch (IOException e) { throw new ExcelGenerateException(e); diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java b/easyexcel-core/src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java index f381d482..31724d99 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java @@ -2,6 +2,7 @@ package com.alibaba.excel.metadata.csv; import java.io.IOException; import java.io.OutputStream; +import java.nio.charset.Charset; import java.util.Iterator; import java.util.List; import java.util.Locale; @@ -72,11 +73,25 @@ public class CsvWorkbook implements Workbook { */ private List csvCellStyleList; - public CsvWorkbook(Appendable out, Locale locale, Boolean use1904windowing, Boolean useScientificFormat) { + /** + * charset. + */ + private Charset charset; + + /** + * Set the encoding prefix in the csv file, otherwise the office may open garbled characters. + * Default true. + */ + private Boolean withBom; + + public CsvWorkbook(Appendable out, Locale locale, Boolean use1904windowing, Boolean useScientificFormat, + Charset charset, Boolean withBom) { this.out = out; this.locale = locale; this.use1904windowing = use1904windowing; this.useScientificFormat = useScientificFormat; + this.charset = charset; + this.withBom = withBom; } @Override diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java b/easyexcel-core/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java index 61240d31..ce00e2eb 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java @@ -9,6 +9,9 @@ import java.util.TreeMap; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.enums.HeadKindEnum; +import com.alibaba.excel.metadata.ConfigurationHolder; +import com.alibaba.excel.metadata.FieldCache; +import com.alibaba.excel.metadata.FieldWrapper; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Holder; import com.alibaba.excel.util.ClassUtils; @@ -51,22 +54,17 @@ public class ExcelHeadProperty { * Configuration header information */ private Map headMap; - /** - * Fields ignored - */ - private Map ignoreMap; - public ExcelHeadProperty(Holder holder, Class headClazz, List> head) { + public ExcelHeadProperty(ConfigurationHolder configurationHolder, Class headClazz, List> head) { this.headClazz = headClazz; headMap = new TreeMap<>(); - ignoreMap = MapUtils.newHashMap(); headKind = HeadKindEnum.NONE; headRowNumber = 0; if (head != null && !head.isEmpty()) { int headIndex = 0; for (int i = 0; i < head.size(); i++) { - if (holder instanceof AbstractWriteHolder) { - if (((AbstractWriteHolder)holder).ignore(null, i)) { + if (configurationHolder instanceof AbstractWriteHolder) { + if (((AbstractWriteHolder)configurationHolder).ignore(null, i)) { continue; } } @@ -76,7 +74,7 @@ public class ExcelHeadProperty { headKind = HeadKindEnum.STRING; } // convert headClazz to head - initColumnProperties(holder); + initColumnProperties(configurationHolder); initHeadRowNumber(); if (LOGGER.isDebugEnabled()) { @@ -104,24 +102,15 @@ public class ExcelHeadProperty { } } - private void initColumnProperties(Holder holder) { + private void initColumnProperties(ConfigurationHolder configurationHolder) { if (headClazz == null) { return; } - // Declared fields - Map sortedAllFieldMap = MapUtils.newTreeMap(); - Map indexFieldMap = MapUtils.newTreeMap(); - - boolean needIgnore = (holder instanceof AbstractWriteHolder) && ( - !CollectionUtils.isEmpty(((AbstractWriteHolder)holder).getExcludeColumnFieldNames()) || !CollectionUtils - .isEmpty(((AbstractWriteHolder)holder).getExcludeColumnIndexes()) || !CollectionUtils - .isEmpty(((AbstractWriteHolder)holder).getIncludeColumnFieldNames()) || !CollectionUtils - .isEmpty(((AbstractWriteHolder)holder).getIncludeColumnIndexes())); - - ClassUtils.declaredFields(headClazz, sortedAllFieldMap, indexFieldMap, ignoreMap, needIgnore, holder); + FieldCache fieldCache = ClassUtils.declaredFields(headClazz, configurationHolder); - for (Map.Entry entry : sortedAllFieldMap.entrySet()) { - initOneColumnProperty(entry.getKey(), entry.getValue(), indexFieldMap.containsKey(entry.getKey())); + for (Map.Entry entry : fieldCache.getSortedFieldMap().entrySet()) { + initOneColumnProperty(entry.getKey(), entry.getValue(), + fieldCache.getIndexFieldMap().containsKey(entry.getKey())); } headKind = HeadKindEnum.CLASS; } @@ -134,22 +123,20 @@ public class ExcelHeadProperty { * @param forceIndex * @return Ignore current field */ - private void initOneColumnProperty(int index, Field field, Boolean forceIndex) { - ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); - List tmpHeadList = new ArrayList(); - String fieldName = FieldUtils.resolveCglibFieldName(field); - boolean notForceName = excelProperty == null || excelProperty.value().length <= 0 - || (excelProperty.value().length == 1 && StringUtils.isEmpty((excelProperty.value())[0])); + private void initOneColumnProperty(int index, FieldWrapper field, Boolean forceIndex) { + List tmpHeadList = new ArrayList<>(); + boolean notForceName = field.getHeads() == null || field.getHeads().length == 0 + || (field.getHeads().length == 1 && StringUtils.isEmpty(field.getHeads()[0])); if (headMap.containsKey(index)) { tmpHeadList.addAll(headMap.get(index).getHeadNameList()); } else { if (notForceName) { - tmpHeadList.add(fieldName); + tmpHeadList.add(field.getFieldName()); } else { - Collections.addAll(tmpHeadList, excelProperty.value()); + Collections.addAll(tmpHeadList, field.getHeads()); } } - Head head = new Head(index, field, fieldName, tmpHeadList, forceIndex, !notForceName); + Head head = new Head(index, field.getField(), field.getFieldName(), tmpHeadList, forceIndex, !notForceName); headMap.put(index, head); } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java b/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java index 41a4f1f6..c3fb3bc9 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java @@ -149,7 +149,8 @@ public class ModelBuildEventListener implements IgnoreExceptionReadListener cellData = cellDataMap.get(index); Object value = ConverterUtils.convertToJavaObject(cellData, head.getField(), ClassUtils.declaredExcelContentProperty(dataMap, readSheetHolder.excelReadHeadProperty().getHeadClazz(), - fieldName), readSheetHolder.converterMap(), context, context.readRowHolder().getRowIndex(), index); + fieldName, readSheetHolder), readSheetHolder.converterMap(), context, + context.readRowHolder().getRowIndex(), index); if (value != null) { dataMap.put(fieldName, value); } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java b/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java index b97db357..f2ab4351 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java @@ -15,7 +15,7 @@ import org.apache.commons.collections4.CollectionUtils; */ public class PageReadListener implements ReadListener { /** - * Defuault single handle the amount of data + * Default single handle the amount of data */ public static int BATCH_COUNT = 100; /** diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/ReadListener.java b/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/ReadListener.java index b56f8a56..79899153 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/ReadListener.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/ReadListener.java @@ -36,7 +36,7 @@ public interface ReadListener extends Listener { /** * When analysis one row trigger invoke function. * - * @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()} + * @param data one row value. It is same as {@link AnalysisContext#readRowHolder()} * @param context analysis context */ void invoke(T data, AnalysisContext context); diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java b/easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java index 90ebe2b5..a65c2d79 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java @@ -10,6 +10,9 @@ import lombok.Setter; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; +import java.io.IOException; +import java.nio.file.Files; + /** * Workbook holder * diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/property/ExcelReadHeadProperty.java b/easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/property/ExcelReadHeadProperty.java index 9c332ceb..0329715c 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/property/ExcelReadHeadProperty.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/property/ExcelReadHeadProperty.java @@ -2,6 +2,7 @@ package com.alibaba.excel.read.metadata.property; import java.util.List; +import com.alibaba.excel.metadata.ConfigurationHolder; import com.alibaba.excel.metadata.Holder; import com.alibaba.excel.metadata.property.ExcelHeadProperty; @@ -12,7 +13,7 @@ import com.alibaba.excel.metadata.property.ExcelHeadProperty; */ public class ExcelReadHeadProperty extends ExcelHeadProperty { - public ExcelReadHeadProperty(Holder holder, Class headClazz, List> head) { - super(holder, headClazz, head); + public ExcelReadHeadProperty(ConfigurationHolder configurationHolder, Class headClazz, List> head) { + super(configurationHolder, headClazz, head); } } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java b/easyexcel-core/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java index 09ab04ec..e78bf94c 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java @@ -23,10 +23,12 @@ public enum ExcelTypeEnum { * csv */ CSV(".csv", new byte[] {-27, -89, -109, -27}), + /** * xls */ XLS(".xls", new byte[] {-48, -49, 17, -32, -95, -79, 26, -31}), + /** * xlsx */ @@ -96,13 +98,11 @@ public enum ExcelTypeEnum { byte[] data = IOUtils.peekFirstNBytes(inputStream, MAX_PATTERN_LENGTH); if (findMagic(XLSX.magic, data)) { return XLSX; - } else if (findMagic(CSV.magic, data)) { - return CSV; } else if (findMagic(XLS.magic, data)) { return XLS; } - throw new ExcelCommonException( - "Convert excel format exception.You can try specifying the 'excelType' yourself"); + // csv has no fixed prefix, if the format is not specified, it defaults to csv + return CSV; } private static boolean findMagic(byte[] expected, byte[] actual) { diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java b/easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java index 6bed3052..f9a53e08 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java @@ -3,6 +3,7 @@ package com.alibaba.excel.util; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -10,6 +11,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; @@ -23,7 +25,9 @@ import com.alibaba.excel.annotation.write.style.ContentStyle; import com.alibaba.excel.converters.AutoConverter; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.exception.ExcelCommonException; -import com.alibaba.excel.metadata.Holder; +import com.alibaba.excel.metadata.ConfigurationHolder; +import com.alibaba.excel.metadata.FieldCache; +import com.alibaba.excel.metadata.FieldWrapper; import com.alibaba.excel.metadata.property.DateTimeFormatProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.FontProperty; @@ -32,9 +36,11 @@ import com.alibaba.excel.metadata.property.StyleProperty; import com.alibaba.excel.write.metadata.holder.WriteHolder; import lombok.AllArgsConstructor; +import lombok.Data; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; +import org.apache.commons.collections4.CollectionUtils; import org.springframework.cglib.beans.BeanMap; /** @@ -44,7 +50,7 @@ import org.springframework.cglib.beans.BeanMap; * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - * + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -55,18 +61,38 @@ import org.springframework.cglib.beans.BeanMap; */ public class ClassUtils { - public static final Map, FieldCache> FIELD_CACHE = new ConcurrentHashMap<>(); + /** + * memory cache + */ + public static final Map FIELD_CACHE = new ConcurrentHashMap<>(); + /** + * thread local cache + */ + private static final ThreadLocal> FIELD_THREAD_LOCAL = new ThreadLocal<>(); + + /** + * The cache configuration information for each of the class + */ + public static final ConcurrentHashMap, Map> CLASS_CONTENT_CACHE + = new ConcurrentHashMap<>(); + + /** + * The cache configuration information for each of the class + */ + private static final ThreadLocal, Map>> CLASS_CONTENT_THREAD_LOCAL + = new ThreadLocal<>(); /** * The cache configuration information for each of the class */ - public static final Map, Map> CLASS_CONTENT_CACHE + public static final ConcurrentHashMap CONTENT_CACHE = new ConcurrentHashMap<>(); /** * The cache configuration information for each of the class */ - public static final Map CONTENT_CACHE = new ConcurrentHashMap<>(); + private static final ThreadLocal> CONTENT_THREAD_LOCAL + = new ThreadLocal<>(); /** * Calculate the configuration information for the class @@ -77,7 +103,8 @@ public class ClassUtils { * @return */ public static ExcelContentProperty declaredExcelContentProperty(Map dataMap, Class headClazz, - String fieldName) { + String fieldName, + ConfigurationHolder configurationHolder) { Class clazz = null; if (dataMap instanceof BeanMap) { Object bean = ((BeanMap)dataMap).getBean(); @@ -85,26 +112,50 @@ public class ClassUtils { clazz = bean.getClass(); } } - return getExcelContentProperty(clazz, headClazz, fieldName); + return getExcelContentProperty(clazz, headClazz, fieldName, configurationHolder); } - private static ExcelContentProperty getExcelContentProperty(Class clazz, Class headClass, String fieldName) { - return CONTENT_CACHE.computeIfAbsent(buildKey(clazz, headClass, fieldName), key -> { - ExcelContentProperty excelContentProperty = Optional.ofNullable(declaredFieldContentMap(clazz)) - .map(map -> map.get(fieldName)) - .orElse(null); - ExcelContentProperty headExcelContentProperty = Optional.ofNullable(declaredFieldContentMap(headClass)) - .map(map -> map.get(fieldName)) - .orElse(null); - ExcelContentProperty combineExcelContentProperty = new ExcelContentProperty(); - - combineExcelContentProperty(combineExcelContentProperty, headExcelContentProperty); - if (clazz != headClass) { - combineExcelContentProperty(combineExcelContentProperty, excelContentProperty); - } - return combineExcelContentProperty; - }); + private static ExcelContentProperty getExcelContentProperty(Class clazz, Class headClass, String fieldName, + ConfigurationHolder configurationHolder) { + switch (configurationHolder.globalConfiguration().getFiledCacheLocation()) { + case THREAD_LOCAL: + Map contentCacheMap = CONTENT_THREAD_LOCAL.get(); + if (contentCacheMap == null) { + contentCacheMap = MapUtils.newHashMap(); + CONTENT_THREAD_LOCAL.set(contentCacheMap); + } + return contentCacheMap.computeIfAbsent(buildKey(clazz, headClass, fieldName), key -> { + return doGetExcelContentProperty(clazz, headClass, fieldName, configurationHolder); + }); + case MEMORY: + return CONTENT_CACHE.computeIfAbsent(buildKey(clazz, headClass, fieldName), key -> { + return doGetExcelContentProperty(clazz, headClass, fieldName, configurationHolder); + }); + case NONE: + return doGetExcelContentProperty(clazz, headClass, fieldName, configurationHolder); + default: + throw new UnsupportedOperationException("unsupported enum"); + } + } + private static ExcelContentProperty doGetExcelContentProperty(Class clazz, Class headClass, + String fieldName, + ConfigurationHolder configurationHolder) { + ExcelContentProperty excelContentProperty = Optional.ofNullable( + declaredFieldContentMap(clazz, configurationHolder)) + .map(map -> map.get(fieldName)) + .orElse(null); + ExcelContentProperty headExcelContentProperty = Optional.ofNullable( + declaredFieldContentMap(headClass, configurationHolder)) + .map(map -> map.get(fieldName)) + .orElse(null); + ExcelContentProperty combineExcelContentProperty = new ExcelContentProperty(); + + combineExcelContentProperty(combineExcelContentProperty, headExcelContentProperty); + if (clazz != headClass) { + combineExcelContentProperty(combineExcelContentProperty, excelContentProperty); + } + return combineExcelContentProperty; } public static void combineExcelContentProperty(ExcelContentProperty combineExcelContentProperty, @@ -136,164 +187,256 @@ public class ClassUtils { return new ContentPropertyKey(clazz, headClass, fieldName); } - private static Map declaredFieldContentMap(Class clazz) { + private static Map declaredFieldContentMap(Class clazz, + ConfigurationHolder configurationHolder) { if (clazz == null) { return null; } - return CLASS_CONTENT_CACHE.computeIfAbsent(clazz, key -> { - List tempFieldList = new ArrayList<>(); - Class tempClass = clazz; - while (tempClass != null) { - Collections.addAll(tempFieldList, tempClass.getDeclaredFields()); - // Get the parent class and give it to yourself - tempClass = tempClass.getSuperclass(); - } - - ContentStyle parentContentStyle = clazz.getAnnotation(ContentStyle.class); - ContentFontStyle parentContentFontStyle = clazz.getAnnotation(ContentFontStyle.class); - Map fieldContentMap = MapUtils.newHashMapWithExpectedSize( - tempFieldList.size()); - for (Field field : tempFieldList) { - ExcelContentProperty excelContentProperty = new ExcelContentProperty(); - excelContentProperty.setField(field); - - ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); - if (excelProperty != null) { - Class> convertClazz = excelProperty.converter(); - if (convertClazz != AutoConverter.class) { - try { - Converter converter = convertClazz.getDeclaredConstructor().newInstance(); - excelContentProperty.setConverter(converter); - } catch (Exception e) { - throw new ExcelCommonException( - "Can not instance custom converter:" + convertClazz.getName()); - } - } + switch (configurationHolder.globalConfiguration().getFiledCacheLocation()) { + case THREAD_LOCAL: + Map, Map> classContentCacheMap + = CLASS_CONTENT_THREAD_LOCAL.get(); + if (classContentCacheMap == null) { + classContentCacheMap = MapUtils.newHashMap(); + CLASS_CONTENT_THREAD_LOCAL.set(classContentCacheMap); } + return classContentCacheMap.computeIfAbsent(clazz, key -> { + return doDeclaredFieldContentMap(clazz); + }); + case MEMORY: + return CLASS_CONTENT_CACHE.computeIfAbsent(clazz, key -> { + return doDeclaredFieldContentMap(clazz); + }); + case NONE: + return doDeclaredFieldContentMap(clazz); + default: + throw new UnsupportedOperationException("unsupported enum"); + } - ContentStyle contentStyle = field.getAnnotation(ContentStyle.class); - if (contentStyle == null) { - contentStyle = parentContentStyle; - } - excelContentProperty.setContentStyleProperty(StyleProperty.build(contentStyle)); + } - ContentFontStyle contentFontStyle = field.getAnnotation(ContentFontStyle.class); - if (contentFontStyle == null) { - contentFontStyle = parentContentFontStyle; + private static Map doDeclaredFieldContentMap(Class clazz) { + if (clazz == null) { + return null; + } + List tempFieldList = new ArrayList<>(); + Class tempClass = clazz; + while (tempClass != null) { + Collections.addAll(tempFieldList, tempClass.getDeclaredFields()); + // Get the parent class and give it to yourself + tempClass = tempClass.getSuperclass(); + } + + ContentStyle parentContentStyle = clazz.getAnnotation(ContentStyle.class); + ContentFontStyle parentContentFontStyle = clazz.getAnnotation(ContentFontStyle.class); + Map fieldContentMap = MapUtils.newHashMapWithExpectedSize( + tempFieldList.size()); + for (Field field : tempFieldList) { + ExcelContentProperty excelContentProperty = new ExcelContentProperty(); + excelContentProperty.setField(field); + + ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); + if (excelProperty != null) { + Class> convertClazz = excelProperty.converter(); + if (convertClazz != AutoConverter.class) { + try { + Converter converter = convertClazz.getDeclaredConstructor().newInstance(); + excelContentProperty.setConverter(converter); + } catch (Exception e) { + throw new ExcelCommonException( + "Can not instance custom converter:" + convertClazz.getName()); + } } - excelContentProperty.setContentFontProperty(FontProperty.build(contentFontStyle)); + } - excelContentProperty.setDateTimeFormatProperty( - DateTimeFormatProperty.build(field.getAnnotation(DateTimeFormat.class))); - excelContentProperty.setNumberFormatProperty( - NumberFormatProperty.build(field.getAnnotation(NumberFormat.class))); + ContentStyle contentStyle = field.getAnnotation(ContentStyle.class); + if (contentStyle == null) { + contentStyle = parentContentStyle; + } + excelContentProperty.setContentStyleProperty(StyleProperty.build(contentStyle)); - fieldContentMap.put(field.getName(), excelContentProperty); + ContentFontStyle contentFontStyle = field.getAnnotation(ContentFontStyle.class); + if (contentFontStyle == null) { + contentFontStyle = parentContentFontStyle; } - return fieldContentMap; - }); + excelContentProperty.setContentFontProperty(FontProperty.build(contentFontStyle)); + + excelContentProperty.setDateTimeFormatProperty( + DateTimeFormatProperty.build(field.getAnnotation(DateTimeFormat.class))); + excelContentProperty.setNumberFormatProperty( + NumberFormatProperty.build(field.getAnnotation(NumberFormat.class))); + + fieldContentMap.put(field.getName(), excelContentProperty); + } + return fieldContentMap; } /** * Parsing field in the class * - * @param clazz Need to parse the class - * @param sortedAllFieldMap Complete the map of sorts - * @param indexFieldMap Use the index to sort fields - * @param ignoreMap You want to ignore field map - * @param needIgnore If you want to ignore fields need to ignore - * @param holder holder + * @param clazz Need to parse the class + * @param configurationHolder configuration */ - public static void declaredFields(Class clazz, Map sortedAllFieldMap, - Map indexFieldMap, Map ignoreMap, Boolean needIgnore, Holder holder) { - FieldCache fieldCache = declaredFields(clazz); - if (fieldCache == null) { - return; + public static FieldCache declaredFields(Class clazz, ConfigurationHolder configurationHolder) { + switch (configurationHolder.globalConfiguration().getFiledCacheLocation()) { + case THREAD_LOCAL: + Map fieldCacheMap = FIELD_THREAD_LOCAL.get(); + if (fieldCacheMap == null) { + fieldCacheMap = MapUtils.newHashMap(); + FIELD_THREAD_LOCAL.set(fieldCacheMap); + } + return fieldCacheMap.computeIfAbsent(new FieldCacheKey(clazz, configurationHolder), key -> { + return doDeclaredFields(clazz, configurationHolder); + }); + case MEMORY: + return FIELD_CACHE.computeIfAbsent(new FieldCacheKey(clazz, configurationHolder), key -> { + return doDeclaredFields(clazz, configurationHolder); + }); + case NONE: + return doDeclaredFields(clazz, configurationHolder); + default: + throw new UnsupportedOperationException("unsupported enum"); } - if (ignoreMap != null) { - ignoreMap.putAll(fieldCache.getIgnoreMap()); + } + + private static FieldCache doDeclaredFields(Class clazz, ConfigurationHolder configurationHolder) { + List tempFieldList = new ArrayList<>(); + Class tempClass = clazz; + // When the parent class is null, it indicates that the parent class (Object class) has reached the top + // level. + while (tempClass != null) { + Collections.addAll(tempFieldList, tempClass.getDeclaredFields()); + // Get the parent class and give it to yourself + tempClass = tempClass.getSuperclass(); } - Map tempIndexFieldMap = indexFieldMap; - if (tempIndexFieldMap == null) { - tempIndexFieldMap = MapUtils.newTreeMap(); + // Screening of field + Map> orderFieldMap = new TreeMap<>(); + Map indexFieldMap = new TreeMap<>(); + Set ignoreSet = new HashSet<>(); + + ExcelIgnoreUnannotated excelIgnoreUnannotated = clazz.getAnnotation(ExcelIgnoreUnannotated.class); + for (Field field : tempFieldList) { + declaredOneField(field, orderFieldMap, indexFieldMap, ignoreSet, excelIgnoreUnannotated); } - tempIndexFieldMap.putAll(fieldCache.getIndexFieldMap()); + Map sortedFieldMap = buildSortedAllFieldMap(orderFieldMap, indexFieldMap); + FieldCache fieldCache = new FieldCache(sortedFieldMap, indexFieldMap); - Map originSortedAllFieldMap = fieldCache.getSortedAllFieldMap(); - if (!needIgnore) { - sortedAllFieldMap.putAll(originSortedAllFieldMap); - return; + if (!(configurationHolder instanceof WriteHolder)) { + return fieldCache; } + WriteHolder writeHolder = (WriteHolder)configurationHolder; + + boolean needIgnore = !CollectionUtils.isEmpty(writeHolder.excludeColumnFieldNames()) + || !CollectionUtils.isEmpty(writeHolder.excludeColumnIndexes()) + || !CollectionUtils.isEmpty(writeHolder.includeColumnFieldNames()) + || !CollectionUtils.isEmpty(writeHolder.includeColumnIndexes()); + + if (!needIgnore) { + return fieldCache; + } + // ignore filed + Map tempSortedFieldMap = MapUtils.newHashMap(); int index = 0; - for (Map.Entry entry : originSortedAllFieldMap.entrySet()) { + for (Map.Entry entry : sortedFieldMap.entrySet()) { Integer key = entry.getKey(); - Field field = entry.getValue(); + FieldWrapper field = entry.getValue(); // The current field needs to be ignored - if (((WriteHolder)holder).ignore(entry.getValue().getName(), entry.getKey())) { - if (ignoreMap != null) { - ignoreMap.put(field.getName(), field); - } - tempIndexFieldMap.remove(index); + if (writeHolder.ignore(field.getFieldName(), entry.getKey())) { + ignoreSet.add(field.getFieldName()); + indexFieldMap.remove(index); } else { // Mandatory sorted fields - if (tempIndexFieldMap.containsKey(key)) { - sortedAllFieldMap.put(key, field); + if (indexFieldMap.containsKey(key)) { + tempSortedFieldMap.put(key, field); } else { // Need to reorder automatically // Check whether the current key is already in use - while (sortedAllFieldMap.containsKey(index)) { + while (tempSortedFieldMap.containsKey(index)) { index++; } - sortedAllFieldMap.put(index++, field); + tempSortedFieldMap.put(index++, field); } } } - } + fieldCache.setSortedFieldMap(tempSortedFieldMap); - public static void declaredFields(Class clazz, Map sortedAllFieldMap, Boolean needIgnore, - WriteHolder writeHolder) { - declaredFields(clazz, sortedAllFieldMap, null, null, needIgnore, writeHolder); + // resort field + resortField(writeHolder, fieldCache); + return fieldCache; } - private static FieldCache declaredFields(Class clazz) { - if (clazz == null) { - return null; + /** + * it only works when {@link WriteHolder#includeColumnFieldNames()} or + * {@link WriteHolder#includeColumnIndexes()} has value + * and {@link WriteHolder#orderByIncludeColumn()} is true + **/ + private static void resortField(WriteHolder writeHolder, FieldCache fieldCache) { + if (!writeHolder.orderByIncludeColumn()) { + return; } - return FIELD_CACHE.computeIfAbsent(clazz, key -> { - List tempFieldList = new ArrayList<>(); - Class tempClass = clazz; - // When the parent class is null, it indicates that the parent class (Object class) has reached the top - // level. - while (tempClass != null) { - Collections.addAll(tempFieldList, tempClass.getDeclaredFields()); - // Get the parent class and give it to yourself - tempClass = tempClass.getSuperclass(); + Map indexFieldMap = fieldCache.getIndexFieldMap(); + + Collection includeColumnFieldNames = writeHolder.includeColumnFieldNames(); + if (!CollectionUtils.isEmpty(includeColumnFieldNames)) { + // Field sorted map + Map filedIndexMap = MapUtils.newHashMap(); + int fieldIndex = 0; + for (String includeColumnFieldName : includeColumnFieldNames) { + filedIndexMap.put(includeColumnFieldName, fieldIndex++); } - // Screening of field - Map> orderFieldMap = new TreeMap>(); - Map indexFieldMap = new TreeMap(); - Map ignoreMap = new HashMap(16); - - ExcelIgnoreUnannotated excelIgnoreUnannotated = clazz.getAnnotation(ExcelIgnoreUnannotated.class); - for (Field field : tempFieldList) { - declaredOneField(field, orderFieldMap, indexFieldMap, ignoreMap, excelIgnoreUnannotated); + + // rebuild sortedFieldMap + Map tempSortedFieldMap = MapUtils.newHashMap(); + fieldCache.getSortedFieldMap().forEach((index, field) -> { + Integer tempFieldIndex = filedIndexMap.get(field.getFieldName()); + if (tempFieldIndex != null) { + tempSortedFieldMap.put(tempFieldIndex, field); + + // The user has redefined the ordering and the ordering of annotations needs to be invalidated + if (!tempFieldIndex.equals(index)) { + indexFieldMap.remove(index); + } + } + }); + fieldCache.setSortedFieldMap(tempSortedFieldMap); + return; + } + + Collection includeColumnIndexes = writeHolder.includeColumnIndexes(); + if (!CollectionUtils.isEmpty(includeColumnIndexes)) { + // Index sorted map + Map filedIndexMap = MapUtils.newHashMap(); + int fieldIndex = 0; + for (Integer includeColumnIndex : includeColumnIndexes) { + filedIndexMap.put(includeColumnIndex, fieldIndex++); } - return new FieldCache(buildSortedAllFieldMap(orderFieldMap, indexFieldMap), indexFieldMap, ignoreMap); - }); + + // rebuild sortedFieldMap + Map tempSortedFieldMap = MapUtils.newHashMap(); + fieldCache.getSortedFieldMap().forEach((index, field) -> { + Integer tempFieldIndex = filedIndexMap.get(index); + + // The user has redefined the ordering and the ordering of annotations needs to be invalidated + if (tempFieldIndex != null) { + tempSortedFieldMap.put(tempFieldIndex, field); + } + }); + fieldCache.setSortedFieldMap(tempSortedFieldMap); + } } - private static Map buildSortedAllFieldMap(Map> orderFieldMap, - Map indexFieldMap) { + private static Map buildSortedAllFieldMap(Map> orderFieldMap, + Map indexFieldMap) { - Map sortedAllFieldMap = new HashMap( + Map sortedAllFieldMap = new HashMap<>( (orderFieldMap.size() + indexFieldMap.size()) * 4 / 3 + 1); - Map tempIndexFieldMap = new HashMap(indexFieldMap); + Map tempIndexFieldMap = new HashMap<>(indexFieldMap); int index = 0; - for (List fieldList : orderFieldMap.values()) { - for (Field field : fieldList) { + for (List fieldList : orderFieldMap.values()) { + for (FieldWrapper field : fieldList) { while (tempIndexFieldMap.containsKey(index)) { sortedAllFieldMap.put(index, tempIndexFieldMap.get(index)); tempIndexFieldMap.remove(index); @@ -307,34 +450,45 @@ public class ClassUtils { return sortedAllFieldMap; } - private static void declaredOneField(Field field, Map> orderFieldMap, - Map indexFieldMap, Map ignoreMap, + private static void declaredOneField(Field field, Map> orderFieldMap, + Map indexFieldMap, Set ignoreSet, ExcelIgnoreUnannotated excelIgnoreUnannotated) { + String fieldName = FieldUtils.resolveCglibFieldName(field); + FieldWrapper fieldWrapper = new FieldWrapper(); + fieldWrapper.setField(field); + fieldWrapper.setFieldName(fieldName); ExcelIgnore excelIgnore = field.getAnnotation(ExcelIgnore.class); + if (excelIgnore != null) { - ignoreMap.put(field.getName(), field); + ignoreSet.add(fieldName); return; } ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); boolean noExcelProperty = excelProperty == null && excelIgnoreUnannotated != null; if (noExcelProperty) { - ignoreMap.put(field.getName(), field); + ignoreSet.add(fieldName); return; } boolean isStaticFinalOrTransient = (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) || Modifier.isTransient(field.getModifiers()); if (excelProperty == null && isStaticFinalOrTransient) { - ignoreMap.put(field.getName(), field); + ignoreSet.add(fieldName); return; } + // set heads + if (excelProperty != null) { + fieldWrapper.setHeads(excelProperty.value()); + } + if (excelProperty != null && excelProperty.index() >= 0) { if (indexFieldMap.containsKey(excelProperty.index())) { - throw new ExcelCommonException("The index of '" + indexFieldMap.get(excelProperty.index()).getName() - + "' and '" + field.getName() + "' must be inconsistent"); + throw new ExcelCommonException( + "The index of '" + indexFieldMap.get(excelProperty.index()).getFieldName() + + "' and '" + field.getName() + "' must be inconsistent"); } - indexFieldMap.put(excelProperty.index(), field); + indexFieldMap.put(excelProperty.index(), fieldWrapper); return; } @@ -342,34 +496,8 @@ public class ClassUtils { if (excelProperty != null) { order = excelProperty.order(); } - List orderFieldList = orderFieldMap.computeIfAbsent(order, key -> ListUtils.newArrayList()); - orderFieldList.add(field); - } - - private static class FieldCache { - - private final Map sortedAllFieldMap; - private final Map indexFieldMap; - private final Map ignoreMap; - - public FieldCache(Map sortedAllFieldMap, Map indexFieldMap, - Map ignoreMap) { - this.sortedAllFieldMap = sortedAllFieldMap; - this.indexFieldMap = indexFieldMap; - this.ignoreMap = ignoreMap; - } - - public Map getSortedAllFieldMap() { - return sortedAllFieldMap; - } - - public Map getIndexFieldMap() { - return indexFieldMap; - } - - public Map getIgnoreMap() { - return ignoreMap; - } + List orderFieldList = orderFieldMap.computeIfAbsent(order, key -> ListUtils.newArrayList()); + orderFieldList.add(fieldWrapper); } /** @@ -425,4 +553,31 @@ public class ClassUtils { private Class headClass; private String fieldName; } + + @Data + public static class FieldCacheKey { + private Class clazz; + private Collection excludeColumnFieldNames; + private Collection excludeColumnIndexes; + private Collection includeColumnFieldNames; + private Collection includeColumnIndexes; + + FieldCacheKey(Class clazz, ConfigurationHolder configurationHolder) { + this.clazz = clazz; + if (configurationHolder instanceof WriteHolder) { + WriteHolder writeHolder = (WriteHolder)configurationHolder; + this.excludeColumnFieldNames = writeHolder.excludeColumnFieldNames(); + this.excludeColumnIndexes = writeHolder.excludeColumnIndexes(); + this.includeColumnFieldNames = writeHolder.includeColumnFieldNames(); + this.includeColumnIndexes = writeHolder.includeColumnIndexes(); + } + } + } + + public static void removeThreadLocalCache() { + FIELD_THREAD_LOCAL.remove(); + CLASS_CONTENT_THREAD_LOCAL.remove(); + CONTENT_THREAD_LOCAL.remove(); + } } + diff --git a/easyexcel-core/src/main/java/org/apache/poi/hssf/usermodel/PoiUtils.java b/easyexcel-core/src/main/java/com/alibaba/excel/util/PoiUtils.java similarity index 59% rename from easyexcel-core/src/main/java/org/apache/poi/hssf/usermodel/PoiUtils.java rename to easyexcel-core/src/main/java/com/alibaba/excel/util/PoiUtils.java index 4f1d404f..0b70a487 100644 --- a/easyexcel-core/src/main/java/org/apache/poi/hssf/usermodel/PoiUtils.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/util/PoiUtils.java @@ -1,10 +1,16 @@ -package org.apache.poi.hssf.usermodel; +package com.alibaba.excel.util; +import com.alibaba.excel.exception.ExcelRuntimeException; + +import org.apache.poi.hssf.record.RowRecord; +import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.util.BitField; import org.apache.poi.util.BitFieldFactory; import org.apache.poi.xssf.usermodel.XSSFRow; +import java.lang.reflect.Field; + /** * utils * @@ -17,6 +23,8 @@ public class PoiUtils { */ public static final BitField CUSTOM_HEIGHT = BitFieldFactory.getInstance(0x640); + private static final Field ROW_RECORD_FIELD = FieldUtils.getField(HSSFRow.class, "row", true); + /** * Whether to customize the height * @@ -30,7 +38,11 @@ public class PoiUtils { } if (row instanceof HSSFRow) { HSSFRow hssfRow = (HSSFRow)row; - return CUSTOM_HEIGHT.getValue(hssfRow.getRowRecord().getOptionFlags()) == 1; + try { + RowRecord record = (RowRecord)ROW_RECORD_FIELD.get(hssfRow); + return CUSTOM_HEIGHT.getValue(record.getOptionFlags()) == 1; + } catch (IllegalAccessException ignore) { + } } return false; } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/util/WorkBookUtil.java b/easyexcel-core/src/main/java/com/alibaba/excel/util/WorkBookUtil.java index 8fc7c210..8ed49e6a 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/util/WorkBookUtil.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/util/WorkBookUtil.java @@ -70,7 +70,9 @@ public class WorkBookUtil { new OutputStreamWriter(writeWorkbookHolder.getOutputStream(), writeWorkbookHolder.getCharset())), writeWorkbookHolder.getGlobalConfiguration().getLocale(), writeWorkbookHolder.getGlobalConfiguration().getUse1904windowing(), - writeWorkbookHolder.getGlobalConfiguration().getUseScientificFormat()); + writeWorkbookHolder.getGlobalConfiguration().getUseScientificFormat(), + writeWorkbookHolder.getCharset(), + writeWorkbookHolder.getWithBom()); writeWorkbookHolder.setCachedWorkbook(csvWorkbook); writeWorkbookHolder.setWorkbook(csvWorkbook); return; diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java index f8844a52..014f97e2 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java @@ -106,8 +106,9 @@ public abstract class AbstractExcelWriterParameterBuilder includeColumnFieldNames) { parameter().setIncludeColumnFieldNames(includeColumnFieldNames); return self(); @@ -120,4 +121,16 @@ public abstract class AbstractExcelWriterParameterBuilder sortedAllFieldMap = new TreeMap<>(); int relativeRowIndex = 0; for (Object oneRowData : data) { int lastRowIndex = relativeRowIndex + newRowIndex; - addOneRowOfDataToExcel(oneRowData, lastRowIndex, relativeRowIndex, sortedAllFieldMap); + addOneRowOfDataToExcel(oneRowData, lastRowIndex, relativeRowIndex); relativeRowIndex++; } } - private void addOneRowOfDataToExcel(Object oneRowData, int rowIndex, int relativeRowIndex, - Map sortedAllFieldMap) { + private void addOneRowOfDataToExcel(Object oneRowData, int rowIndex, int relativeRowIndex) { if (oneRowData == null) { return; } @@ -79,7 +78,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { } else if (oneRowData instanceof Map) { addBasicTypeToExcel(new MapRowData((Map)oneRowData), row, rowIndex, relativeRowIndex); } else { - addJavaObjectToExcel(oneRowData, row, rowIndex, relativeRowIndex, sortedAllFieldMap); + addJavaObjectToExcel(oneRowData, row, rowIndex, relativeRowIndex); } WriteHandlerUtils.afterRowDispose(rowWriteHandlerContext); @@ -119,7 +118,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { int dataIndex, int columnIndex) { ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null, writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), - head == null ? null : head.getFieldName()); + head == null ? null : head.getFieldName(), writeContext.currentWriteHolder()); CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(writeContext, row, rowIndex, head, columnIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty); @@ -138,8 +137,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext); } - private void addJavaObjectToExcel(Object oneRowData, Row row, int rowIndex, int relativeRowIndex, - Map sortedAllFieldMap) { + private void addJavaObjectToExcel(Object oneRowData, Row row, int rowIndex, int relativeRowIndex) { WriteHolder currentWriteHolder = writeContext.currentWriteHolder(); BeanMap beanMap = BeanMapUtils.create(oneRowData); // Bean the contains of the Map Key method with poor performance,So to create a keySet here @@ -158,7 +156,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { } ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, - currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), name); + currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), name, currentWriteHolder); CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext( writeContext, row, rowIndex, head, columnIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty); @@ -185,19 +183,17 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { } maxCellIndex++; - Map ignoreMap = writeContext.currentWriteHolder().excelWriteHeadProperty().getIgnoreMap(); - initSortedAllFieldMapFieldList(oneRowData.getClass(), sortedAllFieldMap); - for (Map.Entry entry : sortedAllFieldMap.entrySet()) { - Field field = entry.getValue(); - String fieldName = FieldUtils.resolveCglibFieldName(field); - boolean uselessData = !beanKeySet.contains(fieldName) || beanMapHandledSet.contains(fieldName) - || ignoreMap.containsKey(fieldName); + FieldCache fieldCache = ClassUtils.declaredFields(oneRowData.getClass(), writeContext.currentWriteHolder()); + for (Map.Entry entry : fieldCache.getSortedFieldMap().entrySet()) { + FieldWrapper field = entry.getValue(); + String fieldName = field.getFieldName(); + boolean uselessData = !beanKeySet.contains(fieldName) || beanMapHandledSet.contains(fieldName); if (uselessData) { continue; } Object value = beanMap.get(fieldName); ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, - currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), fieldName); + currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), fieldName, currentWriteHolder); CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext( writeContext, row, rowIndex, null, maxCellIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty); WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext); @@ -218,18 +214,4 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { } } - private void initSortedAllFieldMapFieldList(Class clazz, Map sortedAllFieldMap) { - if (!sortedAllFieldMap.isEmpty()) { - return; - } - - WriteSheetHolder writeSheetHolder = writeContext.writeSheetHolder(); - boolean needIgnore = - !CollectionUtils.isEmpty(writeSheetHolder.getExcludeColumnFieldNames()) || !CollectionUtils - .isEmpty(writeSheetHolder.getExcludeColumnIndexes()) || !CollectionUtils - .isEmpty(writeSheetHolder.getIncludeColumnFieldNames()) || !CollectionUtils - .isEmpty(writeSheetHolder.getIncludeColumnIndexes()); - ClassUtils.declaredFields(clazz, sortedAllFieldMap, needIgnore, writeSheetHolder); - } - } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java index 6779e875..81524223 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java @@ -37,7 +37,9 @@ import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import org.apache.commons.collections4.CollectionUtils; -import org.apache.poi.hssf.usermodel.PoiUtils; + +import com.alibaba.excel.util.PoiUtils; + import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellType; @@ -215,7 +217,8 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } Object value = dataMap.get(variable); ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap, - writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable); + writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable, + writeContext.currentWriteHolder()); cellWriteHandlerContext.setExcelContentProperty(excelContentProperty); createCell(analysisCell, fillConfig, cellWriteHandlerContext, rowWriteHandlerContext); @@ -249,7 +252,8 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } Object value = dataMap.get(variable); ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap, - writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable); + writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable, + writeContext.currentWriteHolder()); cellWriteHandlerContext.setOriginalValue(value); cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(dataMap, variable, value)); cellWriteHandlerContext.setExcelContentProperty(excelContentProperty); diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java index b0745d38..a926209a 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java @@ -56,4 +56,11 @@ public class WriteBasicParameter extends BasicParameter { * Only output the custom columns. */ private Collection includeColumnFieldNames; + + /** + * Data will be order by {@link #includeColumnFieldNames} or {@link #includeColumnIndexes}. + * + * default is false. + */ + private Boolean orderByIncludeColumn; } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java index fd29562d..61bd9255 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java @@ -41,6 +41,13 @@ public class WriteWorkbook extends WriteBasicParameter { * Only work on the CSV file */ private Charset charset; + + /** + * Set the encoding prefix in the csv file, otherwise the office may open garbled characters. + * Default true. + */ + private Boolean withBom; + /** * Template input stream *

diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java index b6907e42..d03ea3d8 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java @@ -95,6 +95,13 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ */ private Collection includeColumnFieldNames; + /** + * Data will be order by {@link #includeColumnFieldNames} or {@link #includeColumnIndexes}. + * + * default is false. + */ + private Boolean orderByIncludeColumn; + /** * Write handler */ @@ -194,6 +201,17 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ } else { this.includeColumnFieldNames = writeBasicParameter.getIncludeColumnFieldNames(); } + + if (writeBasicParameter.getOrderByIncludeColumn() == null) { + if (parentAbstractWriteHolder == null) { + this.orderByIncludeColumn = Boolean.FALSE; + } else { + this.orderByIncludeColumn = parentAbstractWriteHolder.getOrderByIncludeColumn(); + } + } else { + this.orderByIncludeColumn = writeBasicParameter.getOrderByIncludeColumn(); + } + if (writeBasicParameter.getIncludeColumnIndexes() == null && parentAbstractWriteHolder != null) { this.includeColumnIndexes = parentAbstractWriteHolder.getIncludeColumnIndexes(); } else { @@ -480,4 +498,28 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ return getAutomaticMergeHead(); } + @Override + public boolean orderByIncludeColumn() { + return getOrderByIncludeColumn(); + } + + @Override + public Collection includeColumnIndexes() { + return getIncludeColumnIndexes(); + } + + @Override + public Collection includeColumnFieldNames() { + return getIncludeColumnFieldNames(); + } + + @Override + public Collection excludeColumnIndexes() { + return getExcludeColumnIndexes(); + } + + @Override + public Collection excludeColumnFieldNames() { + return getExcludeColumnFieldNames(); + } } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java index 77f3c14f..ca0fb3e9 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java @@ -1,10 +1,11 @@ package com.alibaba.excel.write.metadata.holder; +import java.util.Collection; + import com.alibaba.excel.metadata.ConfigurationHolder; import com.alibaba.excel.write.property.ExcelWriteHeadProperty; /** - * * Get the corresponding Holder * * @author Jiaju Zhuang @@ -46,4 +47,42 @@ public interface WriteHolder extends ConfigurationHolder { * @return */ int relativeHeadRowIndex(); + + /** + * Data will be order by {@link #includeColumnFieldNames} or {@link #includeColumnIndexes}. + * + * default is false. + * + * @return + */ + + boolean orderByIncludeColumn(); + + /** + * Only output the custom columns. + * + * @return + */ + Collection includeColumnIndexes(); + + /** + * Only output the custom columns. + * + * @return + */ + Collection includeColumnFieldNames(); + + /** + * Ignore the custom columns. + * + * @return + */ + Collection excludeColumnIndexes(); + + /** + * Ignore the custom columns. + * + * @return + */ + Collection excludeColumnFieldNames(); } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java index e64b8e38..37d91975 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java @@ -11,6 +11,7 @@ import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; +import com.alibaba.excel.enums.CacheLocationEnum; import com.alibaba.excel.enums.HolderEnum; import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.metadata.data.DataFormatData; @@ -84,6 +85,13 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { * Only work on the CSV file */ private Charset charset; + + /** + * Set the encoding prefix in the csv file, otherwise the office may open garbled characters. + * Default true. + */ + private Boolean withBom; + /** * Template input stream *

@@ -176,6 +184,12 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { this.charset = writeWorkbook.getCharset(); } + if (writeWorkbook.getWithBom() == null) { + this.withBom = Boolean.TRUE; + } else { + this.withBom = writeWorkbook.getWithBom(); + } + if (writeWorkbook.getAutoCloseStream() == null) { this.autoCloseStream = Boolean.TRUE; } else { diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java index 4c11010a..7affadbe 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java @@ -16,6 +16,7 @@ import com.alibaba.excel.annotation.write.style.HeadStyle; import com.alibaba.excel.annotation.write.style.OnceAbsoluteMerge; import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.metadata.CellRange; +import com.alibaba.excel.metadata.ConfigurationHolder; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Holder; import com.alibaba.excel.metadata.property.ColumnWidthProperty; @@ -44,8 +45,8 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty { private RowHeightProperty contentRowHeightProperty; private OnceAbsoluteMergeProperty onceAbsoluteMergeProperty; - public ExcelWriteHeadProperty(Holder holder, Class headClazz, List> head) { - super(holder, headClazz, head); + public ExcelWriteHeadProperty(ConfigurationHolder configurationHolder, Class headClazz, List> head) { + super(configurationHolder, headClazz, head); if (getHeadKind() != HeadKindEnum.CLASS) { return; } diff --git a/easyexcel-support/pom.xml b/easyexcel-support/pom.xml index 23613040..20e7c937 100644 --- a/easyexcel-support/pom.xml +++ b/easyexcel-support/pom.xml @@ -21,6 +21,7 @@ org.springframework spring-core + 5.3.27 org.springframework diff --git a/easyexcel-test/pom.xml b/easyexcel-test/pom.xml index e13c5a9b..723e092f 100644 --- a/easyexcel-test/pom.xml +++ b/easyexcel-test/pom.xml @@ -31,27 +31,43 @@ com.alibaba easyexcel-core - test - - - ch.qos.logback - logback-classic - test com.alibaba.fastjson2 fastjson2 - test + 2.0.29 org.springframework.boot spring-boot-starter-web - test + 2.7.11 - junit - junit - test + org.springframework.boot + spring-boot-starter-test + 2.7.11 + + + + + org.slf4j + slf4j-simple + 1.7.36 + + + org.slf4j + jcl-over-slf4j + 1.7.36 + + + org.slf4j + log4j-over-slf4j + 1.7.36 + + + ch.qos.logback + logback-classic + 1.2.12 @@ -61,11 +77,9 @@ org.apache.maven.plugins maven-surefire-plugin - - /com/alibaba/easyexcel/test/demo/**/*.java - /com/alibaba/easyexcel/test/temp/**/*.java - /com/alibaba/easyexcel/test/util/**/*.java - + + /com/alibaba/easyexcel/test/core/**/*.java + true diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataListener.java index 222d83b5..a7249bfb 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataListener.java @@ -4,16 +4,16 @@ import java.text.ParseException; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; -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.ExcelCommonException; import com.alibaba.excel.util.DateUtils; import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Assertions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ @@ -28,14 +28,14 @@ public class AnnotationDataListener extends AnalysisEventListener dataStyle() throws Exception { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataListener.java index ea091f23..eaa73f99 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataListener.java @@ -3,14 +3,14 @@ package com.alibaba.easyexcel.test.core.annotation; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Assertions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ @@ -25,12 +25,12 @@ public class AnnotationIndexAndNameDataListener extends AnalysisEventListener() { + + private final List dataList = Lists.newArrayList(); + + @Override + public void invokeHead(Map> headMap, AnalysisContext context) { + String head = headMap.get(0).getStringValue(); + Assertions.assertEquals("姓名", head); + } + + @Override + public void invoke(BomData data, AnalysisContext context) { + dataList.add(data); + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + Assertions.assertEquals(dataList.size(), 10); + BomData bomData = dataList.get(0); + Assertions.assertEquals("姓名0", bomData.getName()); + Assertions.assertEquals(20, (long)bomData.getAge()); + } + }) + .charset(charset) + .sheet().doRead(); + } + + private void readCsv(File file) { + EasyExcel.read(file, BomData.class, new ReadListener() { + + private final List dataList = Lists.newArrayList(); + + @Override + public void invokeHead(Map> headMap, AnalysisContext context) { + String head = headMap.get(0).getStringValue(); + Assertions.assertEquals("姓名", head); + } + + @Override + public void invoke(BomData data, AnalysisContext context) { + dataList.add(data); + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + Assertions.assertEquals(dataList.size(), 10); + BomData bomData = dataList.get(0); + Assertions.assertEquals("姓名0", bomData.getName()); + Assertions.assertEquals(20L, (long)bomData.getAge()); + } + }).sheet().doRead(); + } + + private List data() { + List list = ListUtils.newArrayList(); + for (int i = 0; i < 10; i++) { + BomData data = new BomData(); + data.setName("姓名" + i); + data.setAge(20L); + list.add(data); + } + return list; + } +} diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/cache/CacheData.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/cache/CacheData.java new file mode 100644 index 00000000..f6455438 --- /dev/null +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/cache/CacheData.java @@ -0,0 +1,21 @@ +package com.alibaba.easyexcel.test.core.cache; + +import com.alibaba.excel.annotation.ExcelProperty; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +/** + * @author Jiaju Zhuang + */ +@Getter +@Setter +@EqualsAndHashCode +public class CacheData { + @ExcelProperty("姓名") + private String name; + + @ExcelProperty("年龄") + private Long age; +} diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/cache/CacheDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/cache/CacheDataTest.java new file mode 100644 index 00000000..b1c242b3 --- /dev/null +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/cache/CacheDataTest.java @@ -0,0 +1,219 @@ +package com.alibaba.easyexcel.test.core.cache; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Proxy; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import com.alibaba.easyexcel.test.demo.read.DemoData; +import com.alibaba.easyexcel.test.util.TestFileUtil; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.enums.CacheLocationEnum; +import com.alibaba.excel.event.AnalysisEventListener; +import com.alibaba.excel.metadata.FieldCache; +import com.alibaba.excel.read.listener.PageReadListener; +import com.alibaba.excel.util.ClassUtils; +import com.alibaba.excel.util.FieldUtils; + +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +/** + * @author Jiaju Zhuang + */ +@TestMethodOrder(MethodOrderer.MethodName.class) +@Slf4j +public class CacheDataTest { + + private static File file07; + private static File fileCacheInvoke; + private static File fileCacheInvoke2; + private static File fileCacheInvokeMemory; + private static File fileCacheInvokeMemory2; + + @BeforeAll + public static void init() { + file07 = TestFileUtil.createNewFile("cache/cache.xlsx"); + fileCacheInvoke = TestFileUtil.createNewFile("cache/fileCacheInvoke.xlsx"); + fileCacheInvoke2 = TestFileUtil.createNewFile("cache/fileCacheInvoke2.xlsx"); + fileCacheInvokeMemory = TestFileUtil.createNewFile("cache/fileCacheInvokeMemory.xlsx"); + fileCacheInvokeMemory2 = TestFileUtil.createNewFile("cache/fileCacheInvokeMemory2.xlsx"); + } + + @Test + public void t01ReadAndWrite() throws Exception { + Field field = FieldUtils.getField(ClassUtils.class, "FIELD_THREAD_LOCAL", true); + ThreadLocal, FieldCache>> fieldThreadLocal = (ThreadLocal, FieldCache>>)field.get( + ClassUtils.class.newInstance()); + Assertions.assertNull(fieldThreadLocal.get()); + EasyExcel.write(file07, CacheData.class).sheet().doWrite(data()); + EasyExcel.read(file07, CacheData.class, new PageReadListener(dataList -> { + Assertions.assertNotNull(fieldThreadLocal.get()); + })) + .sheet() + .doRead(); + Assertions.assertNull(fieldThreadLocal.get()); + } + + @Test + public void t02ReadAndWriteInvoke() throws Exception { + EasyExcel.write(fileCacheInvoke, CacheInvokeData.class).sheet().doWrite(dataInvoke()); + EasyExcel.read(fileCacheInvoke, CacheInvokeData.class, new AnalysisEventListener() { + + @Override + public void invokeHeadMap(Map headMap, AnalysisContext context) { + Assertions.assertEquals(2, headMap.size()); + Assertions.assertEquals("姓名", headMap.get(0)); + Assertions.assertEquals("年龄", headMap.get(1)); + } + + @Override + public void invoke(CacheInvokeData data, AnalysisContext context) { + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + + } + }).sheet() + .doRead(); + + Field name = FieldUtils.getField(CacheInvokeData.class, "name", true); + ExcelProperty annotation = name.getAnnotation(ExcelProperty.class); + InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation); + Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues"); + memberValues.setAccessible(true); + Map map = (Map)memberValues.get(invocationHandler); + map.put("value", new String[] {"姓名2"}); + + EasyExcel.write(fileCacheInvoke2, CacheInvokeData.class).sheet().doWrite(dataInvoke()); + EasyExcel.read(fileCacheInvoke2, CacheInvokeData.class, new AnalysisEventListener() { + + @Override + public void invokeHeadMap(Map headMap, AnalysisContext context) { + Assertions.assertEquals(2, headMap.size()); + Assertions.assertEquals("姓名2", headMap.get(0)); + Assertions.assertEquals("年龄", headMap.get(1)); + } + + @Override + public void invoke(CacheInvokeData data, AnalysisContext context) { + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + + } + }).sheet() + .doRead(); + + } + + @Test + public void t03ReadAndWriteInvokeMemory() throws Exception { + EasyExcel.write(fileCacheInvokeMemory, CacheInvokeMemoryData.class) + .filedCacheLocation(CacheLocationEnum.MEMORY) + .sheet() + .doWrite(dataInvokeMemory()); + EasyExcel.read(fileCacheInvokeMemory, CacheInvokeMemoryData.class, + new AnalysisEventListener() { + + @Override + public void invokeHeadMap(Map headMap, AnalysisContext context) { + Assertions.assertEquals(2, headMap.size()); + Assertions.assertEquals("姓名", headMap.get(0)); + Assertions.assertEquals("年龄", headMap.get(1)); + } + + @Override + public void invoke(CacheInvokeMemoryData data, AnalysisContext context) { + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + + } + }) + .filedCacheLocation(CacheLocationEnum.MEMORY) + .sheet() + .doRead(); + + Field name = FieldUtils.getField(CacheInvokeMemoryData.class, "name", true); + ExcelProperty annotation = name.getAnnotation(ExcelProperty.class); + InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation); + Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues"); + memberValues.setAccessible(true); + Map map = (Map)memberValues.get(invocationHandler); + map.put("value", new String[] {"姓名2"}); + + EasyExcel.write(fileCacheInvokeMemory2, CacheInvokeMemoryData.class) + .filedCacheLocation(CacheLocationEnum.MEMORY) + .sheet() + .doWrite(dataInvokeMemory()); + EasyExcel.read(fileCacheInvokeMemory2, CacheInvokeMemoryData.class, + new AnalysisEventListener() { + + @Override + public void invokeHeadMap(Map headMap, AnalysisContext context) { + Assertions.assertEquals(2, headMap.size()); + Assertions.assertEquals("姓名", headMap.get(0)); + Assertions.assertEquals("年龄", headMap.get(1)); + } + + @Override + public void invoke(CacheInvokeMemoryData data, AnalysisContext context) { + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + + } + }) + .filedCacheLocation(CacheLocationEnum.MEMORY) + .sheet() + .doRead(); + + } + + private List data() { + List list = new ArrayList(); + for (int i = 0; i < 10; i++) { + CacheData simpleData = new CacheData(); + simpleData.setName("姓名" + i); + simpleData.setAge((long)i); + list.add(simpleData); + } + return list; + } + + private List dataInvoke() { + List list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + CacheInvokeData simpleData = new CacheInvokeData(); + simpleData.setName("姓名" + i); + simpleData.setAge((long)i); + list.add(simpleData); + } + return list; + } + + private List dataInvokeMemory() { + List list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + CacheInvokeMemoryData simpleData = new CacheInvokeMemoryData(); + simpleData.setName("姓名" + i); + simpleData.setAge((long)i); + list.add(simpleData); + } + return list; + } +} diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/cache/CacheInvokeData.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/cache/CacheInvokeData.java new file mode 100644 index 00000000..6506c8af --- /dev/null +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/cache/CacheInvokeData.java @@ -0,0 +1,21 @@ +package com.alibaba.easyexcel.test.core.cache; + +import com.alibaba.excel.annotation.ExcelProperty; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +/** + * @author Jiaju Zhuang + */ +@Getter +@Setter +@EqualsAndHashCode +public class CacheInvokeData { + @ExcelProperty("姓名") + private String name; + + @ExcelProperty("年龄") + private Long age; +} diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/cache/CacheInvokeMemoryData.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/cache/CacheInvokeMemoryData.java new file mode 100644 index 00000000..25fa25de --- /dev/null +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/cache/CacheInvokeMemoryData.java @@ -0,0 +1,21 @@ +package com.alibaba.easyexcel.test.core.cache; + +import com.alibaba.excel.annotation.ExcelProperty; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +/** + * @author Jiaju Zhuang + */ +@Getter +@Setter +@EqualsAndHashCode +public class CacheInvokeMemoryData { + @ExcelProperty("姓名") + private String name; + + @ExcelProperty("年龄") + private Long age; +} diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java index acafba47..8f4d079d 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java @@ -8,7 +8,7 @@ import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.fastjson2.JSON; -import org.junit.Assert; +import org.junit.jupiter.api.Assertions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,16 +26,16 @@ public class CellDataDataListener extends AnalysisEventListener> headMap, AnalysisContext context) { String head = headMap.get(0).getStringValue(); - Assert.assertNotEquals("姓名", head); + Assertions.assertNotEquals("姓名", head); } @Override @@ -79,7 +79,7 @@ public class CharsetDataTest { @Override public void invokeHead(Map> headMap, AnalysisContext context) { String head = headMap.get(0).getStringValue(); - Assert.assertEquals("姓名", head); + Assertions.assertEquals("姓名", head); } @Override @@ -89,10 +89,10 @@ public class CharsetDataTest { @Override public void doAfterAllAnalysed(AnalysisContext context) { - Assert.assertEquals(dataList.size(), 10); + Assertions.assertEquals(dataList.size(), 10); CharsetData charsetData = dataList.get(0); - Assert.assertEquals("姓名0", charsetData.getName()); - Assert.assertEquals(0, (long)charsetData.getAge()); + Assertions.assertEquals("姓名0", charsetData.getName()); + Assertions.assertEquals(0, (long)charsetData.getAge()); } }).charset(charset).sheet().doRead(); } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java index df5a2dbb..df621680 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java @@ -6,30 +6,27 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import com.alibaba.easyexcel.test.core.large.LargeData; -import com.alibaba.easyexcel.test.core.large.LargeDataListener; import com.alibaba.easyexcel.test.core.simple.SimpleData; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.cache.Ehcache; -import com.alibaba.excel.constant.EasyExcelConstants; import com.alibaba.excel.enums.ReadDefaultReturnEnum; import com.alibaba.excel.util.FileUtils; import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; import org.apache.poi.util.TempFile; -import org.junit.Assert; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** * Compatible with some special files * * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) @Slf4j public class CompatibilityTest { @@ -38,9 +35,9 @@ public class CompatibilityTest { // https://github.com/alibaba/easyexcel/issues/2236 List> list = EasyExcel.read(TestFileUtil.getPath() + "compatibility/t01.xls").sheet() .doReadSync(); - Assert.assertEquals(2, list.size()); + Assertions.assertEquals(2, list.size()); Map row1 = list.get(1); - Assert.assertEquals("Q235(碳钢)", row1.get(0)); + Assertions.assertEquals("Q235(碳钢)", row1.get(0)); } @Test @@ -49,9 +46,9 @@ public class CompatibilityTest { List> list = EasyExcel.read(TestFileUtil.getPath() + "compatibility/t02.xlsx").sheet() .headRowNumber(0).doReadSync(); log.info("data:{}", JSON.toJSONString(list)); - Assert.assertEquals(3, list.size()); + Assertions.assertEquals(3, list.size()); Map row2 = list.get(2); - Assert.assertEquals("1,2-戊二醇", row2.get(2)); + Assertions.assertEquals("1,2-戊二醇", row2.get(2)); } @Test @@ -60,9 +57,9 @@ public class CompatibilityTest { List> list = EasyExcel.read(TestFileUtil.getPath() + "compatibility/t03.xlsx").sheet() .doReadSync(); log.info("data:{}", JSON.toJSONString(list)); - Assert.assertEquals(1, list.size()); + Assertions.assertEquals(1, list.size()); Map row0 = list.get(0); - Assert.assertEquals(12, row0.size()); + Assertions.assertEquals(12, row0.size()); } @Test @@ -71,9 +68,9 @@ public class CompatibilityTest { List> list = EasyExcel.read(TestFileUtil.getPath() + "compatibility/t04.xlsx").sheet() .doReadSync(); log.info("data:{}", JSON.toJSONString(list)); - Assert.assertEquals(56, list.size()); + Assertions.assertEquals(56, list.size()); Map row0 = list.get(0); - Assert.assertEquals("QQSJK28F152A012242S0081", row0.get(5)); + Assertions.assertEquals("QQSJK28F152A012242S0081", row0.get(5)); } @Test @@ -85,11 +82,11 @@ public class CompatibilityTest { .sheet() .doReadSync(); log.info("data:{}", JSON.toJSONString(list)); - Assert.assertEquals("2023-01-01 00:00:00", list.get(0).get(0)); - Assert.assertEquals("2023-01-01 00:00:00", list.get(1).get(0)); - Assert.assertEquals("2023-01-01 00:00:00", list.get(2).get(0)); - Assert.assertEquals("2023-01-01 00:00:01", list.get(3).get(0)); - Assert.assertEquals("2023-01-01 00:00:01", list.get(4).get(0)); + Assertions.assertEquals("2023-01-01 00:00:00", list.get(0).get(0)); + Assertions.assertEquals("2023-01-01 00:00:00", list.get(1).get(0)); + Assertions.assertEquals("2023-01-01 00:00:00", list.get(2).get(0)); + Assertions.assertEquals("2023-01-01 00:00:01", list.get(3).get(0)); + Assertions.assertEquals("2023-01-01 00:00:01", list.get(4).get(0)); } @Test @@ -101,7 +98,7 @@ public class CompatibilityTest { .sheet() .doReadSync(); log.info("data:{}", JSON.toJSONString(list)); - Assert.assertEquals("2087.03", list.get(0).get(2)); + Assertions.assertEquals("2087.03", list.get(0).get(2)); } @Test @@ -114,14 +111,14 @@ public class CompatibilityTest { .sheet() .doReadSync(); log.info("data:{}", JSON.toJSONString(list)); - Assert.assertEquals(0, new BigDecimal("24.1998124").compareTo((BigDecimal)list.get(0).get(11))); + Assertions.assertEquals(0, new BigDecimal("24.1998124").compareTo((BigDecimal)list.get(0).get(11))); list = EasyExcel .read(TestFileUtil.getPath() + "compatibility/t07.xlsx") .sheet() .doReadSync(); log.info("data:{}", JSON.toJSONString(list)); - Assert.assertEquals("24.20", list.get(0).get(11)); + Assertions.assertEquals("24.20", list.get(0).get(11)); } @Test @@ -137,7 +134,7 @@ public class CompatibilityTest { .readCache(new Ehcache(null, 20)) .sheet() .doReadSync(); - Assert.assertEquals(10L, list.size()); + Assertions.assertEquals(10L, list.size()); FileUtils.delete(new File(System.getProperty(TempFile.JAVA_IO_TMPDIR))); @@ -145,7 +142,21 @@ public class CompatibilityTest { .readCache(new Ehcache(null, 20)) .sheet() .doReadSync(); - Assert.assertEquals(10L, list.size()); + Assertions.assertEquals(10L, list.size()); + } + + @Test + public void t09() { + // `SH_x005f_x000D_Z002` exists in `ShardingString.xml` and needs to be replaced by: `SH_x000D_Z002` + File file = TestFileUtil.readFile("compatibility/t09.xlsx"); + List> list = EasyExcel.read(file) + .headRowNumber(0) + .sheet() + .doReadSync(); + log.info("data:{}", JSON.toJSONString(list)); + Assertions.assertEquals(1, list.size()); + + Assertions.assertEquals("SH_x000D_Z002", list.get(0).get(0)); } private List data() { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java index a7327d42..adb4b164 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java @@ -2,19 +2,15 @@ package com.alibaba.easyexcel.test.core.converter; import java.math.BigDecimal; import java.math.BigInteger; -import java.text.ParseException; -import java.time.LocalDate; import java.util.ArrayList; import java.util.List; import com.alibaba.easyexcel.test.util.TestUtil; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; -import com.alibaba.excel.exception.ExcelCommonException; -import com.alibaba.excel.util.DateUtils; import com.alibaba.fastjson2.JSON; -import org.junit.Assert; +import org.junit.jupiter.api.Assertions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,22 +28,22 @@ public class ConverterDataListener extends AnalysisEventListener context = new WriteConverterContext<>(); context.setValue(95.62F); WriteCellData writeCellData = floatNumberConverter.convertToExcelData(context); - Assert.assertEquals(0, writeCellData.getNumberValue().compareTo(new BigDecimal("95.62"))); + Assertions.assertEquals(0, writeCellData.getNumberValue().compareTo(new BigDecimal("95.62"))); } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java index f56eab46..eae1fa92 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java @@ -13,7 +13,7 @@ import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.util.DateUtils; import com.alibaba.fastjson2.JSON; -import org.junit.Assert; +import org.junit.jupiter.api.Assertions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,54 +31,56 @@ public class ReadAllConverterDataListener extends AnalysisEventListener { @Override public void doAfterAllAnalysed(AnalysisContext context) { - Assert.assertEquals(list.size(), 10); - Assert.assertEquals(list.get(0).getName(), "姓名0"); - Assert.assertEquals((int)(context.readSheetHolder().getSheetNo()), 0); - Assert.assertEquals( + Assertions.assertEquals(list.size(), 10); + Assertions.assertEquals(list.get(0).getName(), "姓名0"); + Assertions.assertEquals((int)(context.readSheetHolder().getSheetNo()), 0); + Assertions.assertEquals( context.readSheetHolder().getExcelReadHeadProperty().getHeadMap().get(0).getHeadNameList().get(0), "姓名"); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java index b7f63cf0..8c2f0dca 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java @@ -14,15 +14,15 @@ import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.support.ExcelTypeEnum; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class EncryptDataTest { private static File file07; @@ -39,7 +39,7 @@ public class EncryptDataTest { System.out.println(decimalFormat.format(bigDecimal)); } - @BeforeClass + @BeforeAll public static void init() { file07 = TestFileUtil.createNewFile("encrypt07.xlsx"); file03 = TestFileUtil.createNewFile("encrypt03.xls"); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataListener.java index 8009d4db..95b6ddc3 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataListener.java @@ -3,14 +3,14 @@ package com.alibaba.easyexcel.test.core.exception; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Assertions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ @@ -38,10 +38,10 @@ public class ExceptionDataListener extends AnalysisEventListener @Override public void doAfterAllAnalysed(AnalysisContext context) { - Assert.assertEquals(list.size(), 8); - Assert.assertEquals(list.get(0).getName(), "姓名0"); - Assert.assertEquals((int)(context.readSheetHolder().getSheetNo()), 0); - Assert.assertEquals( + Assertions.assertEquals(list.size(), 8); + Assertions.assertEquals(list.get(0).getName(), "姓名0"); + Assertions.assertEquals((int)(context.readSheetHolder().getSheetNo()), 0); + Assertions.assertEquals( context.readSheetHolder().getExcelReadHeadProperty().getHeadMap().get(0).getHeadNameList().get(0), "姓名"); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataTest.java index fb1ef815..b06a105e 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataTest.java @@ -9,16 +9,16 @@ 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; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class ExceptionDataTest { private static File file07; @@ -27,7 +27,7 @@ public class ExceptionDataTest { private static File fileException03; private static File fileCsv; - @BeforeClass + @BeforeAll public static void init() { file07 = TestFileUtil.createNewFile("exception.xlsx"); file03 = TestFileUtil.createNewFile("exception.xls"); @@ -63,10 +63,10 @@ public class ExceptionDataTest { 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( + ArithmeticException exception = Assertions.assertThrows(ArithmeticException.class, () -> EasyExcel.read( new FileInputStream(file), ExceptionData.class, new ExceptionThrowDataListener()).sheet().doRead()); - Assert.assertEquals("/ by zero",exception.getMessage()); + Assertions.assertEquals("/ by zero", exception.getMessage()); } private void readAndWrite(File file) throws Exception { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeDataTest.java index 9bf69821..314b5de4 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeDataTest.java @@ -1,25 +1,16 @@ package com.alibaba.easyexcel.test.core.excludeorinclude; -import java.io.File; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; +import org.junit.jupiter.api.*; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import java.io.File; +import java.util.*; /** * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class ExcludeOrIncludeDataTest { private static File excludeIndex07; @@ -34,8 +25,15 @@ public class ExcludeOrIncludeDataTest { private static File includeFieldName07; private static File includeFieldName03; private static File includeFieldNameCsv; + private static File includeFieldNameOrder07; + private static File includeFieldNameOrder03; + private static File includeFieldNameOrderCsv; - @BeforeClass + private static File includeFieldNameOrderIndex07; + private static File includeFieldNameOrderIndex03; + private static File includeFieldNameOrderIndexCsv; + + @BeforeAll public static void init() { excludeIndex07 = TestFileUtil.createNewFile("excludeIndex.xlsx"); excludeIndex03 = TestFileUtil.createNewFile("excludeIndex.xls"); @@ -49,6 +47,12 @@ public class ExcludeOrIncludeDataTest { includeFieldName07 = TestFileUtil.createNewFile("includeFieldName.xlsx"); includeFieldName03 = TestFileUtil.createNewFile("includeFieldName.xls"); includeFieldNameCsv = TestFileUtil.createNewFile("includeFieldName.csv"); + includeFieldNameOrder07 = TestFileUtil.createNewFile("includeFieldNameOrder.xlsx"); + includeFieldNameOrder03 = TestFileUtil.createNewFile("includeFieldNameOrder.xls"); + includeFieldNameOrderCsv = TestFileUtil.createNewFile("includeFieldNameOrder.csv"); + includeFieldNameOrderIndex07 = TestFileUtil.createNewFile("includeFieldNameOrderIndex.xlsx"); + includeFieldNameOrderIndex03 = TestFileUtil.createNewFile("includeFieldNameOrderIndex.xls"); + includeFieldNameOrderIndexCsv = TestFileUtil.createNewFile("includeFieldNameOrderIndex.csv"); } @Test @@ -81,7 +85,6 @@ public class ExcludeOrIncludeDataTest { excludeFieldName(excludeFieldNameCsv); } - @Test public void t21IncludeIndex07() { includeIndex(includeIndex07); @@ -112,18 +115,48 @@ public class ExcludeOrIncludeDataTest { includeFieldName(includeFieldNameCsv); } + @Test + public void t41IncludeFieldNameOrder07() { + includeFieldNameOrder(includeFieldNameOrder07); + } + + @Test + public void t42IncludeFieldNameOrder03() { + includeFieldNameOrder(includeFieldNameOrder03); + } + + @Test + public void t43IncludeFieldNameOrderCsv() { + includeFieldNameOrder(includeFieldNameOrderCsv); + } + + @Test + public void t41IncludeFieldNameOrderIndex07() { + includeFieldNameOrderIndex(includeFieldNameOrderIndex07); + } + + @Test + public void t42IncludeFieldNameOrderIndex03() { + includeFieldNameOrderIndex(includeFieldNameOrderIndex03); + } + + @Test + public void t43IncludeFieldNameOrderIndexCsv() { + includeFieldNameOrderIndex(includeFieldNameOrderIndexCsv); + } + private void excludeIndex(File file) { Set excludeColumnIndexes = new HashSet(); excludeColumnIndexes.add(0); excludeColumnIndexes.add(3); EasyExcel.write(file, ExcludeOrIncludeData.class).excludeColumnIndexes(excludeColumnIndexes).sheet() - .doWrite(data()); + .doWrite(data()); List> dataMap = EasyExcel.read(file).sheet().doReadSync(); - Assert.assertEquals(1, dataMap.size()); + Assertions.assertEquals(1, dataMap.size()); Map record = dataMap.get(0); - Assert.assertEquals(2, record.size()); - Assert.assertEquals("column2", record.get(0)); - Assert.assertEquals("column3", record.get(1)); + Assertions.assertEquals(2, record.size()); + Assertions.assertEquals("column2", record.get(0)); + Assertions.assertEquals("column3", record.get(1)); } @@ -133,12 +166,12 @@ public class ExcludeOrIncludeDataTest { excludeColumnFieldNames.add("column3"); excludeColumnFieldNames.add("column4"); EasyExcel.write(file, ExcludeOrIncludeData.class).excludeColumnFieldNames(excludeColumnFieldNames).sheet() - .doWrite(data()); + .doWrite(data()); List> dataMap = EasyExcel.read(file).sheet().doReadSync(); - Assert.assertEquals(1, dataMap.size()); + Assertions.assertEquals(1, dataMap.size()); Map record = dataMap.get(0); - Assert.assertEquals(1, record.size()); - Assert.assertEquals("column2", record.get(0)); + Assertions.assertEquals(1, record.size()); + Assertions.assertEquals("column2", record.get(0)); } @@ -147,13 +180,13 @@ public class ExcludeOrIncludeDataTest { includeColumnIndexes.add(1); includeColumnIndexes.add(2); EasyExcel.write(file, ExcludeOrIncludeData.class).includeColumnIndexes(includeColumnIndexes).sheet() - .doWrite(data()); + .doWrite(data()); List> dataMap = EasyExcel.read(file).sheet().doReadSync(); - Assert.assertEquals(1, dataMap.size()); + Assertions.assertEquals(1, dataMap.size()); Map record = dataMap.get(0); - Assert.assertEquals(2, record.size()); - Assert.assertEquals("column2", record.get(0)); - Assert.assertEquals("column3", record.get(1)); + Assertions.assertEquals(2, record.size()); + Assertions.assertEquals("column2", record.get(0)); + Assertions.assertEquals("column3", record.get(1)); } @@ -161,14 +194,56 @@ public class ExcludeOrIncludeDataTest { Set includeColumnFieldNames = new HashSet(); includeColumnFieldNames.add("column2"); includeColumnFieldNames.add("column3"); - EasyExcel.write(file, ExcludeOrIncludeData.class).includeColumnFieldNames(includeColumnFieldNames).sheet() - .doWrite(data()); + EasyExcel.write(file, ExcludeOrIncludeData.class) + .sheet() + .includeColumnFieldNames(includeColumnFieldNames) + .doWrite(data()); + List> dataMap = EasyExcel.read(file).sheet().doReadSync(); + Assertions.assertEquals(1, dataMap.size()); + Map record = dataMap.get(0); + Assertions.assertEquals(2, record.size()); + Assertions.assertEquals("column2", record.get(0)); + Assertions.assertEquals("column3", record.get(1)); + } + + private void includeFieldNameOrderIndex(File file) { + List includeColumnIndexes = new ArrayList<>(); + includeColumnIndexes.add(3); + includeColumnIndexes.add(1); + includeColumnIndexes.add(2); + includeColumnIndexes.add(0); + EasyExcel.write(file, ExcludeOrIncludeData.class) + .includeColumnIndexes(includeColumnIndexes) + .orderByIncludeColumn(true). + sheet() + .doWrite(data()); + List> dataMap = EasyExcel.read(file).sheet().doReadSync(); + Assertions.assertEquals(1, dataMap.size()); + Map record = dataMap.get(0); + Assertions.assertEquals(4, record.size()); + Assertions.assertEquals("column4", record.get(0)); + Assertions.assertEquals("column2", record.get(1)); + Assertions.assertEquals("column3", record.get(2)); + Assertions.assertEquals("column1", record.get(3)); + } + + private void includeFieldNameOrder(File file) { + List includeColumnFieldNames = new ArrayList<>(); + includeColumnFieldNames.add("column4"); + includeColumnFieldNames.add("column2"); + includeColumnFieldNames.add("column3"); + EasyExcel.write(file, ExcludeOrIncludeData.class) + .includeColumnFieldNames(includeColumnFieldNames) + .orderByIncludeColumn(true). + sheet() + .doWrite(data()); List> dataMap = EasyExcel.read(file).sheet().doReadSync(); - Assert.assertEquals(1, dataMap.size()); + Assertions.assertEquals(1, dataMap.size()); Map record = dataMap.get(0); - Assert.assertEquals(2, record.size()); - Assert.assertEquals("column2", record.get(0)); - Assert.assertEquals("column3", record.get(1)); + Assertions.assertEquals(3, record.size()); + Assertions.assertEquals("column4", record.get(0)); + Assertions.assertEquals("column2", record.get(1)); + Assertions.assertEquals("column3", record.get(2)); } private List data() { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataListener.java index d58c4cdb..e11b1f16 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataListener.java @@ -1,14 +1,14 @@ package com.alibaba.easyexcel.test.core.extra; -import org.junit.Assert; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.metadata.CellExtra; import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Assertions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ @@ -26,28 +26,28 @@ public class ExtraDataListener extends AnalysisEventListener { LOGGER.info("extra data:{}", JSON.toJSONString(extra)); switch (extra.getType()) { case COMMENT: - Assert.assertEquals("批注的内容", extra.getText()); - Assert.assertEquals(4, (int)extra.getRowIndex()); - Assert.assertEquals(0, (int)extra.getColumnIndex()); + Assertions.assertEquals("批注的内容", extra.getText()); + Assertions.assertEquals(4, (int)extra.getRowIndex()); + Assertions.assertEquals(0, (int)extra.getColumnIndex()); break; case HYPERLINK: if ("Sheet1!A1".equals(extra.getText())) { - Assert.assertEquals(1, (int)extra.getRowIndex()); - Assert.assertEquals(0, (int)extra.getColumnIndex()); + Assertions.assertEquals(1, (int)extra.getRowIndex()); + Assertions.assertEquals(0, (int)extra.getColumnIndex()); } else if ("Sheet2!A1".equals(extra.getText())) { - Assert.assertEquals(2, (int)extra.getFirstRowIndex()); - Assert.assertEquals(0, (int)extra.getFirstColumnIndex()); - Assert.assertEquals(3, (int)extra.getLastRowIndex()); - Assert.assertEquals(1, (int)extra.getLastColumnIndex()); + Assertions.assertEquals(2, (int)extra.getFirstRowIndex()); + Assertions.assertEquals(0, (int)extra.getFirstColumnIndex()); + Assertions.assertEquals(3, (int)extra.getLastRowIndex()); + Assertions.assertEquals(1, (int)extra.getLastColumnIndex()); } else { - Assert.fail("Unknown hyperlink!"); + Assertions.fail("Unknown hyperlink!"); } break; case MERGE: - Assert.assertEquals(5, (int)extra.getFirstRowIndex()); - Assert.assertEquals(0, (int)extra.getFirstColumnIndex()); - Assert.assertEquals(6, (int)extra.getLastRowIndex()); - Assert.assertEquals(1, (int)extra.getLastColumnIndex()); + Assertions.assertEquals(5, (int)extra.getFirstRowIndex()); + Assertions.assertEquals(0, (int)extra.getFirstColumnIndex()); + Assertions.assertEquals(6, (int)extra.getLastRowIndex()); + Assertions.assertEquals(1, (int)extra.getLastColumnIndex()); break; default: } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataTest.java index 92f35574..9409a8df 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataTest.java @@ -2,12 +2,6 @@ package com.alibaba.easyexcel.test.core.extra; import java.io.File; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.context.AnalysisContext; @@ -16,6 +10,12 @@ import com.alibaba.excel.metadata.CellExtra; import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ @@ -26,7 +26,7 @@ public class ExtraDataTest { private static File extraRelationships; - @BeforeClass + @BeforeAll public static void init() { file03 = TestFileUtil.readFile("extra" + File.separator + "extra.xls"); file07 = TestFileUtil.readFile("extra" + File.separator + "extra.xlsx"); @@ -60,13 +60,13 @@ public class ExtraDataTest { switch (extra.getType()) { case HYPERLINK: if ("222222222".equals(extra.getText())) { - Assert.assertEquals(1, (int)extra.getRowIndex()); - Assert.assertEquals(0, (int)extra.getColumnIndex()); + Assertions.assertEquals(1, (int)extra.getRowIndex()); + Assertions.assertEquals(0, (int)extra.getColumnIndex()); } else if ("333333333333".equals(extra.getText())) { - Assert.assertEquals(1, (int)extra.getRowIndex()); - Assert.assertEquals(1, (int)extra.getColumnIndex()); + Assertions.assertEquals(1, (int)extra.getRowIndex()); + Assertions.assertEquals(1, (int)extra.getColumnIndex()); } else { - Assert.fail("Unknown hyperlink!"); + Assertions.fail("Unknown hyperlink!"); } break; default: diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java index 90772691..6d88f848 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java @@ -16,16 +16,16 @@ 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.Assert; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class FillDataTest { private static File file07; @@ -51,7 +51,7 @@ public class FillDataTest { private static File fileComposite03; private static File compositeFillTemplate03; - @BeforeClass + @BeforeAll public static void init() { file07 = TestFileUtil.createNewFile("fill07.xlsx"); file03 = TestFileUtil.createNewFile("fill03.xls"); @@ -89,9 +89,9 @@ public class FillDataTest { @Test public void t03FillCsv() { - ExcelGenerateException excelGenerateException = Assert.assertThrows(ExcelGenerateException.class, + ExcelGenerateException excelGenerateException = Assertions.assertThrows(ExcelGenerateException.class, () -> fill(fileCsv, simpleTemplateCsv)); - Assert.assertEquals("csv cannot use template.", excelGenerateException.getMessage()); + Assertions.assertEquals("csv cannot use template.", excelGenerateException.getMessage()); } @Test @@ -159,11 +159,11 @@ public class FillDataTest { List list = EasyExcel.read(file).ignoreEmptyRow(false).sheet().headRowNumber(0).doReadSync(); Map map0 = (Map)list.get(0); - Assert.assertEquals("张三", map0.get(21)); + Assertions.assertEquals("张三", map0.get(21)); Map map27 = (Map)list.get(27); - Assert.assertEquals("张三", map27.get(0)); + Assertions.assertEquals("张三", map27.get(0)); Map map29 = (Map)list.get(29); - Assert.assertEquals("张三", map29.get(3)); + Assertions.assertEquals("张三", map29.get(3)); } private void horizontalFill(File file, File template) { @@ -179,9 +179,9 @@ public class FillDataTest { } List list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync(); - Assert.assertEquals(list.size(), 5L); + Assertions.assertEquals(list.size(), 5L); Map map0 = (Map)list.get(0); - Assert.assertEquals("张三", map0.get(2)); + Assertions.assertEquals("张三", map0.get(2)); } private void complexFill(File file, File template) { @@ -196,9 +196,9 @@ public class FillDataTest { excelWriter.fill(map, writeSheet); } List list = EasyExcel.read(file).sheet().headRowNumber(3).doReadSync(); - Assert.assertEquals(list.size(), 21L); + Assertions.assertEquals(list.size(), 21L); Map map19 = (Map)list.get(19); - Assert.assertEquals("张三", map19.get(0)); + Assertions.assertEquals("张三", map19.get(0)); } private void fill(File file, File template) { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/annotation/FillAnnotationDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/annotation/FillAnnotationDataTest.java index 8d1d56bc..2e8275b5 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/annotation/FillAnnotationDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/annotation/FillAnnotationDataTest.java @@ -22,17 +22,17 @@ import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFPicture; import org.apache.poi.xssf.usermodel.XSSFShape; import org.apache.poi.xssf.usermodel.XSSFSheet; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker; /** * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class FillAnnotationDataTest { private static File file07; @@ -40,7 +40,7 @@ public class FillAnnotationDataTest { private static File fileTemplate07; private static File fileTemplate03; - @BeforeClass + @BeforeAll public static void init() { file07 = TestFileUtil.createNewFile("fillAnnotation07.xlsx"); file03 = TestFileUtil.createNewFile("fillAnnotation03.xls"); @@ -65,14 +65,14 @@ public class FillAnnotationDataTest { Sheet sheet = workbook.getSheetAt(0); Row row1 = sheet.getRow(1); - Assert.assertEquals(2000, row1.getHeight(), 0); + Assertions.assertEquals(2000, row1.getHeight(), 0); Cell cell10 = row1.getCell(0); Date date = cell10.getDateCellValue(); - Assert.assertEquals(DateUtils.parseDate("2020-01-01 01:01:01").getTime(), date.getTime()); + Assertions.assertEquals(DateUtils.parseDate("2020-01-01 01:01:01").getTime(), date.getTime()); String dataFormatString = cell10.getCellStyle().getDataFormatString(); - Assert.assertEquals("yyyy年MM月dd日HH时mm分ss秒", dataFormatString); + Assertions.assertEquals("yyyy年MM月dd日HH时mm分ss秒", dataFormatString); Cell cell11 = row1.getCell(1); - Assert.assertEquals(99.99, cell11.getNumericCellValue(), 2); + Assertions.assertEquals(99.99, cell11.getNumericCellValue(), 2); boolean hasMerge = false; for (CellRangeAddress mergedRegion : sheet.getMergedRegions()) { if (mergedRegion.getFirstRow() == 1 && mergedRegion.getLastRow() == 1 @@ -81,25 +81,25 @@ public class FillAnnotationDataTest { break; } } - Assert.assertTrue(hasMerge); + Assertions.assertTrue(hasMerge); if (sheet instanceof XSSFSheet) { XSSFSheet xssfSheet = (XSSFSheet)sheet; List shapeList = xssfSheet.getDrawingPatriarch().getShapes(); XSSFShape shape0 = shapeList.get(0); - Assert.assertTrue(shape0 instanceof XSSFPicture); + Assertions.assertTrue(shape0 instanceof XSSFPicture); XSSFPicture picture0 = (XSSFPicture)shape0; CTMarker ctMarker0 = picture0.getPreferredSize().getFrom(); - Assert.assertEquals(1, ctMarker0.getRow()); - Assert.assertEquals(4, ctMarker0.getCol()); + Assertions.assertEquals(1, ctMarker0.getRow()); + Assertions.assertEquals(4, ctMarker0.getCol()); } else { HSSFSheet hssfSheet = (HSSFSheet)sheet; List shapeList = hssfSheet.getDrawingPatriarch().getChildren(); HSSFShape shape0 = shapeList.get(0); - Assert.assertTrue(shape0 instanceof HSSFPicture); + Assertions.assertTrue(shape0 instanceof HSSFPicture); HSSFPicture picture0 = (HSSFPicture)shape0; HSSFClientAnchor anchor = (HSSFClientAnchor)picture0.getAnchor(); - Assert.assertEquals(1, anchor.getRow1()); - Assert.assertEquals(4, anchor.getCol1()); + Assertions.assertEquals(1, anchor.getRow1()); + Assertions.assertEquals(4, anchor.getCol1()); } } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedTest.java index 13367904..406255bc 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedTest.java @@ -25,16 +25,16 @@ import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class FillStyleAnnotatedTest { private static File FillStyleAnnotated07; @@ -42,7 +42,7 @@ public class FillStyleAnnotatedTest { private static File fileStyleTemplate07; private static File fileStyleTemplate03; - @BeforeClass + @BeforeAll public static void init() { FillStyleAnnotated07 = TestFileUtil.createNewFile("FillStyleAnnotated07.xlsx"); FillStyleAnnotated03 = TestFileUtil.createNewFile("FillStyleAnnotated03.xls"); @@ -61,46 +61,47 @@ public class FillStyleAnnotatedTest { private void t01Fill07check(XSSFRow row) { XSSFCell cell0 = row.getCell(0); - Assert.assertEquals("张三", cell0.getStringCellValue()); - Assert.assertEquals(49, cell0.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFFFF00", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF808000", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell0.getCellStyle().getFont().getBold()); + Assertions.assertEquals("张三", cell0.getStringCellValue()); + Assertions.assertEquals(49, cell0.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFFFF00", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF808000", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell0.getCellStyle().getFont().getBold()); XSSFCell cell1 = row.getCell(1); - Assert.assertEquals(5.2, cell1.getNumericCellValue(), 1); - Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF0000", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF800000", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell1.getCellStyle().getFont().getBold()); + Assertions.assertEquals(5.2, cell1.getNumericCellValue(), 1); + Assertions.assertEquals(0, cell1.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFF0000", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF800000", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell1.getCellStyle().getFont().getBold()); XSSFCell cell2 = row.getCell(2); - Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); - Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); - Assert.assertEquals("FF008000", cell2.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF003300", cell2.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell2.getCellStyle().getFont().getBold()); + Assertions.assertEquals("2020-01-01 01:01:01", + DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); + Assertions.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); + Assertions.assertEquals("FF008000", cell2.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF003300", cell2.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell2.getCellStyle().getFont().getBold()); XSSFCell cell3 = row.getCell(3); - Assert.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); - Assert.assertEquals(0, cell3.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF0000", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FFEEECE1", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell3.getCellStyle().getFont().getBold()); + Assertions.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); + Assertions.assertEquals(0, cell3.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFF0000", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FFEEECE1", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell3.getCellStyle().getFont().getBold()); XSSFCell cell4 = row.getCell(4); - Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); - Assert.assertEquals(0, cell4.getCellStyle().getDataFormat()); - Assert.assertEquals("FFC00000", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF000000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertFalse(cell4.getCellStyle().getFont().getBold()); + Assertions.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); + Assertions.assertEquals(0, cell4.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFC00000", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF000000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertFalse(cell4.getCellStyle().getFont().getBold()); XSSFCell cell5 = row.getCell(5); - Assert.assertEquals("空", cell5.getStringCellValue()); - Assert.assertEquals(0, cell5.getCellStyle().getDataFormat()); - Assert.assertEquals("FFF79646", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF8064A2", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertFalse(cell5.getCellStyle().getFont().getBold()); + Assertions.assertEquals("空", cell5.getStringCellValue()); + Assertions.assertEquals(0, cell5.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFF79646", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF8064A2", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertFalse(cell5.getCellStyle().getFont().getBold()); } @Test @@ -114,150 +115,153 @@ public class FillStyleAnnotatedTest { private void t02Fill03check(HSSFWorkbook workbook, HSSFRow row) { HSSFCell cell0 = row.getCell(0); - Assert.assertEquals("张三", cell0.getStringCellValue()); - Assert.assertEquals(49, cell0.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF:FFFF:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("8080:8080:0", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("张三", cell0.getStringCellValue()); + Assertions.assertEquals(49, cell0.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFF:FFFF:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("8080:8080:0", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertTrue(cell0.getCellStyle().getFont(workbook).getBold()); + Assertions.assertTrue(cell0.getCellStyle().getFont(workbook).getBold()); HSSFCell cell1 = row.getCell(1); - Assert.assertEquals(5.2, cell1.getNumericCellValue(), 1); - Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF:0:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("8080:0:0", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals(5.2, cell1.getNumericCellValue(), 1); + Assertions.assertEquals(0, cell1.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFF:0:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("8080:0:0", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertTrue(cell1.getCellStyle().getFont(workbook).getBold()); + Assertions.assertTrue(cell1.getCellStyle().getFont(workbook).getBold()); HSSFCell cell2 = row.getCell(2); - Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); - Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); - Assert.assertEquals("0:8080:0", cell2.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("0:3333:0", cell2.getCellStyle().getFont(workbook).getHSSFColor(workbook).getHexString()); - Assert.assertTrue(cell2.getCellStyle().getFont(workbook).getBold()); + Assertions.assertEquals("2020-01-01 01:01:01", + DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); + Assertions.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); + Assertions.assertEquals("0:8080:0", cell2.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("0:3333:0", cell2.getCellStyle().getFont(workbook).getHSSFColor(workbook) + .getHexString()); + Assertions.assertTrue(cell2.getCellStyle().getFont(workbook).getBold()); HSSFCell cell3 = row.getCell(3); - Assert.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); - Assert.assertEquals(0, cell3.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF:0:0", cell3.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("FFFF:FFFF:9999", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); + Assertions.assertEquals(0, cell3.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFF:0:0", cell3.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("FFFF:FFFF:9999", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertTrue(cell3.getCellStyle().getFont(workbook).getBold()); + Assertions.assertTrue(cell3.getCellStyle().getFont(workbook).getBold()); HSSFCell cell4 = row.getCell(4); - Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); - Assert.assertEquals(0, cell4.getCellStyle().getDataFormat()); - Assert.assertEquals("9999:3333:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("3333:3333:3333", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); + Assertions.assertEquals(0, cell4.getCellStyle().getDataFormat()); + Assertions.assertEquals("9999:3333:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("3333:3333:3333", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertFalse(cell4.getCellStyle().getFont(workbook).getBold()); + Assertions.assertFalse(cell4.getCellStyle().getFont(workbook).getBold()); HSSFCell cell5 = row.getCell(5); - Assert.assertEquals("空", cell5.getStringCellValue()); - Assert.assertEquals(0, cell5.getCellStyle().getDataFormat()); - Assert.assertEquals("9999:3333:0", cell5.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("CCCC:9999:FFFF", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("空", cell5.getStringCellValue()); + Assertions.assertEquals(0, cell5.getCellStyle().getDataFormat()); + Assertions.assertEquals("9999:3333:0", cell5.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("CCCC:9999:FFFF", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertFalse(cell5.getCellStyle().getFont(workbook).getBold()); + Assertions.assertFalse(cell5.getCellStyle().getFont(workbook).getBold()); } private void fill(File file, File template) throws Exception { EasyExcel.write(file, FillStyleAnnotatedData.class).withTemplate(template).sheet().doFill(data()); } - private void t11FillStyleHandler07check(XSSFRow row) { XSSFCell cell0 = row.getCell(0); - Assert.assertEquals("张三", cell0.getStringCellValue()); - Assert.assertEquals(49, cell0.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFFFF00", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF808000", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell0.getCellStyle().getFont().getBold()); + Assertions.assertEquals("张三", cell0.getStringCellValue()); + Assertions.assertEquals(49, cell0.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFFFF00", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF808000", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell0.getCellStyle().getFont().getBold()); XSSFCell cell1 = row.getCell(1); - Assert.assertEquals("5", cell1.getStringCellValue()); - Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF0000", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF800000", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell1.getCellStyle().getFont().getBold()); + Assertions.assertEquals("5", cell1.getStringCellValue()); + Assertions.assertEquals(0, cell1.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFF0000", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF800000", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell1.getCellStyle().getFont().getBold()); XSSFCell cell2 = row.getCell(2); - Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); - Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); - Assert.assertEquals("FF008000", cell2.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF003300", cell2.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell2.getCellStyle().getFont().getBold()); + Assertions.assertEquals("2020-01-01 01:01:01", + DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); + Assertions.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); + Assertions.assertEquals("FF008000", cell2.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF003300", cell2.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell2.getCellStyle().getFont().getBold()); XSSFCell cell3 = row.getCell(3); - Assert.assertEquals("张三今年5岁了", cell3.getStringCellValue()); - Assert.assertEquals(0, cell3.getCellStyle().getDataFormat()); - Assert.assertEquals("FF0000FF", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF000080", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell3.getCellStyle().getFont().getBold()); + Assertions.assertEquals("张三今年5岁了", cell3.getStringCellValue()); + Assertions.assertEquals(0, cell3.getCellStyle().getDataFormat()); + Assertions.assertEquals("FF0000FF", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF000080", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell3.getCellStyle().getFont().getBold()); XSSFCell cell4 = row.getCell(4); - Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); - Assert.assertEquals(0, cell4.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFFFF00", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF808000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell4.getCellStyle().getFont().getBold()); + Assertions.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); + Assertions.assertEquals(0, cell4.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFFFF00", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF808000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell4.getCellStyle().getFont().getBold()); XSSFCell cell5 = row.getCell(5); - Assert.assertEquals("空", cell5.getStringCellValue()); - Assert.assertEquals(0, cell5.getCellStyle().getDataFormat()); - Assert.assertEquals("FF008080", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF003366", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell5.getCellStyle().getFont().getBold()); + Assertions.assertEquals("空", cell5.getStringCellValue()); + Assertions.assertEquals(0, cell5.getCellStyle().getDataFormat()); + Assertions.assertEquals("FF008080", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF003366", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell5.getCellStyle().getFont().getBold()); } - private void t12FillStyleHandler03check(HSSFWorkbook workbook, HSSFRow row) { HSSFCell cell0 = row.getCell(0); - Assert.assertEquals("张三", cell0.getStringCellValue()); - Assert.assertEquals(49, cell0.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF:FFFF:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("8080:8080:0", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("张三", cell0.getStringCellValue()); + Assertions.assertEquals(49, cell0.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFF:FFFF:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("8080:8080:0", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertTrue(cell0.getCellStyle().getFont(workbook).getBold()); + Assertions.assertTrue(cell0.getCellStyle().getFont(workbook).getBold()); HSSFCell cell1 = row.getCell(1); - Assert.assertEquals("5", cell1.getStringCellValue()); - Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF:0:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("8080:0:0", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("5", cell1.getStringCellValue()); + Assertions.assertEquals(0, cell1.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFF:0:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("8080:0:0", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertTrue(cell1.getCellStyle().getFont(workbook).getBold()); + Assertions.assertTrue(cell1.getCellStyle().getFont(workbook).getBold()); HSSFCell cell2 = row.getCell(2); - Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); - Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); - Assert.assertEquals("0:8080:0", cell2.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("0:3333:0", cell2.getCellStyle().getFont(workbook).getHSSFColor(workbook).getHexString()); - Assert.assertTrue(cell2.getCellStyle().getFont(workbook).getBold()); + Assertions.assertEquals("2020-01-01 01:01:01", + DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); + Assertions.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); + Assertions.assertEquals("0:8080:0", cell2.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("0:3333:0", cell2.getCellStyle().getFont(workbook).getHSSFColor(workbook) + .getHexString()); + Assertions.assertTrue(cell2.getCellStyle().getFont(workbook).getBold()); HSSFCell cell3 = row.getCell(3); - Assert.assertEquals("张三今年5岁了", cell3.getStringCellValue()); - Assert.assertEquals(0, cell3.getCellStyle().getDataFormat()); - Assert.assertEquals("0:0:FFFF", cell3.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("0:0:8080", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("张三今年5岁了", cell3.getStringCellValue()); + Assertions.assertEquals(0, cell3.getCellStyle().getDataFormat()); + Assertions.assertEquals("0:0:FFFF", cell3.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("0:0:8080", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertTrue(cell3.getCellStyle().getFont(workbook).getBold()); + Assertions.assertTrue(cell3.getCellStyle().getFont(workbook).getBold()); HSSFCell cell4 = row.getCell(4); - Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); - Assert.assertEquals(0, cell4.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF:FFFF:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("8080:8080:0", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); + Assertions.assertEquals(0, cell4.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFF:FFFF:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("8080:8080:0", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertTrue(cell4.getCellStyle().getFont(workbook).getBold()); + Assertions.assertTrue(cell4.getCellStyle().getFont(workbook).getBold()); HSSFCell cell5 = row.getCell(5); - Assert.assertEquals("空", cell5.getStringCellValue()); - Assert.assertEquals(0, cell5.getCellStyle().getDataFormat()); - Assert.assertEquals("0:8080:8080", cell5.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("0:3333:6666", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("空", cell5.getStringCellValue()); + Assertions.assertEquals(0, cell5.getCellStyle().getDataFormat()); + Assertions.assertEquals("0:8080:8080", cell5.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("0:3333:6666", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertTrue(cell5.getCellStyle().getFont(workbook).getBold()); + Assertions.assertTrue(cell5.getCellStyle().getFont(workbook).getBold()); } private void fillStyleHandler(File file, File template) throws Exception { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java index 210a5b9f..3276b039 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java @@ -24,16 +24,16 @@ import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class FillStyleDataTest { private static File fileStyle07; @@ -43,7 +43,7 @@ public class FillStyleDataTest { private static File fileStyleTemplate07; private static File fileStyleTemplate03; - @BeforeClass + @BeforeAll public static void init() { fileStyle07 = TestFileUtil.createNewFile("fileStyle07.xlsx"); fileStyle03 = TestFileUtil.createNewFile("fileStyle03.xls"); @@ -64,46 +64,47 @@ public class FillStyleDataTest { private void t01Fill07check(XSSFRow row) { XSSFCell cell0 = row.getCell(0); - Assert.assertEquals("张三", cell0.getStringCellValue()); - Assert.assertEquals(49, cell0.getCellStyle().getDataFormat()); - Assert.assertEquals("FF00B050", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF7030A0", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell0.getCellStyle().getFont().getBold()); + Assertions.assertEquals("张三", cell0.getStringCellValue()); + Assertions.assertEquals(49, cell0.getCellStyle().getDataFormat()); + Assertions.assertEquals("FF00B050", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF7030A0", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell0.getCellStyle().getFont().getBold()); XSSFCell cell1 = row.getCell(1); - Assert.assertEquals(5.2, cell1.getNumericCellValue(), 1); - Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); - Assert.assertEquals("FF92D050", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF4BACC6", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertFalse(cell1.getCellStyle().getFont().getBold()); + Assertions.assertEquals(5.2, cell1.getNumericCellValue(), 1); + Assertions.assertEquals(0, cell1.getCellStyle().getDataFormat()); + Assertions.assertEquals("FF92D050", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF4BACC6", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertFalse(cell1.getCellStyle().getFont().getBold()); XSSFCell cell2 = row.getCell(2); - Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); - Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); - Assert.assertEquals("FFFFC000", cell2.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FFC0504D", cell2.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell2.getCellStyle().getFont().getBold()); + Assertions.assertEquals("2020-01-01 01:01:01", + DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); + Assertions.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); + Assertions.assertEquals("FFFFC000", cell2.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FFC0504D", cell2.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell2.getCellStyle().getFont().getBold()); XSSFCell cell3 = row.getCell(3); - Assert.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); - Assert.assertEquals(0, cell3.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF0000", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FFEEECE1", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell3.getCellStyle().getFont().getBold()); + Assertions.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); + Assertions.assertEquals(0, cell3.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFF0000", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FFEEECE1", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell3.getCellStyle().getFont().getBold()); XSSFCell cell4 = row.getCell(4); - Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); - Assert.assertEquals(0, cell4.getCellStyle().getDataFormat()); - Assert.assertEquals("FFC00000", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF000000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertFalse(cell4.getCellStyle().getFont().getBold()); + Assertions.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); + Assertions.assertEquals(0, cell4.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFC00000", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF000000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertFalse(cell4.getCellStyle().getFont().getBold()); XSSFCell cell5 = row.getCell(5); - Assert.assertEquals("空", cell5.getStringCellValue()); - Assert.assertEquals(0, cell5.getCellStyle().getDataFormat()); - Assert.assertEquals("FFF79646", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF8064A2", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertFalse(cell5.getCellStyle().getFont().getBold()); + Assertions.assertEquals("空", cell5.getStringCellValue()); + Assertions.assertEquals(0, cell5.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFF79646", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF8064A2", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertFalse(cell5.getCellStyle().getFont().getBold()); } @Test @@ -117,51 +118,53 @@ public class FillStyleDataTest { private void t02Fill03check(HSSFWorkbook workbook, HSSFRow row) { HSSFCell cell0 = row.getCell(0); - Assert.assertEquals("张三", cell0.getStringCellValue()); - Assert.assertEquals(49, cell0.getCellStyle().getDataFormat()); - Assert.assertEquals("0:8080:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("8080:0:8080", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("张三", cell0.getStringCellValue()); + Assertions.assertEquals(49, cell0.getCellStyle().getDataFormat()); + Assertions.assertEquals("0:8080:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("8080:0:8080", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertTrue(cell0.getCellStyle().getFont(workbook).getBold()); + Assertions.assertTrue(cell0.getCellStyle().getFont(workbook).getBold()); HSSFCell cell1 = row.getCell(1); - Assert.assertEquals(5.2, cell1.getNumericCellValue(), 1); - Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); - Assert.assertEquals("9999:CCCC:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("0:8080:8080", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals(5.2, cell1.getNumericCellValue(), 1); + Assertions.assertEquals(0, cell1.getCellStyle().getDataFormat()); + Assertions.assertEquals("9999:CCCC:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("0:8080:8080", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertFalse(cell1.getCellStyle().getFont(workbook).getBold()); + Assertions.assertFalse(cell1.getCellStyle().getFont(workbook).getBold()); HSSFCell cell2 = row.getCell(2); - Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); - Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); - Assert.assertEquals("FFFF:CCCC:0", cell2.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("8080:0:0", cell2.getCellStyle().getFont(workbook).getHSSFColor(workbook).getHexString()); - Assert.assertTrue(cell2.getCellStyle().getFont(workbook).getBold()); + Assertions.assertEquals("2020-01-01 01:01:01", + DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); + Assertions.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); + Assertions.assertEquals("FFFF:CCCC:0", cell2.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("8080:0:0", cell2.getCellStyle().getFont(workbook).getHSSFColor(workbook) + .getHexString()); + Assertions.assertTrue(cell2.getCellStyle().getFont(workbook).getBold()); HSSFCell cell3 = row.getCell(3); - Assert.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); - Assert.assertEquals(0, cell3.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF:0:0", cell3.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("FFFF:FFFF:9999", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); + Assertions.assertEquals(0, cell3.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFF:0:0", cell3.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("FFFF:FFFF:9999", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertTrue(cell3.getCellStyle().getFont(workbook).getBold()); + Assertions.assertTrue(cell3.getCellStyle().getFont(workbook).getBold()); HSSFCell cell4 = row.getCell(4); - Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); - Assert.assertEquals(0, cell4.getCellStyle().getDataFormat()); - Assert.assertEquals("9999:3333:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("3333:3333:3333", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); + Assertions.assertEquals(0, cell4.getCellStyle().getDataFormat()); + Assertions.assertEquals("9999:3333:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("3333:3333:3333", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertFalse(cell4.getCellStyle().getFont(workbook).getBold()); + Assertions.assertFalse(cell4.getCellStyle().getFont(workbook).getBold()); HSSFCell cell5 = row.getCell(5); - Assert.assertEquals("空", cell5.getStringCellValue()); - Assert.assertEquals(0, cell5.getCellStyle().getDataFormat()); - Assert.assertEquals("9999:3333:0", cell5.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("CCCC:9999:FFFF", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("空", cell5.getStringCellValue()); + Assertions.assertEquals(0, cell5.getCellStyle().getDataFormat()); + Assertions.assertEquals("9999:3333:0", cell5.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("CCCC:9999:FFFF", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertFalse(cell5.getCellStyle().getFont(workbook).getBold()); + Assertions.assertFalse(cell5.getCellStyle().getFont(workbook).getBold()); } private void fill(File file, File template) throws Exception { @@ -179,46 +182,47 @@ public class FillStyleDataTest { private void t11FillStyleHandler07check(XSSFRow row) { XSSFCell cell0 = row.getCell(0); - Assert.assertEquals("张三", cell0.getStringCellValue()); - Assert.assertEquals(49, cell0.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFFFF00", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF808000", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell0.getCellStyle().getFont().getBold()); + Assertions.assertEquals("张三", cell0.getStringCellValue()); + Assertions.assertEquals(49, cell0.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFFFF00", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF808000", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell0.getCellStyle().getFont().getBold()); XSSFCell cell1 = row.getCell(1); - Assert.assertEquals(5.2, cell1.getNumericCellValue(), 1); - Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF0000", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF800000", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell1.getCellStyle().getFont().getBold()); + Assertions.assertEquals(5.2, cell1.getNumericCellValue(), 1); + Assertions.assertEquals(0, cell1.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFF0000", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF800000", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell1.getCellStyle().getFont().getBold()); XSSFCell cell2 = row.getCell(2); - Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); - Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); - Assert.assertEquals("FF008000", cell2.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF003300", cell2.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell2.getCellStyle().getFont().getBold()); + Assertions.assertEquals("2020-01-01 01:01:01", + DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); + Assertions.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); + Assertions.assertEquals("FF008000", cell2.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF003300", cell2.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell2.getCellStyle().getFont().getBold()); XSSFCell cell3 = row.getCell(3); - Assert.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); - Assert.assertEquals(0, cell3.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF0000", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FFEEECE1", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell3.getCellStyle().getFont().getBold()); + Assertions.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); + Assertions.assertEquals(0, cell3.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFF0000", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FFEEECE1", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertTrue(cell3.getCellStyle().getFont().getBold()); XSSFCell cell4 = row.getCell(4); - Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); - Assert.assertEquals(0, cell4.getCellStyle().getDataFormat()); - Assert.assertEquals("FFC00000", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF000000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertFalse(cell4.getCellStyle().getFont().getBold()); + Assertions.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); + Assertions.assertEquals(0, cell4.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFC00000", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF000000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertFalse(cell4.getCellStyle().getFont().getBold()); XSSFCell cell5 = row.getCell(5); - Assert.assertEquals("空", cell5.getStringCellValue()); - Assert.assertEquals(0, cell5.getCellStyle().getDataFormat()); - Assert.assertEquals("FFF79646", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF8064A2", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertFalse(cell5.getCellStyle().getFont().getBold()); + Assertions.assertEquals("空", cell5.getStringCellValue()); + Assertions.assertEquals(0, cell5.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFF79646", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assertions.assertEquals("FF8064A2", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assertions.assertFalse(cell5.getCellStyle().getFont().getBold()); } @Test @@ -232,51 +236,53 @@ public class FillStyleDataTest { private void t12FillStyleHandler03check(HSSFWorkbook workbook, HSSFRow row) { HSSFCell cell0 = row.getCell(0); - Assert.assertEquals("张三", cell0.getStringCellValue()); - Assert.assertEquals(49, cell0.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF:FFFF:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("8080:8080:0", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("张三", cell0.getStringCellValue()); + Assertions.assertEquals(49, cell0.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFF:FFFF:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("8080:8080:0", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertTrue(cell0.getCellStyle().getFont(workbook).getBold()); + Assertions.assertTrue(cell0.getCellStyle().getFont(workbook).getBold()); HSSFCell cell1 = row.getCell(1); - Assert.assertEquals(5.2, cell1.getNumericCellValue(), 1); - Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF:0:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("8080:0:0", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals(5.2, cell1.getNumericCellValue(), 1); + Assertions.assertEquals(0, cell1.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFF:0:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("8080:0:0", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertTrue(cell1.getCellStyle().getFont(workbook).getBold()); + Assertions.assertTrue(cell1.getCellStyle().getFont(workbook).getBold()); HSSFCell cell2 = row.getCell(2); - Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); - Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); - Assert.assertEquals("0:8080:0", cell2.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("0:3333:0", cell2.getCellStyle().getFont(workbook).getHSSFColor(workbook).getHexString()); - Assert.assertTrue(cell2.getCellStyle().getFont(workbook).getBold()); + Assertions.assertEquals("2020-01-01 01:01:01", + DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); + Assertions.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); + Assertions.assertEquals("0:8080:0", cell2.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("0:3333:0", cell2.getCellStyle().getFont(workbook).getHSSFColor(workbook) + .getHexString()); + Assertions.assertTrue(cell2.getCellStyle().getFont(workbook).getBold()); HSSFCell cell3 = row.getCell(3); - Assert.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); - Assert.assertEquals(0, cell3.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF:0:0", cell3.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("FFFF:FFFF:9999", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); + Assertions.assertEquals(0, cell3.getCellStyle().getDataFormat()); + Assertions.assertEquals("FFFF:0:0", cell3.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("FFFF:FFFF:9999", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertTrue(cell3.getCellStyle().getFont(workbook).getBold()); + Assertions.assertTrue(cell3.getCellStyle().getFont(workbook).getBold()); HSSFCell cell4 = row.getCell(4); - Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); - Assert.assertEquals(0, cell4.getCellStyle().getDataFormat()); - Assert.assertEquals("9999:3333:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("3333:3333:3333", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); + Assertions.assertEquals(0, cell4.getCellStyle().getDataFormat()); + Assertions.assertEquals("9999:3333:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("3333:3333:3333", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertFalse(cell4.getCellStyle().getFont(workbook).getBold()); + Assertions.assertFalse(cell4.getCellStyle().getFont(workbook).getBold()); HSSFCell cell5 = row.getCell(5); - Assert.assertEquals("空", cell5.getStringCellValue()); - Assert.assertEquals(0, cell5.getCellStyle().getDataFormat()); - Assert.assertEquals("9999:3333:0", cell5.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("CCCC:9999:FFFF", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assertions.assertEquals("空", cell5.getStringCellValue()); + Assertions.assertEquals(0, cell5.getCellStyle().getDataFormat()); + Assertions.assertEquals("9999:3333:0", cell5.getCellStyle().getFillForegroundColorColor().getHexString()); + Assertions.assertEquals("CCCC:9999:FFFF", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertFalse(cell5.getCellStyle().getFont(workbook).getBold()); + Assertions.assertFalse(cell5.getCellStyle().getFont(workbook).getBold()); } private void fillStyleHandler(File file, File template) throws Exception { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandler.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandler.java index a6f07014..ba16e61b 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandler.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandler.java @@ -14,7 +14,7 @@ import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; -import org.junit.Assert; +import org.junit.jupiter.api.Assertions; /** * @author JiaJu Zhuang @@ -38,18 +38,18 @@ public class WriteHandler implements WorkbookWriteHandler, SheetWriteHandler, Ro public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) { if (isHead) { - Assert.assertEquals(0L, beforeCellCreate); - Assert.assertEquals(0L, afterCellCreate); - Assert.assertEquals(0L, afterCellDataConverted); - Assert.assertEquals(0L, afterCellDispose); - Assert.assertEquals(1L, beforeRowCreate); - Assert.assertEquals(1L, afterRowCreate); - Assert.assertEquals(0L, afterRowDispose); - Assert.assertEquals(1L, beforeSheetCreate); - Assert.assertEquals(1L, afterSheetCreate); - Assert.assertEquals(1L, beforeWorkbookCreate); - Assert.assertEquals(1L, afterWorkbookCreate); - Assert.assertEquals(0L, afterWorkbookDispose); + Assertions.assertEquals(0L, beforeCellCreate); + Assertions.assertEquals(0L, afterCellCreate); + Assertions.assertEquals(0L, afterCellDataConverted); + Assertions.assertEquals(0L, afterCellDispose); + Assertions.assertEquals(1L, beforeRowCreate); + Assertions.assertEquals(1L, afterRowCreate); + Assertions.assertEquals(0L, afterRowDispose); + Assertions.assertEquals(1L, beforeSheetCreate); + Assertions.assertEquals(1L, afterSheetCreate); + Assertions.assertEquals(1L, beforeWorkbookCreate); + Assertions.assertEquals(1L, afterWorkbookCreate); + Assertions.assertEquals(0L, afterWorkbookDispose); beforeCellCreate++; } @@ -59,18 +59,18 @@ public class WriteHandler implements WorkbookWriteHandler, SheetWriteHandler, Ro public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { if (isHead) { - Assert.assertEquals(1L, beforeCellCreate); - Assert.assertEquals(0L, afterCellCreate); - Assert.assertEquals(0L, afterCellDataConverted); - Assert.assertEquals(0L, afterCellDispose); - Assert.assertEquals(1L, beforeRowCreate); - Assert.assertEquals(1L, afterRowCreate); - Assert.assertEquals(0L, afterRowDispose); - Assert.assertEquals(1L, beforeSheetCreate); - Assert.assertEquals(1L, afterSheetCreate); - Assert.assertEquals(1L, beforeWorkbookCreate); - Assert.assertEquals(1L, afterWorkbookCreate); - Assert.assertEquals(0L, afterWorkbookDispose); + Assertions.assertEquals(1L, beforeCellCreate); + Assertions.assertEquals(0L, afterCellCreate); + Assertions.assertEquals(0L, afterCellDataConverted); + Assertions.assertEquals(0L, afterCellDispose); + Assertions.assertEquals(1L, beforeRowCreate); + Assertions.assertEquals(1L, afterRowCreate); + Assertions.assertEquals(0L, afterRowDispose); + Assertions.assertEquals(1L, beforeSheetCreate); + Assertions.assertEquals(1L, afterSheetCreate); + Assertions.assertEquals(1L, beforeWorkbookCreate); + Assertions.assertEquals(1L, afterWorkbookCreate); + Assertions.assertEquals(0L, afterWorkbookDispose); afterCellCreate++; } } @@ -78,18 +78,18 @@ public class WriteHandler implements WorkbookWriteHandler, SheetWriteHandler, Ro @Override public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, WriteCellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { - Assert.assertEquals(1L, beforeCellCreate); - Assert.assertEquals(1L, afterCellCreate); - Assert.assertEquals(0L, afterCellDataConverted); - Assert.assertEquals(1, afterCellDispose); - Assert.assertEquals(1L, beforeRowCreate); - Assert.assertEquals(1L, afterRowCreate); - Assert.assertEquals(1L, afterRowDispose); - Assert.assertEquals(1L, beforeSheetCreate); - Assert.assertEquals(1L, afterSheetCreate); - Assert.assertEquals(1L, beforeWorkbookCreate); - Assert.assertEquals(1L, afterWorkbookCreate); - Assert.assertEquals(0L, afterWorkbookDispose); + Assertions.assertEquals(1L, beforeCellCreate); + Assertions.assertEquals(1L, afterCellCreate); + Assertions.assertEquals(0L, afterCellDataConverted); + Assertions.assertEquals(1, afterCellDispose); + Assertions.assertEquals(1L, beforeRowCreate); + Assertions.assertEquals(1L, afterRowCreate); + Assertions.assertEquals(1L, afterRowDispose); + Assertions.assertEquals(1L, beforeSheetCreate); + Assertions.assertEquals(1L, afterSheetCreate); + Assertions.assertEquals(1L, beforeWorkbookCreate); + Assertions.assertEquals(1L, afterWorkbookCreate); + Assertions.assertEquals(0L, afterWorkbookDispose); afterCellDataConverted++; } @@ -97,18 +97,18 @@ public class WriteHandler implements WorkbookWriteHandler, SheetWriteHandler, Ro public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { if (isHead) { - Assert.assertEquals(1L, beforeCellCreate); - Assert.assertEquals(1L, afterCellCreate); - Assert.assertEquals(0L, afterCellDataConverted); - Assert.assertEquals(0L, afterCellDispose); - Assert.assertEquals(1L, beforeRowCreate); - Assert.assertEquals(1L, afterRowCreate); - Assert.assertEquals(0L, afterRowDispose); - Assert.assertEquals(1L, beforeSheetCreate); - Assert.assertEquals(1L, afterSheetCreate); - Assert.assertEquals(1L, beforeWorkbookCreate); - Assert.assertEquals(1L, afterWorkbookCreate); - Assert.assertEquals(0L, afterWorkbookDispose); + Assertions.assertEquals(1L, beforeCellCreate); + Assertions.assertEquals(1L, afterCellCreate); + Assertions.assertEquals(0L, afterCellDataConverted); + Assertions.assertEquals(0L, afterCellDispose); + Assertions.assertEquals(1L, beforeRowCreate); + Assertions.assertEquals(1L, afterRowCreate); + Assertions.assertEquals(0L, afterRowDispose); + Assertions.assertEquals(1L, beforeSheetCreate); + Assertions.assertEquals(1L, afterSheetCreate); + Assertions.assertEquals(1L, beforeWorkbookCreate); + Assertions.assertEquals(1L, afterWorkbookCreate); + Assertions.assertEquals(0L, afterWorkbookDispose); afterCellDispose++; } } @@ -117,18 +117,18 @@ public class WriteHandler implements WorkbookWriteHandler, SheetWriteHandler, Ro public void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer rowIndex, Integer relativeRowIndex, Boolean isHead) { if (isHead) { - Assert.assertEquals(0L, beforeCellCreate); - Assert.assertEquals(0L, afterCellCreate); - Assert.assertEquals(0L, afterCellDataConverted); - Assert.assertEquals(0L, afterCellDispose); - Assert.assertEquals(0L, beforeRowCreate); - Assert.assertEquals(0L, afterRowCreate); - Assert.assertEquals(0L, afterRowDispose); - Assert.assertEquals(1L, beforeSheetCreate); - Assert.assertEquals(1L, afterSheetCreate); - Assert.assertEquals(1L, beforeWorkbookCreate); - Assert.assertEquals(1L, afterWorkbookCreate); - Assert.assertEquals(0L, afterWorkbookDispose); + Assertions.assertEquals(0L, beforeCellCreate); + Assertions.assertEquals(0L, afterCellCreate); + Assertions.assertEquals(0L, afterCellDataConverted); + Assertions.assertEquals(0L, afterCellDispose); + Assertions.assertEquals(0L, beforeRowCreate); + Assertions.assertEquals(0L, afterRowCreate); + Assertions.assertEquals(0L, afterRowDispose); + Assertions.assertEquals(1L, beforeSheetCreate); + Assertions.assertEquals(1L, afterSheetCreate); + Assertions.assertEquals(1L, beforeWorkbookCreate); + Assertions.assertEquals(1L, afterWorkbookCreate); + Assertions.assertEquals(0L, afterWorkbookDispose); beforeRowCreate++; } @@ -138,18 +138,18 @@ public class WriteHandler implements WorkbookWriteHandler, SheetWriteHandler, Ro public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) { if (isHead) { - Assert.assertEquals(0L, beforeCellCreate); - Assert.assertEquals(0L, afterCellCreate); - Assert.assertEquals(0L, afterCellDataConverted); - Assert.assertEquals(0L, afterCellDispose); - Assert.assertEquals(1L, beforeRowCreate); - Assert.assertEquals(0L, afterRowCreate); - Assert.assertEquals(0L, afterRowDispose); - Assert.assertEquals(1L, beforeSheetCreate); - Assert.assertEquals(1L, afterSheetCreate); - Assert.assertEquals(1L, beforeWorkbookCreate); - Assert.assertEquals(1L, afterWorkbookCreate); - Assert.assertEquals(0L, afterWorkbookDispose); + Assertions.assertEquals(0L, beforeCellCreate); + Assertions.assertEquals(0L, afterCellCreate); + Assertions.assertEquals(0L, afterCellDataConverted); + Assertions.assertEquals(0L, afterCellDispose); + Assertions.assertEquals(1L, beforeRowCreate); + Assertions.assertEquals(0L, afterRowCreate); + Assertions.assertEquals(0L, afterRowDispose); + Assertions.assertEquals(1L, beforeSheetCreate); + Assertions.assertEquals(1L, afterSheetCreate); + Assertions.assertEquals(1L, beforeWorkbookCreate); + Assertions.assertEquals(1L, afterWorkbookCreate); + Assertions.assertEquals(0L, afterWorkbookDispose); afterRowCreate++; } } @@ -158,120 +158,120 @@ public class WriteHandler implements WorkbookWriteHandler, SheetWriteHandler, Ro public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) { if (isHead) { - Assert.assertEquals(1L, beforeCellCreate); - Assert.assertEquals(1L, afterCellCreate); - Assert.assertEquals(0L, afterCellDataConverted); - Assert.assertEquals(1L, afterCellDispose); - Assert.assertEquals(1L, beforeRowCreate); - Assert.assertEquals(1L, afterRowCreate); - Assert.assertEquals(0L, afterRowDispose); - Assert.assertEquals(1L, beforeSheetCreate); - Assert.assertEquals(1L, afterSheetCreate); - Assert.assertEquals(1L, beforeWorkbookCreate); - Assert.assertEquals(1L, afterWorkbookCreate); - Assert.assertEquals(0L, afterWorkbookDispose); + Assertions.assertEquals(1L, beforeCellCreate); + Assertions.assertEquals(1L, afterCellCreate); + Assertions.assertEquals(0L, afterCellDataConverted); + Assertions.assertEquals(1L, afterCellDispose); + Assertions.assertEquals(1L, beforeRowCreate); + Assertions.assertEquals(1L, afterRowCreate); + Assertions.assertEquals(0L, afterRowDispose); + Assertions.assertEquals(1L, beforeSheetCreate); + Assertions.assertEquals(1L, afterSheetCreate); + Assertions.assertEquals(1L, beforeWorkbookCreate); + Assertions.assertEquals(1L, afterWorkbookCreate); + Assertions.assertEquals(0L, afterWorkbookDispose); afterRowDispose++; } } @Override public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { - Assert.assertEquals(0L, beforeCellCreate); - Assert.assertEquals(0L, afterCellCreate); - Assert.assertEquals(0L, afterCellDataConverted); - Assert.assertEquals(0L, afterCellDispose); - Assert.assertEquals(0L, beforeRowCreate); - Assert.assertEquals(0L, afterRowCreate); - Assert.assertEquals(0L, afterRowDispose); - Assert.assertEquals(0L, beforeSheetCreate); - Assert.assertEquals(0L, afterSheetCreate); - Assert.assertEquals(1L, beforeWorkbookCreate); - Assert.assertEquals(1L, afterWorkbookCreate); - Assert.assertEquals(0L, afterWorkbookDispose); + Assertions.assertEquals(0L, beforeCellCreate); + Assertions.assertEquals(0L, afterCellCreate); + Assertions.assertEquals(0L, afterCellDataConverted); + Assertions.assertEquals(0L, afterCellDispose); + Assertions.assertEquals(0L, beforeRowCreate); + Assertions.assertEquals(0L, afterRowCreate); + Assertions.assertEquals(0L, afterRowDispose); + Assertions.assertEquals(0L, beforeSheetCreate); + Assertions.assertEquals(0L, afterSheetCreate); + Assertions.assertEquals(1L, beforeWorkbookCreate); + Assertions.assertEquals(1L, afterWorkbookCreate); + Assertions.assertEquals(0L, afterWorkbookDispose); beforeSheetCreate++; } @Override public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { - Assert.assertEquals(0L, beforeCellCreate); - Assert.assertEquals(0L, afterCellCreate); - Assert.assertEquals(0L, afterCellDataConverted); - Assert.assertEquals(0L, afterCellDispose); - Assert.assertEquals(0L, beforeRowCreate); - Assert.assertEquals(0L, afterRowCreate); - Assert.assertEquals(0L, afterRowDispose); - Assert.assertEquals(1L, beforeSheetCreate); - Assert.assertEquals(0L, afterSheetCreate); - Assert.assertEquals(1L, beforeWorkbookCreate); - Assert.assertEquals(1L, afterWorkbookCreate); - Assert.assertEquals(0L, afterWorkbookDispose); + Assertions.assertEquals(0L, beforeCellCreate); + Assertions.assertEquals(0L, afterCellCreate); + Assertions.assertEquals(0L, afterCellDataConverted); + Assertions.assertEquals(0L, afterCellDispose); + Assertions.assertEquals(0L, beforeRowCreate); + Assertions.assertEquals(0L, afterRowCreate); + Assertions.assertEquals(0L, afterRowDispose); + Assertions.assertEquals(1L, beforeSheetCreate); + Assertions.assertEquals(0L, afterSheetCreate); + Assertions.assertEquals(1L, beforeWorkbookCreate); + Assertions.assertEquals(1L, afterWorkbookCreate); + Assertions.assertEquals(0L, afterWorkbookDispose); afterSheetCreate++; } @Override public void beforeWorkbookCreate() { - Assert.assertEquals(0L, beforeCellCreate); - Assert.assertEquals(0L, afterCellCreate); - Assert.assertEquals(0L, afterCellDataConverted); - Assert.assertEquals(0L, afterCellDispose); - Assert.assertEquals(0L, beforeRowCreate); - Assert.assertEquals(0L, afterRowCreate); - Assert.assertEquals(0L, afterRowDispose); - Assert.assertEquals(0L, beforeSheetCreate); - Assert.assertEquals(0L, afterSheetCreate); - Assert.assertEquals(0L, beforeWorkbookCreate); - Assert.assertEquals(0L, afterWorkbookCreate); - Assert.assertEquals(0L, afterWorkbookDispose); + Assertions.assertEquals(0L, beforeCellCreate); + Assertions.assertEquals(0L, afterCellCreate); + Assertions.assertEquals(0L, afterCellDataConverted); + Assertions.assertEquals(0L, afterCellDispose); + Assertions.assertEquals(0L, beforeRowCreate); + Assertions.assertEquals(0L, afterRowCreate); + Assertions.assertEquals(0L, afterRowDispose); + Assertions.assertEquals(0L, beforeSheetCreate); + Assertions.assertEquals(0L, afterSheetCreate); + Assertions.assertEquals(0L, beforeWorkbookCreate); + Assertions.assertEquals(0L, afterWorkbookCreate); + Assertions.assertEquals(0L, afterWorkbookDispose); beforeWorkbookCreate++; } @Override public void afterWorkbookCreate(WriteWorkbookHolder writeWorkbookHolder) { - Assert.assertEquals(0L, beforeCellCreate); - Assert.assertEquals(0L, afterCellCreate); - Assert.assertEquals(0L, afterCellDataConverted); - Assert.assertEquals(0L, afterCellDispose); - Assert.assertEquals(0L, beforeRowCreate); - Assert.assertEquals(0L, afterRowCreate); - Assert.assertEquals(0L, afterRowDispose); - Assert.assertEquals(0L, beforeSheetCreate); - Assert.assertEquals(0L, afterSheetCreate); - Assert.assertEquals(1L, beforeWorkbookCreate); - Assert.assertEquals(0L, afterWorkbookCreate); - Assert.assertEquals(0L, afterWorkbookDispose); + Assertions.assertEquals(0L, beforeCellCreate); + Assertions.assertEquals(0L, afterCellCreate); + Assertions.assertEquals(0L, afterCellDataConverted); + Assertions.assertEquals(0L, afterCellDispose); + Assertions.assertEquals(0L, beforeRowCreate); + Assertions.assertEquals(0L, afterRowCreate); + Assertions.assertEquals(0L, afterRowDispose); + Assertions.assertEquals(0L, beforeSheetCreate); + Assertions.assertEquals(0L, afterSheetCreate); + Assertions.assertEquals(1L, beforeWorkbookCreate); + Assertions.assertEquals(0L, afterWorkbookCreate); + Assertions.assertEquals(0L, afterWorkbookDispose); afterWorkbookCreate++; } @Override public void afterWorkbookDispose(WriteWorkbookHolder writeWorkbookHolder) { - Assert.assertEquals(1L, beforeCellCreate); - Assert.assertEquals(1L, afterCellCreate); - Assert.assertEquals(1L, afterCellDataConverted); - Assert.assertEquals(1L, afterCellDispose); - Assert.assertEquals(1L, beforeRowCreate); - Assert.assertEquals(1L, afterRowCreate); - Assert.assertEquals(1L, afterRowDispose); - Assert.assertEquals(1L, beforeSheetCreate); - Assert.assertEquals(1L, afterSheetCreate); - Assert.assertEquals(1L, beforeWorkbookCreate); - Assert.assertEquals(1L, afterWorkbookCreate); - Assert.assertEquals(0L, afterWorkbookDispose); + Assertions.assertEquals(1L, beforeCellCreate); + Assertions.assertEquals(1L, afterCellCreate); + Assertions.assertEquals(1L, afterCellDataConverted); + Assertions.assertEquals(1L, afterCellDispose); + Assertions.assertEquals(1L, beforeRowCreate); + Assertions.assertEquals(1L, afterRowCreate); + Assertions.assertEquals(1L, afterRowDispose); + Assertions.assertEquals(1L, beforeSheetCreate); + Assertions.assertEquals(1L, afterSheetCreate); + Assertions.assertEquals(1L, beforeWorkbookCreate); + Assertions.assertEquals(1L, afterWorkbookCreate); + Assertions.assertEquals(0L, afterWorkbookDispose); afterWorkbookDispose++; } public void afterAll() { - Assert.assertEquals(1L, beforeCellCreate); - Assert.assertEquals(1L, afterCellCreate); - Assert.assertEquals(1L, afterCellDataConverted); - Assert.assertEquals(1L, afterCellDispose); - Assert.assertEquals(1L, beforeRowCreate); - Assert.assertEquals(1L, afterRowCreate); - Assert.assertEquals(1L, afterRowDispose); - Assert.assertEquals(1L, beforeSheetCreate); - Assert.assertEquals(1L, afterSheetCreate); - Assert.assertEquals(1L, beforeWorkbookCreate); - Assert.assertEquals(1L, afterWorkbookCreate); - Assert.assertEquals(1L, afterWorkbookDispose); + Assertions.assertEquals(1L, beforeCellCreate); + Assertions.assertEquals(1L, afterCellCreate); + Assertions.assertEquals(1L, afterCellDataConverted); + Assertions.assertEquals(1L, afterCellDispose); + Assertions.assertEquals(1L, beforeRowCreate); + Assertions.assertEquals(1L, afterRowCreate); + Assertions.assertEquals(1L, afterRowDispose); + Assertions.assertEquals(1L, beforeSheetCreate); + Assertions.assertEquals(1L, afterSheetCreate); + Assertions.assertEquals(1L, beforeWorkbookCreate); + Assertions.assertEquals(1L, afterWorkbookCreate); + Assertions.assertEquals(1L, afterWorkbookDispose); } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java index a3354f35..3988e8a0 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java @@ -7,24 +7,22 @@ import java.util.List; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** - * * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class WriteHandlerTest { private static File file07; private static File file03; private static File fileCsv; - - @BeforeClass + @BeforeAll public static void init() { file07 = TestFileUtil.createNewFile("writeHandler07.xlsx"); file03 = TestFileUtil.createNewFile("writeHandler03.xls"); @@ -46,7 +44,6 @@ public class WriteHandlerTest { workbookWrite(fileCsv); } - @Test public void t11SheetWrite07() throws Exception { sheetWrite(file07); @@ -72,7 +69,6 @@ public class WriteHandlerTest { tableWrite(file03); } - @Test public void t23TableWriteCsv() throws Exception { tableWrite(fileCsv); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexDataListener.java index 1eab8585..1b127962 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexDataListener.java @@ -3,14 +3,14 @@ package com.alibaba.easyexcel.test.core.head; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Assertions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ @@ -25,9 +25,9 @@ public class ComplexDataListener extends AnalysisEventListener @Override public void doAfterAllAnalysed(AnalysisContext context) { - Assert.assertEquals(list.size(), 1); + Assertions.assertEquals(list.size(), 1); ComplexHeadData data = list.get(0); - Assert.assertEquals(data.getString4(), "字符串4"); + Assertions.assertEquals(data.getString4(), "字符串4"); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadDataTest.java index 07340c3b..a6e2ada2 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadDataTest.java @@ -7,15 +7,15 @@ import java.util.List; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class ComplexHeadDataTest { private static File file07; @@ -25,7 +25,7 @@ public class ComplexHeadDataTest { private static File file03AutomaticMergeHead; private static File fileCsvAutomaticMergeHead; - @BeforeClass + @BeforeAll public static void init() { file07 = TestFileUtil.createNewFile("complexHead07.xlsx"); file03 = TestFileUtil.createNewFile("complexHead03.xls"); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataListener.java index 3e0ba094..2cc93a2d 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataListener.java @@ -9,7 +9,7 @@ import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.fastjson2.JSON; -import org.junit.Assert; +import org.junit.jupiter.api.Assertions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,10 +23,10 @@ public class ListHeadDataListener implements ReadListener> @Override public void invokeHead(Map> headMap, AnalysisContext context) { - Assert.assertNotNull(context.readRowHolder().getRowIndex()); + Assertions.assertNotNull(context.readRowHolder().getRowIndex()); headMap.forEach((key, value) -> { - Assert.assertEquals(value.getRowIndex(), context.readRowHolder().getRowIndex()); - Assert.assertEquals(value.getColumnIndex(), key); + Assertions.assertEquals(value.getRowIndex(), context.readRowHolder().getRowIndex()); + Assertions.assertEquals(value.getColumnIndex(), key); }); } @@ -37,12 +37,12 @@ public class ListHeadDataListener implements ReadListener> @Override public void doAfterAllAnalysed(AnalysisContext context) { - Assert.assertEquals(list.size(), 1); + Assertions.assertEquals(list.size(), 1); Map data = list.get(0); - Assert.assertEquals("字符串0", data.get(0)); - Assert.assertEquals("1", data.get(1)); - Assert.assertEquals("2020-01-01 01:01:01", data.get(2)); - Assert.assertEquals("额外数据", data.get(3)); + Assertions.assertEquals("字符串0", data.get(0)); + Assertions.assertEquals("1", data.get(1)); + Assertions.assertEquals("2020-01-01 01:01:01", data.get(2)); + Assertions.assertEquals("额外数据", data.get(3)); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataTest.java index 073c0e9c..c1dab546 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataTest.java @@ -9,24 +9,22 @@ import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.util.DateUtils; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** - * * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class ListHeadDataTest { private static File file07; private static File file03; private static File fileCsv; - - @BeforeClass + @BeforeAll public static void init() { file07 = TestFileUtil.createNewFile("listHead07.xlsx"); file03 = TestFileUtil.createNewFile("listHead03.xls"); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataListener.java index ae07ab41..3e5df209 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataListener.java @@ -3,14 +3,14 @@ package com.alibaba.easyexcel.test.core.head; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Assertions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ @@ -25,9 +25,9 @@ public class NoHeadDataListener extends AnalysisEventListener { @Override public void doAfterAllAnalysed(AnalysisContext context) { - Assert.assertEquals(list.size(), 1); + Assertions.assertEquals(list.size(), 1); NoHeadData data = list.get(0); - Assert.assertEquals(data.getString(), "字符串0"); + Assertions.assertEquals(data.getString(), "字符串0"); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataTest.java index b08e4630..a7568602 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataTest.java @@ -7,22 +7,22 @@ import java.util.List; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class NoHeadDataTest { private static File file07; private static File file03; private static File fileCsv; - @BeforeClass + @BeforeAll public static void init() { file07 = TestFileUtil.createNewFile("noHead07.xlsx"); file03 = TestFileUtil.createNewFile("noHead03.xls"); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java index fd056a5c..7736de30 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java @@ -5,7 +5,7 @@ import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.fastjson2.JSON; -import org.junit.Assert; +import org.junit.jupiter.api.Assertions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,9 +31,9 @@ public class LargeDataListener extends AnalysisEventListener { public void doAfterAllAnalysed(AnalysisContext context) { LOGGER.info("Large row count:{}", count); if (context.readWorkbookHolder().getExcelType() != ExcelTypeEnum.CSV) { - Assert.assertEquals(count, 464509); + Assertions.assertEquals(count, 464509); } else { - Assert.assertEquals(count, 499999); + Assertions.assertEquals(count, 499999); } } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java index 8862fe18..2eecca2a 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java @@ -2,35 +2,30 @@ package com.alibaba.easyexcel.test.core.large; import java.io.File; import java.io.FileOutputStream; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; -import com.alibaba.excel.cache.Ehcache; -import com.alibaba.excel.cache.ReadCache; -import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.write.metadata.WriteSheet; -import org.apache.poi.util.TempFile; import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class LargeDataTest { private static final Logger LOGGER = LoggerFactory.getLogger(LargeDataTest.class); private static File fileFill07; @@ -42,7 +37,7 @@ public class LargeDataTest { private int i = 0; - @BeforeClass + @BeforeAll public static void init() { fileFill07 = TestFileUtil.createNewFile("largefill07.xlsx"); fileWrite07 = TestFileUtil.createNewFile("large" + File.separator + "fileWrite07.xlsx"); @@ -60,7 +55,6 @@ public class LargeDataTest { LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start); } - @Test public void t02Fill() { try (ExcelWriter excelWriter = EasyExcel.write(fileFill07).withTemplate(template07).build()) { @@ -131,7 +125,7 @@ public class LargeDataTest { long costPoi = System.currentTimeMillis() - start; LOGGER.info("poi write cost:{}", System.currentTimeMillis() - start); LOGGER.info("{} vs {}", cost, costPoi); - Assert.assertTrue(costPoi * 2 > cost); + Assertions.assertTrue(costPoi * 2 > cost); } private List data() { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsDataTest.java index b87decde..0ac27617 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsDataTest.java @@ -8,22 +8,22 @@ import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelReader; import com.alibaba.excel.read.metadata.ReadSheet; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class MultipleSheetsDataTest { private static File file07; private static File file03; - @BeforeClass + @BeforeAll public static void init() { file07 = TestFileUtil.readFile("multiplesheets" + File.separator + "multiplesheets.xlsx"); file03 = TestFileUtil.readFile("multiplesheets" + File.separator + "multiplesheets.xls"); @@ -56,7 +56,7 @@ public class MultipleSheetsDataTest { int count = 1; for (ReadSheet readSheet : sheets) { excelReader.read(readSheet); - Assert.assertEquals(multipleSheetsListener.getList().size(), count); + Assertions.assertEquals(multipleSheetsListener.getList().size(), count); count++; } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsListener.java index c078f20e..b30484aa 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsListener.java @@ -3,14 +3,14 @@ package com.alibaba.easyexcel.test.core.multiplesheets; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Assertions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ @@ -26,7 +26,7 @@ public class MultipleSheetsListener extends AnalysisEventListener> result = EasyExcel.read(file).headRowNumber(0).sheet().doReadSync(); - Assert.assertEquals(10, result.size()); + Assertions.assertEquals(10, result.size()); Map data10 = result.get(9); - Assert.assertEquals("string19", data10.get(0)); - Assert.assertEquals("109", data10.get(1)); - Assert.assertEquals("2020-01-01 01:01:01", data10.get(2)); + Assertions.assertEquals("string19", data10.get(0)); + Assertions.assertEquals("109", data10.get(1)); + Assertions.assertEquals("2020-01-01 01:01:01", data10.get(2)); List> actualDataList = EasyExcel.read(file) .headRowNumber(0) @@ -75,16 +75,16 @@ public class NoModelDataTest { .sheet() .doReadSync(); log.info("actualDataList:{}", JSON.toJSONString(actualDataList)); - Assert.assertEquals(10, actualDataList.size()); + Assertions.assertEquals(10, actualDataList.size()); Map actualData10 = actualDataList.get(9); - Assert.assertEquals("string19", actualData10.get(0)); + Assertions.assertEquals("string19", actualData10.get(0)); if (isCsv) { // CSV only string type - Assert.assertEquals("109", actualData10.get(1)); - Assert.assertEquals("2020-01-01 01:01:01", actualData10.get(2)); + Assertions.assertEquals("109", actualData10.get(1)); + Assertions.assertEquals("2020-01-01 01:01:01", actualData10.get(2)); } else { - Assert.assertEquals(0, new BigDecimal("109").compareTo((BigDecimal)actualData10.get(1))); - Assert.assertEquals(LocalDateTime.of(2020, 1, 1, 1, 1, 1), actualData10.get(2)); + Assertions.assertEquals(0, new BigDecimal("109").compareTo((BigDecimal)actualData10.get(1))); + Assertions.assertEquals(LocalDateTime.of(2020, 1, 1, 1, 1, 1), actualData10.get(2)); } List>> readCellDataList = EasyExcel.read(file) @@ -93,25 +93,25 @@ public class NoModelDataTest { .sheet() .doReadSync(); log.info("readCellDataList:{}", JSON.toJSONString(readCellDataList)); - Assert.assertEquals(10, readCellDataList.size()); + Assertions.assertEquals(10, readCellDataList.size()); Map> readCellData10 = readCellDataList.get(9); - Assert.assertEquals("string19", readCellData10.get(0).getData()); + Assertions.assertEquals("string19", readCellData10.get(0).getData()); if (isCsv) { // CSV only string type - Assert.assertEquals("109", readCellData10.get(1).getData()); - Assert.assertEquals("2020-01-01 01:01:01", readCellData10.get(2).getData()); + Assertions.assertEquals("109", readCellData10.get(1).getData()); + Assertions.assertEquals("2020-01-01 01:01:01", readCellData10.get(2).getData()); } else { - Assert.assertEquals(0, new BigDecimal("109").compareTo((BigDecimal)readCellData10.get(1).getData())); - Assert.assertEquals(LocalDateTime.of(2020, 1, 1, 1, 1, 1), readCellData10.get(2).getData()); + Assertions.assertEquals(0, new BigDecimal("109").compareTo((BigDecimal)readCellData10.get(1).getData())); + Assertions.assertEquals(LocalDateTime.of(2020, 1, 1, 1, 1, 1), readCellData10.get(2).getData()); } EasyExcel.write(fileRepeat).sheet().doWrite(result); result = EasyExcel.read(fileRepeat).headRowNumber(0).sheet().doReadSync(); - Assert.assertEquals(10, result.size()); + Assertions.assertEquals(10, result.size()); data10 = result.get(9); - Assert.assertEquals("string19", data10.get(0)); - Assert.assertEquals("109", data10.get(1)); - Assert.assertEquals("2020-01-01 01:01:01", data10.get(2)); + Assertions.assertEquals("string19", data10.get(0)); + Assertions.assertEquals("109", data10.get(1)); + Assertions.assertEquals("2020-01-01 01:01:01", data10.get(2)); } private List> data() throws Exception { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataListener.java index 8c5e93c7..f9dd43e6 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataListener.java @@ -9,7 +9,7 @@ import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; -import org.junit.Assert; +import org.junit.jupiter.api.Assertions; /** * @author Jiaju Zhuang @@ -21,12 +21,12 @@ public class UnCamelDataListener extends AnalysisEventListener { @Override public void invokeHeadMap(Map headMap, AnalysisContext context) { log.debug("Head is:{}", JSON.toJSONString(headMap)); - Assert.assertEquals(headMap.get(0), "string1"); - Assert.assertEquals(headMap.get(1), "string2"); - Assert.assertEquals(headMap.get(2), "STring3"); - Assert.assertEquals(headMap.get(3), "STring4"); - Assert.assertEquals(headMap.get(4), "STRING5"); - Assert.assertEquals(headMap.get(5), "STRing6"); + Assertions.assertEquals(headMap.get(0), "string1"); + Assertions.assertEquals(headMap.get(1), "string2"); + Assertions.assertEquals(headMap.get(2), "STring3"); + Assertions.assertEquals(headMap.get(3), "STring4"); + Assertions.assertEquals(headMap.get(4), "STRING5"); + Assertions.assertEquals(headMap.get(5), "STRing6"); } @@ -37,14 +37,14 @@ public class UnCamelDataListener extends AnalysisEventListener { @Override public void doAfterAllAnalysed(AnalysisContext context) { - Assert.assertEquals(list.size(), 10); + Assertions.assertEquals(list.size(), 10); UnCamelData unCamelData = list.get(0); - Assert.assertEquals(unCamelData.getString1(), "string1"); - Assert.assertEquals(unCamelData.getString2(), "string2"); - Assert.assertEquals(unCamelData.getSTring3(), "string3"); - Assert.assertEquals(unCamelData.getSTring4(), "string4"); - Assert.assertEquals(unCamelData.getSTRING5(), "string5"); - Assert.assertEquals(unCamelData.getSTRing6(), "string6"); + Assertions.assertEquals(unCamelData.getString1(), "string1"); + Assertions.assertEquals(unCamelData.getString2(), "string2"); + Assertions.assertEquals(unCamelData.getSTring3(), "string3"); + Assertions.assertEquals(unCamelData.getSTring4(), "string4"); + Assertions.assertEquals(unCamelData.getSTRING5(), "string5"); + Assertions.assertEquals(unCamelData.getSTRing6(), "string6"); log.debug("First row:{}", JSON.toJSONString(list.get(0))); } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataTest.java index e8696c5a..f60e3636 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataTest.java @@ -7,23 +7,22 @@ import java.util.List; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class UnCamelDataTest { private static File file07; private static File file03; private static File fileCsv; - - @BeforeClass + @BeforeAll public static void init() { file07 = TestFileUtil.createNewFile("unCame07.xlsx"); file03 = TestFileUtil.createNewFile("unCame03.xls"); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataListener.java index f465e92c..7a3a9cb9 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataListener.java @@ -3,14 +3,14 @@ package com.alibaba.easyexcel.test.core.parameter; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Assertions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ @@ -25,10 +25,10 @@ public class ParameterDataListener extends AnalysisEventListener @Override public void doAfterAllAnalysed(AnalysisContext context) { - Assert.assertEquals(list.size(), 10); - Assert.assertEquals(list.get(0).getName(), "姓名0"); - Assert.assertEquals((int)(context.readSheetHolder().getSheetNo()), 0); - Assert.assertEquals( + Assertions.assertEquals(list.size(), 10); + Assertions.assertEquals(list.get(0).getName(), "姓名0"); + Assertions.assertEquals((int)(context.readSheetHolder().getSheetNo()), 0); + Assertions.assertEquals( context.readSheetHolder().getExcelReadHeadProperty().getHeadMap().get(0).getHeadNameList().get(0), "姓名"); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataTest.java index 4e0f2a09..ee5e294b 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataTest.java @@ -16,21 +16,21 @@ import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteTable; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class ParameterDataTest { private static File file07; private static File fileCsv; - @BeforeClass + @BeforeAll public static void init() { file07 = TestFileUtil.createNewFile("parameter07.xlsx"); fileCsv = TestFileUtil.createNewFile("parameterCsv.csv"); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataListener.java index 4827f67a..d4a57bcb 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataListener.java @@ -3,15 +3,15 @@ package com.alibaba.easyexcel.test.core.repetition; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.easyexcel.test.core.simple.SimpleDataListener; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Assertions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ @@ -26,9 +26,9 @@ public class RepetitionDataListener extends AnalysisEventListener { @Override public void invokeHeadMap(Map headMap, AnalysisContext context) { LOGGER.debug("Head is:{}", JSON.toJSONString(headMap)); - Assert.assertEquals(headMap.get(0), "姓名"); + Assertions.assertEquals(headMap.get(0), "姓名"); } @Override @@ -32,10 +32,10 @@ public class SimpleDataListener extends AnalysisEventListener { @Override public void doAfterAllAnalysed(AnalysisContext context) { - Assert.assertEquals(list.size(), 10); - Assert.assertEquals(list.get(0).getName(), "姓名0"); - Assert.assertEquals((int)(context.readSheetHolder().getSheetNo()), 0); - Assert.assertEquals( + Assertions.assertEquals(list.size(), 10); + Assertions.assertEquals(list.get(0).getName(), "姓名0"); + Assertions.assertEquals((int)(context.readSheetHolder().getSheetNo()), 0); + Assertions.assertEquals( context.readSheetHolder().getExcelReadHeadProperty().getHeadMap().get(0).getHeadNameList().get(0), "姓名"); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataSheetNameListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataSheetNameListener.java index 59cfa64f..09d8cbe4 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataSheetNameListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataSheetNameListener.java @@ -3,14 +3,14 @@ package com.alibaba.easyexcel.test.core.simple; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Assertions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ @@ -25,8 +25,8 @@ public class SimpleDataSheetNameListener extends AnalysisEventListener(dataList -> { - Assert.assertEquals(5, dataList.size()); + Assertions.assertEquals(5, dataList.size()); }, 5)) .sheet().doRead(); } @@ -114,9 +113,9 @@ public class SimpleDataTest { private void synchronousRead(File file) { // Synchronous read file List list = EasyExcel.read(file).head(SimpleData.class).sheet().doReadSync(); - Assert.assertEquals(list.size(), 10); - Assert.assertTrue(list.get(0) instanceof SimpleData); - Assert.assertEquals(((SimpleData)list.get(0)).getName(), "姓名0"); + Assertions.assertEquals(list.size(), 10); + Assertions.assertTrue(list.get(0) instanceof SimpleData); + Assertions.assertEquals(((SimpleData)list.get(0)).getName(), "姓名0"); } private List data() { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipDataTest.java index 0d0777e2..07e2415f 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipDataTest.java @@ -14,23 +14,23 @@ import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.write.metadata.WriteSheet; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class SkipDataTest { private static File file07; private static File file03; private static File fileCsv; - @BeforeClass + @BeforeAll public static void init() { file07 = TestFileUtil.createNewFile("skip.xlsx"); file03 = TestFileUtil.createNewFile("skip.xls"); @@ -49,7 +49,7 @@ public class SkipDataTest { @Test public void t03ReadAndWriteCsv() { - Assert.assertThrows(ExcelGenerateException.class, () -> readAndWrite(fileCsv)); + Assertions.assertThrows(ExcelGenerateException.class, () -> readAndWrite(fileCsv)); } private void readAndWrite(File file) { @@ -65,8 +65,8 @@ public class SkipDataTest { } List list = EasyExcel.read(file, SkipData.class, null).sheet("第二个").doReadSync(); - Assert.assertEquals(1, list.size()); - Assert.assertEquals("name2", list.get(0).getName()); + Assertions.assertEquals(1, list.size()); + Assertions.assertEquals("name2", list.get(0).getName()); SyncReadListener syncReadListener = new SyncReadListener(); try (ExcelReader excelReader = EasyExcel.read(file, SkipData.class, null).registerReadListener(syncReadListener) @@ -75,9 +75,9 @@ public class SkipDataTest { ReadSheet readSheet3 = EasyExcel.readSheet("第四个").build(); excelReader.read(readSheet1, readSheet3); List syncList = syncReadListener.getList(); - Assert.assertEquals(2, syncList.size()); - Assert.assertEquals("name2", ((SkipData)syncList.get(0)).getName()); - Assert.assertEquals("name4", ((SkipData)syncList.get(1)).getName()); + Assertions.assertEquals(2, syncList.size()); + Assertions.assertEquals("name2", ((SkipData)syncList.get(0)).getName()); + Assertions.assertEquals("name4", ((SkipData)syncList.get(1)).getName()); } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/sort/SortDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/sort/SortDataListener.java index 4a9df22f..dae966c1 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/sort/SortDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/sort/SortDataListener.java @@ -3,13 +3,13 @@ package com.alibaba.easyexcel.test.core.sort; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; +import org.junit.jupiter.api.Assertions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ @@ -18,7 +18,6 @@ public class SortDataListener extends AnalysisEventListener { private static final Logger LOGGER = LoggerFactory.getLogger(SortDataListener.class); List list = new ArrayList(); - @Override public void invoke(SortData data, AnalysisContext context) { list.add(data); @@ -26,13 +25,13 @@ public class SortDataListener extends AnalysisEventListener { @Override public void doAfterAllAnalysed(AnalysisContext context) { - Assert.assertEquals(list.size(), 1); + Assertions.assertEquals(list.size(), 1); SortData sortData = list.get(0); - Assert.assertEquals("column1", sortData.getColumn1()); - Assert.assertEquals("column2", sortData.getColumn2()); - Assert.assertEquals("column3", sortData.getColumn3()); - Assert.assertEquals("column4", sortData.getColumn4()); - Assert.assertEquals("column5", sortData.getColumn5()); - Assert.assertEquals("column6", sortData.getColumn6()); + Assertions.assertEquals("column1", sortData.getColumn1()); + Assertions.assertEquals("column2", sortData.getColumn2()); + Assertions.assertEquals("column3", sortData.getColumn3()); + Assertions.assertEquals("column4", sortData.getColumn4()); + Assertions.assertEquals("column5", sortData.getColumn5()); + Assertions.assertEquals("column6", sortData.getColumn6()); } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/sort/SortDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/sort/SortDataTest.java index f1a2b618..74f34b9b 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/sort/SortDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/sort/SortDataTest.java @@ -9,16 +9,16 @@ import java.util.Map; 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; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class SortDataTest { private static File file07; @@ -28,7 +28,7 @@ public class SortDataTest { private static File sortNoHead03; private static File sortNoHeadCsv; - @BeforeClass + @BeforeAll public static void init() { file07 = TestFileUtil.createNewFile("sort.xlsx"); file03 = TestFileUtil.createNewFile("sort.xls"); @@ -71,14 +71,14 @@ public class SortDataTest { private void readAndWrite(File file) { EasyExcel.write(file, SortData.class).sheet().doWrite(data()); List> dataMap = EasyExcel.read(file).sheet().doReadSync(); - Assert.assertEquals(1, dataMap.size()); + Assertions.assertEquals(1, dataMap.size()); Map record = dataMap.get(0); - Assert.assertEquals("column1", record.get(0)); - Assert.assertEquals("column2", record.get(1)); - Assert.assertEquals("column3", record.get(2)); - Assert.assertEquals("column4", record.get(3)); - Assert.assertEquals("column5", record.get(4)); - Assert.assertEquals("column6", record.get(5)); + Assertions.assertEquals("column1", record.get(0)); + Assertions.assertEquals("column2", record.get(1)); + Assertions.assertEquals("column3", record.get(2)); + Assertions.assertEquals("column4", record.get(3)); + Assertions.assertEquals("column5", record.get(4)); + Assertions.assertEquals("column6", record.get(5)); EasyExcel.read(file, SortData.class, new SortDataListener()).sheet().doRead(); } @@ -86,14 +86,14 @@ public class SortDataTest { private void readAndWriteNoHead(File file) { EasyExcel.write(file).head(head()).sheet().doWrite(data()); List> dataMap = EasyExcel.read(file).sheet().doReadSync(); - Assert.assertEquals(1, dataMap.size()); + Assertions.assertEquals(1, dataMap.size()); Map record = dataMap.get(0); - Assert.assertEquals("column1", record.get(0)); - Assert.assertEquals("column2", record.get(1)); - Assert.assertEquals("column3", record.get(2)); - Assert.assertEquals("column4", record.get(3)); - Assert.assertEquals("column5", record.get(4)); - Assert.assertEquals("column6", record.get(5)); + Assertions.assertEquals("column1", record.get(0)); + Assertions.assertEquals("column2", record.get(1)); + Assertions.assertEquals("column3", record.get(2)); + Assertions.assertEquals("column4", record.get(3)); + Assertions.assertEquals("column5", record.get(4)); + Assertions.assertEquals("column6", record.get(5)); EasyExcel.read(file, SortData.class, new SortDataListener()).sheet().doRead(); } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataListener.java index ee16e023..64e7ff8b 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataListener.java @@ -3,15 +3,15 @@ package com.alibaba.easyexcel.test.core.style; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.easyexcel.test.core.simple.SimpleDataListener; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Assertions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ @@ -26,9 +26,9 @@ public class StyleDataListener extends AnalysisEventListener { @Override public void doAfterAllAnalysed(AnalysisContext context) { - Assert.assertEquals(list.size(), 2); - Assert.assertEquals(list.get(0).getString(), "字符串0"); - Assert.assertEquals(list.get(1).getString(), "字符串1"); + Assertions.assertEquals(list.size(), 2); + Assertions.assertEquals(list.get(0).getString(), "字符串0"); + Assertions.assertEquals(list.get(1).getString(), "字符串1"); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java index e9323761..989c9c56 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java @@ -33,16 +33,16 @@ import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class StyleDataTest { private static File file07; @@ -51,7 +51,7 @@ public class StyleDataTest { private static File fileVerticalCellStyleStrategy207; private static File fileLoopMergeStrategy; - @BeforeClass + @BeforeAll public static void init() { file07 = TestFileUtil.createNewFile("style07.xlsx"); file03 = TestFileUtil.createNewFile("style03.xls"); @@ -209,30 +209,30 @@ public class StyleDataTest { Workbook workbook = WorkbookFactory.create(file); Sheet sheet = workbook.getSheetAt(0); - Assert.assertEquals(50 * 256, sheet.getColumnWidth(0), 0); + Assertions.assertEquals(50 * 256, sheet.getColumnWidth(0), 0); Row row0 = sheet.getRow(0); - Assert.assertEquals(800, row0.getHeight(), 0); + Assertions.assertEquals(800, row0.getHeight(), 0); Cell cell00 = row0.getCell(0); - Assert.assertArrayEquals(new byte[] {-1, -1, 0}, StyleTestUtils.getFillForegroundColor(cell00)); - Assert.assertArrayEquals(new byte[] {-128, -128, 0}, StyleTestUtils.getFontColor(cell00, workbook)); - Assert.assertEquals(20, StyleTestUtils.getFontHeightInPoints(cell00, workbook)); + Assertions.assertArrayEquals(new byte[] {-1, -1, 0}, StyleTestUtils.getFillForegroundColor(cell00)); + Assertions.assertArrayEquals(new byte[] {-128, -128, 0}, StyleTestUtils.getFontColor(cell00, workbook)); + Assertions.assertEquals(20, StyleTestUtils.getFontHeightInPoints(cell00, workbook)); Cell cell01 = row0.getCell(1); - Assert.assertArrayEquals(new byte[] {-1, -1, 0}, StyleTestUtils.getFillForegroundColor(cell01)); - Assert.assertArrayEquals(new byte[] {-128, -128, 0}, StyleTestUtils.getFontColor(cell01, workbook)); - Assert.assertEquals(20, StyleTestUtils.getFontHeightInPoints(cell01, workbook)); + Assertions.assertArrayEquals(new byte[] {-1, -1, 0}, StyleTestUtils.getFillForegroundColor(cell01)); + Assertions.assertArrayEquals(new byte[] {-128, -128, 0}, StyleTestUtils.getFontColor(cell01, workbook)); + Assertions.assertEquals(20, StyleTestUtils.getFontHeightInPoints(cell01, workbook)); Row row1 = sheet.getRow(1); - Assert.assertEquals(1000, row1.getHeight(), 0); + Assertions.assertEquals(1000, row1.getHeight(), 0); Cell cell10 = row1.getCell(0); - Assert.assertArrayEquals(new byte[] {0, -128, -128}, StyleTestUtils.getFillForegroundColor(cell10)); - Assert.assertArrayEquals(new byte[] {0, 51, 102}, StyleTestUtils.getFontColor(cell10, workbook)); - Assert.assertEquals(30, StyleTestUtils.getFontHeightInPoints(cell10, workbook)); + Assertions.assertArrayEquals(new byte[] {0, -128, -128}, StyleTestUtils.getFillForegroundColor(cell10)); + Assertions.assertArrayEquals(new byte[] {0, 51, 102}, StyleTestUtils.getFontColor(cell10, workbook)); + Assertions.assertEquals(30, StyleTestUtils.getFontHeightInPoints(cell10, workbook)); Cell cell11 = row1.getCell(1); - Assert.assertArrayEquals(new byte[] {0, -128, -128}, StyleTestUtils.getFillForegroundColor(cell11)); - Assert.assertArrayEquals(new byte[] {0, 51, 102}, StyleTestUtils.getFontColor(cell11, workbook)); - Assert.assertEquals(30, StyleTestUtils.getFontHeightInPoints(cell11, workbook)); + Assertions.assertArrayEquals(new byte[] {0, -128, -128}, StyleTestUtils.getFillForegroundColor(cell11)); + Assertions.assertArrayEquals(new byte[] {0, 51, 102}, StyleTestUtils.getFontColor(cell11, workbook)); + Assertions.assertEquals(30, StyleTestUtils.getFontHeightInPoints(cell11, workbook)); } private List data() { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataListener.java index e3b8b3cf..94f526c0 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataListener.java @@ -3,15 +3,15 @@ package com.alibaba.easyexcel.test.core.template; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.easyexcel.test.core.simple.SimpleDataListener; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Assertions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ @@ -26,9 +26,9 @@ public class TemplateDataListener extends AnalysisEventListener { @Override public void doAfterAllAnalysed(AnalysisContext context) { - Assert.assertEquals(list.size(), 2); - Assert.assertEquals(list.get(0).getString0(), "字符串0"); - Assert.assertEquals(list.get(1).getString0(), "字符串1"); + Assertions.assertEquals(list.size(), 2); + Assertions.assertEquals(list.get(0).getString0(), "字符串0"); + Assertions.assertEquals(list.get(1).getString0(), "字符串1"); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataTest.java index d6ebd0aa..66c90d08 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataTest.java @@ -7,23 +7,21 @@ import java.util.List; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; /** - * * @author Jiaju Zhuang */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@TestMethodOrder(MethodOrderer.MethodName.class) public class TemplateDataTest { private static File file07; private static File file03; - - @BeforeClass + @BeforeAll public static void init() { file07 = TestFileUtil.createNewFile("template07.xlsx"); file03 = TestFileUtil.createNewFile("template03.xls"); 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 3b3c37fc..53b8b273 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 @@ -16,8 +16,7 @@ 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; +import org.junit.jupiter.api.Test; /** * 写的填充写法 @@ -25,7 +24,7 @@ import org.junit.Test; * @author Jiaju Zhuang * @since 2.1.1 */ -@Ignore + public class FillTest { /** * 最简单的填充 @@ -73,18 +72,7 @@ public class FillTest { // 这里 会填充到第一个sheet, 然后文件流会自动关闭 EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(data()); - // 方案2 分多次 填充 会使用文件缓存(省内存) jdk8 - // since: 3.0.0-beta1 - fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx"; - EasyExcel.write(fileName) - .withTemplate(templateFileName) - .sheet() - .doFill(() -> { - // 分页查询数据 - return data(); - }); - - // 方案3 分多次 填充 会使用文件缓存(省内存) + // 方案2 分多次 填充 会使用文件缓存(省内存) fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx"; try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) { WriteSheet writeSheet = EasyExcel.writerSheet().build(); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/rare/WriteTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/rare/WriteTest.java index c35d7f96..b41ae653 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/rare/WriteTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/rare/WriteTest.java @@ -11,10 +11,8 @@ import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.write.handler.RowWriteHandler; -import com.alibaba.excel.write.handler.SheetWriteHandler; import com.alibaba.excel.write.handler.WorkbookWriteHandler; import com.alibaba.excel.write.handler.context.RowWriteHandlerContext; -import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext; import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext; import com.alibaba.excel.write.metadata.WriteSheet; @@ -24,15 +22,14 @@ import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.streaming.SXSSFWorkbook; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * 记录一些不太常见的案例 * * @author Jiaju Zhuang */ -@Ignore + @Slf4j public class WriteTest { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java index a6ddaf95..7815d374 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java @@ -48,7 +48,7 @@ public class DemoDataListener implements ReadListener { /** * 这个每一条数据解析都会来调用 * - * @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()} + * @param data one row value. It is same as {@link AnalysisContext#readRowHolder()} * @param context */ @Override diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraListener.java index 130d5675..15c285df 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraListener.java @@ -6,7 +6,7 @@ import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; -import org.junit.Assert; +import org.junit.jupiter.api.Assertions; /** * 读取单元格的批注 @@ -27,7 +27,8 @@ public class DemoExtraListener implements ReadListener { log.info("读取到了一条额外信息:{}", JSON.toJSONString(extra)); switch (extra.getType()) { case COMMENT: - log.info("额外信息是批注,在rowIndex:{},columnIndex;{},内容是:{}", extra.getRowIndex(), extra.getColumnIndex(), + log.info("额外信息是批注,在rowIndex:{},columnIndex;{},内容是:{}", extra.getRowIndex(), + extra.getColumnIndex(), extra.getText()); break; case HYPERLINK: @@ -41,7 +42,7 @@ public class DemoExtraListener implements ReadListener { extra.getFirstRowIndex(), extra.getFirstColumnIndex(), extra.getLastRowIndex(), extra.getLastColumnIndex(), extra.getText()); } else { - Assert.fail("Unknown hyperlink!"); + Assertions.fail("Unknown hyperlink!"); } break; case MERGE: 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 83ec2c90..a405fae0 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 @@ -20,15 +20,14 @@ import com.alibaba.excel.util.ListUtils; import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * 读的常见写法 * * @author Jiaju Zhuang */ -@Ignore + @Slf4j public class ReadTest { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java index 2096cd4d..183bc140 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java @@ -44,7 +44,7 @@ public class UploadDataListener implements ReadListener { /** * 这个每一条数据解析都会来调用 * - * @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()} + * @param data one row value. It is same as {@link AnalysisContext#readRowHolder()} * @param context */ @Override diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDataWithAnnotation.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDataWithAnnotation.java index 4c39ffb1..ad59b68f 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDataWithAnnotation.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDataWithAnnotation.java @@ -15,7 +15,6 @@ import lombok.Setter; /** * 图片导出类 - * */ @Getter @Setter 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 705c2b53..0e7cf2db 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 @@ -4,11 +4,13 @@ import java.io.File; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; +import com.alibaba.easyexcel.test.core.head.ComplexHeadData; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; @@ -46,15 +48,14 @@ import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.streaming.SXSSFSheet; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * 写的常见写法 * * @author Jiaju Zhuang */ -@Ignore + public class WriteTest { /** @@ -410,7 +411,7 @@ public class WriteTest { /** * 列宽、行高 *

- * 1. 创建excel对应的实体对象 参照{@link WidthAndHeightData} + * 1. 创建excel对应的实体对象 参照{@link WidthAndHeightData } *

* 2. 使用注解{@link ColumnWidth}、{@link HeadRowHeight}、{@link ContentRowHeight}指定宽度或高度 *

diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/DemoData3.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/DemoData3.java index 0be8ec7a..9daffc57 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/DemoData3.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/DemoData3.java @@ -1,9 +1,7 @@ package com.alibaba.easyexcel.test.temp; import java.time.LocalDateTime; -import java.util.Date; -import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelProperty; import lombok.EqualsAndHashCode; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/FillTempTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/FillTempTest.java index 7917e063..d28fe298 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/FillTempTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/FillTempTest.java @@ -5,9 +5,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.demo.fill.FillData; import com.alibaba.easyexcel.test.temp.fill.FillData2; import com.alibaba.easyexcel.test.util.TestFileUtil; @@ -16,13 +13,15 @@ import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy; import com.alibaba.excel.write.metadata.WriteSheet; +import org.junit.jupiter.api.Test; + /** * 写的填充写法 * - * @since 2.1.1 * @author Jiaju Zhuang + * @since 2.1.1 */ -@Ignore + public class FillTempTest { /** @@ -37,7 +36,8 @@ public class FillTempTest { OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(2, 2, 0, 1); String fileName = TestFileUtil.getPath() + "complexFill" + System.currentTimeMillis() + ".xlsx"; - ExcelWriter excelWriter = EasyExcel.write(fileName).registerWriteHandler(onceAbsoluteMergeStrategy).withTemplate(TestFileUtil.readUserHomeFile("test/simple.xlsx")).build(); + ExcelWriter excelWriter = EasyExcel.write(fileName).registerWriteHandler(onceAbsoluteMergeStrategy) + .withTemplate(TestFileUtil.readUserHomeFile("test/simple.xlsx")).build(); WriteSheet writeSheet0 = EasyExcel.writerSheet(0).build(); WriteSheet writeSheet1 = EasyExcel.writerSheet(1).build(); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java index 6e85addf..db1ead44 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java @@ -18,8 +18,6 @@ import com.alibaba.easyexcel.test.demo.write.DemoData; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.metadata.data.ReadCellData; -import com.alibaba.excel.util.NumberDataFormatterUtils; -import com.alibaba.excel.util.NumberUtils; import com.alibaba.excel.util.PositionUtils; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteFont; @@ -32,11 +30,10 @@ import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.IndexedColors; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.util.StringUtils; /** * 临时测试 @@ -55,7 +52,8 @@ public class Lock2Test { File file = new File("/Users/zhuangjiaju/IdeaProjects/easyexcel/src/test/resources/converter/converter07.xlsx"); List list = EasyExcel.read( - "/Users/zhuangjiaju/IdeaProjects/easyexcel/easyexcel-test/target/test-classes/simpleWrite1674051907397.xlsx") + "/Users/zhuangjiaju/IdeaProjects/easyexcel/easyexcel-test/target/test-classes" + + "/simpleWrite1674051907397.xlsx") //.useDefaultListener(false) .sheet(0) .headRowNumber(0).doReadSync(); @@ -266,9 +264,9 @@ public class Lock2Test { Date date = new Date(dateTime); double excelDate = DateUtil.getExcelDate(date); - Assert.assertEquals("测试基本转换错误" + dateTime, format.format(date), + Assertions.assertEquals("测试基本转换错误" + dateTime, format.format(date), format.format(DateUtil.getJavaDate(excelDate, false))); - Assert.assertEquals("测试精度5转换错误" + dateTime, format.format(date), + Assertions.assertEquals("测试精度5转换错误" + dateTime, format.format(date), format.format(DateUtil.getJavaDate(BigDecimal.valueOf(excelDate) .setScale(10, RoundingMode.HALF_UP).doubleValue(), false))); LOGGER.info("date:{}", format2.format(DateUtil.getJavaDate(BigDecimal.valueOf(excelDate) @@ -353,9 +351,8 @@ public class Lock2Test { @Test public void numberforamt99() throws Exception { - LocalDateTime localDateTime=LocalDateTime.of(2023, 1, 1, 0, 0, 0, 995000000); - log.info("date:{}",localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))); - + LocalDateTime localDateTime = LocalDateTime.of(2023, 1, 1, 0, 0, 0, 995000000); + log.info("date:{}", localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))); } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java index d7003f25..6a95ab04 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java @@ -3,14 +3,14 @@ package com.alibaba.easyexcel.test.temp; import java.util.ArrayList; import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.easyexcel.test.demo.read.DemoDataListener; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson2.JSON; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * 模板的读取类 * diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java index 8e4ec6c9..9dac75b0 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java @@ -7,8 +7,7 @@ import java.util.Map; import com.alibaba.excel.EasyExcel; import com.alibaba.fastjson2.JSON; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,14 +16,15 @@ import org.slf4j.LoggerFactory; * * @author Jiaju Zhuang **/ -@Ignore + public class LockTest { private static final Logger LOGGER = LoggerFactory.getLogger(LockTest.class); @Test public void test() throws Exception { List list = - EasyExcel.read(new FileInputStream("/Users/zhuangjiaju/Downloads/-0304.2.xlsx")).useDefaultListener(false).doReadAllSync(); + EasyExcel.read(new FileInputStream("/Users/zhuangjiaju/Downloads/-0304.2.xlsx")).useDefaultListener(false) + .doReadAllSync(); for (Object data : list) { LOGGER.info("返回数据:{}", JSON.toJSONString(data)); } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/StyleData.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/StyleData.java index 89453e5e..df898cf0 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/StyleData.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/StyleData.java @@ -7,7 +7,6 @@ import lombok.Getter; import lombok.Setter; /** - * * @author Jiaju Zhuang **/ @Getter diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/StyleTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/StyleTest.java index b4675b1d..f604eda6 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/StyleTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/StyleTest.java @@ -7,6 +7,10 @@ import java.lang.reflect.Field; import java.util.Date; import java.util.List; +import com.alibaba.excel.EasyExcel; +import com.alibaba.fastjson2.JSON; + +import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; @@ -21,20 +25,16 @@ import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.alibaba.excel.EasyExcel; -import com.alibaba.fastjson2.JSON; - /** * 临时测试 * * @author Jiaju Zhuang **/ -@Ignore + public class StyleTest { private static final Logger LOGGER = LoggerFactory.getLogger(StyleTest.class); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteLargeTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteLargeTest.java index aa772c20..846ebdb7 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteLargeTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteLargeTest.java @@ -1,21 +1,38 @@ package com.alibaba.easyexcel.test.temp; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; import java.util.ArrayList; import java.util.List; +import java.util.Map; import com.alibaba.easyexcel.test.core.large.LargeData; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; +import com.alibaba.excel.read.listener.PageReadListener; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; +import lombok.extern.slf4j.Slf4j; +import org.apache.poi.hssf.eventusermodel.HSSFEventFactory; +import org.apache.poi.hssf.eventusermodel.HSSFListener; +import org.apache.poi.hssf.eventusermodel.HSSFRequest; +import org.apache.poi.hssf.record.BOFRecord; +import org.apache.poi.hssf.record.BoundSheetRecord; +import org.apache.poi.hssf.record.Record; +import org.apache.poi.hssf.record.SSTRecord; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.IndexedColors; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -24,7 +41,8 @@ import org.slf4j.LoggerFactory; * * @author Jiaju Zhuang **/ -@Ignore + +@Slf4j public class WriteLargeTest { private static final Logger LOGGER = LoggerFactory.getLogger(WriteLargeTest.class); @@ -65,6 +83,91 @@ public class WriteLargeTest { } + @Test + public void read() throws Exception { + log.info("start"); + String fileName = "/Users/zhuangjiaju/Downloads/1e9e0578a9634abbbbd9b67f338f142a.xls"; + // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭 + // 这里默认每次会读取100条数据 然后返回过来 直接调用使用数据就行 + // 具体需要返回多少行可以在`PageReadListener`的构造函数设置 + EasyExcel.read(fileName, new PageReadListener>>(dataList -> { + log.info("SIZEL:{}", dataList.size()); + })).sheet().doRead(); + + log.info("test"); + + } + + @Test + public void read2() throws Exception { + // 使用输入的文件创建一个新的文件输入流 + //FileInputStream fin = new FileInputStream("/Users/zhuangjiaju/Downloads/1e9e0578a9634abbbbd9b67f338f142a + // .xls"); + // 创建一个新的org.apache.poi.poifs.filesystem.Filesystem + POIFSFileSystem poifs = new POIFSFileSystem( + new File("/Users/zhuangjiaju/Downloads/1e9e0578a9634abbbbd9b67f338f142a.xls")); + // 在InputStream中获取Workbook流 + InputStream din = poifs.createDocumentInputStream("Workbook"); + // 构造出HSSFRequest对象 + HSSFRequest req = new HSSFRequest(); + // 注册全部的监听器 + req.addListenerForAllRecords(new EventExample()); + // 创建事件工厂 + HSSFEventFactory factory = new HSSFEventFactory(); + // 根据文档输入流处理我们监听的事件 + factory.processEvents(req, din); + // 关闭文件输入流 + //fin.close(); + // 关闭文档输入流 + din.close(); + System.out.println("读取结束"); + } + + @Test + public void read3() throws Exception { + HSSFWorkbook hwb = new HSSFWorkbook( + new FileInputStream("/Users/zhuangjiaju/Downloads/1e9e0578a9634abbbbd9b67f338f142a.xls")); + HSSFSheet sheet = hwb.getSheetAt(0); + HSSFRow row = null; + HSSFCell cell = null; + for (int i = sheet.getFirstRowNum(); i <= sheet.getPhysicalNumberOfRows(); i++) { + row = sheet.getRow(i); + if(row!=null){ + log.info("r:{}",row.getRowNum()); + + } + } + + log.info("end"); + } + + public static class EventExample implements HSSFListener { + private SSTRecord sstrec; + + /** + * 此方法监听传入记录并根据需要处理它们 + * + * @param record读取时找到的记录 + */ + public void processRecord(Record record) { + switch (record.getSid()) { + //BOFRecord可以表示工作表或工作簿的开头 + case BOFRecord.sid: + BOFRecord bof = (BOFRecord)record; + if (bof.getType() == bof.TYPE_WORKBOOK) { + System.out.println("监听到工作表"); + } else if (bof.getType() == bof.TYPE_WORKSHEET) { + System.out.println("监听到工作簿"); + } + break; + case BoundSheetRecord.sid: + BoundSheetRecord bsr = (BoundSheetRecord)record; + System.out.println("工作簿名称: " + bsr.getSheetname()); + break; + } + } + } + @Test public void test2() throws Exception { // 方法2 如果写到不同的sheet 同一个对象 diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteV33Test.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteV33Test.java index 4e258e51..ed5b2385 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteV33Test.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteV33Test.java @@ -16,8 +16,7 @@ import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Workbook; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,7 +25,7 @@ import org.slf4j.LoggerFactory; * * @author Jiaju Zhuang **/ -@Ignore + public class WriteV33Test { private static final Logger LOGGER = LoggerFactory.getLogger(WriteV33Test.class); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteV34Test.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteV34Test.java index f9215d7b..68d8794b 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteV34Test.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteV34Test.java @@ -3,13 +3,6 @@ package com.alibaba.easyexcel.test.temp; import java.util.ArrayList; import java.util.List; -import org.apache.poi.ss.usermodel.FillPatternType; -import org.apache.poi.ss.usermodel.IndexedColors; -import org.junit.Ignore; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.easyexcel.test.demo.write.DemoData; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; @@ -17,12 +10,18 @@ import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; +import org.apache.poi.ss.usermodel.FillPatternType; +import org.apache.poi.ss.usermodel.IndexedColors; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * 临时测试 * * @author Jiaju Zhuang **/ -@Ignore + public class WriteV34Test { private static final Logger LOGGER = LoggerFactory.getLogger(WriteV34Test.class); @@ -52,11 +51,11 @@ public class WriteV34Test { new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 - EasyExcel.write(fileName,DemoData.class).head(head()).registerWriteHandler(horizontalCellStyleStrategy).sheet("模板") + EasyExcel.write(fileName, DemoData.class).head(head()).registerWriteHandler(horizontalCellStyleStrategy).sheet( + "模板") .doWrite(data(1)); } - private List> head() { List> list = new ArrayList>(); List head0 = new ArrayList(); @@ -70,8 +69,8 @@ public class WriteV34Test { head3.add("日期" + System.currentTimeMillis()); list.add(head0); list.add(head1); - list.add(head2); list.add(head3); - + list.add(head2); + list.add(head3); return list; } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Xls03Test.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Xls03Test.java index b7624599..7d5a4490 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Xls03Test.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Xls03Test.java @@ -6,8 +6,7 @@ import com.alibaba.excel.EasyExcel; import com.alibaba.excel.util.BeanMapUtils; import com.alibaba.fastjson2.JSON; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cglib.beans.BeanMap; @@ -18,7 +17,7 @@ import org.springframework.cglib.core.DebuggingClassWriter; * * @author Jiaju Zhuang **/ -@Ignore + public class Xls03Test { private static final Logger LOGGER = LoggerFactory.getLogger(Xls03Test.class); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/bug/ExcelCreat.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/bug/ExcelCreat.java index f9d97463..f190ea78 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/bug/ExcelCreat.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/bug/ExcelCreat.java @@ -1,14 +1,14 @@ package com.alibaba.easyexcel.test.temp.bug; -import com.alibaba.excel.EasyExcel; -import com.alibaba.excel.ExcelWriter; -import com.alibaba.excel.write.metadata.WriteSheet; - import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.Collections; import java.util.List; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelWriter; +import com.alibaba.excel.write.metadata.WriteSheet; + /** * @author jiaosong * @desc @@ -16,7 +16,6 @@ import java.util.List; */ public class ExcelCreat { - public static void main(String[] args) throws FileNotFoundException { List data = getData(); ExcelWriter excelWriter = null; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/bug/HeadType.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/bug/HeadType.java index bcfa101c..e04f74a7 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/bug/HeadType.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/bug/HeadType.java @@ -16,14 +16,12 @@ import lombok.Setter; @EqualsAndHashCode public class HeadType { - /** * 任务id */ @ExcelProperty("任务ID") private Integer id; - @ExcelProperty(value = "备注1") private String firstRemark; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/cache/CacheTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/cache/CacheTest.java index 22015457..7ff376f6 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/cache/CacheTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/cache/CacheTest.java @@ -4,26 +4,23 @@ import java.io.File; import java.util.HashMap; import java.util.UUID; +import com.alibaba.easyexcel.test.temp.poi.Poi2Test; +import com.alibaba.excel.util.FileUtils; +import com.alibaba.fastjson2.JSON; + import org.ehcache.Cache; import org.ehcache.PersistentCacheManager; import org.ehcache.config.builders.CacheConfigurationBuilder; import org.ehcache.config.builders.CacheManagerBuilder; import org.ehcache.config.builders.ResourcePoolsBuilder; import org.ehcache.config.units.MemoryUnit; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.alibaba.easyexcel.test.temp.poi.Poi2Test; -import com.alibaba.excel.util.FileUtils; -import com.alibaba.fastjson2.JSON; - /** - * * @author Jiaju Zhuang **/ -@Ignore public class CacheTest { private static final Logger LOGGER = LoggerFactory.getLogger(Poi2Test.class); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java index 2a6c69ce..015970c3 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java @@ -18,10 +18,8 @@ import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; import org.apache.commons.csv.CSVRecord; import org.apache.poi.poifs.filesystem.FileMagic; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; -@Ignore @Slf4j public class CsvReadTest { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/data/HeadType.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/data/HeadType.java index 2eaf0a27..53fd0df4 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/data/HeadType.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/data/HeadType.java @@ -11,14 +11,12 @@ import lombok.Setter; @EqualsAndHashCode public class HeadType { - /** * 任务id */ @ExcelProperty("任务ID") private Integer id; - @ExcelProperty(value = "备注1") private String firstRemark; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatTest.java index 280a5046..3e7baf74 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatTest.java @@ -22,8 +22,7 @@ import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,7 +31,7 @@ import org.slf4j.LoggerFactory; * * @author Jiaju Zhuang **/ -@Ignore + public class DataFormatTest { private static final Logger LOGGER = LoggerFactory.getLogger(Lock2Test.class); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatter1.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatter1.java index 05f3d09f..bb143e85 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatter1.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatter1.java @@ -121,16 +121,24 @@ import org.slf4j.LoggerFactory; public class DataFormatter1 implements Observer { private static final String defaultFractionWholePartFormat = "#"; private static final String defaultFractionFractionPartFormat = "#/##"; - /** Pattern to find a number format: "0" or "#" */ + /** + * Pattern to find a number format: "0" or "#" + */ private static final Pattern numPattern = Pattern.compile("[0#]+"); - /** Pattern to find days of week as text "ddd...." */ + /** + * Pattern to find days of week as text "ddd...." + */ private static final Pattern daysAsText = Pattern.compile("([d]{3,})", Pattern.CASE_INSENSITIVE); - /** Pattern to find "AM/PM" marker */ + /** + * Pattern to find "AM/PM" marker + */ private static final Pattern amPmPattern = Pattern.compile("((A|P)[M/P]*)", Pattern.CASE_INSENSITIVE); - /** Pattern to find formats with condition ranges e.g. [>=100] */ + /** + * Pattern to find formats with condition ranges e.g. [>=100] + */ private static final Pattern rangeConditionalPattern = Pattern.compile(".*\\[\\s*(>|>=|<|<=|=)\\s*[0-9]*\\.*[0-9].*"); @@ -168,10 +176,10 @@ public class DataFormatter1 implements Observer { * ("#"). */ private static final String invalidDateTimeString; + static { StringBuilder buf = new StringBuilder(); - for (int i = 0; i < 255; i++) - buf.append('#'); + for (int i = 0; i < 255; i++) {buf.append('#');} invalidDateTimeString = buf.toString(); } @@ -190,10 +198,14 @@ public class DataFormatter1 implements Observer { */ private DateFormat defaultDateformat; - /** General format for numbers. */ + /** + * General format for numbers. + */ private Format generalNumberFormat; - /** A default format to use when a number pattern cannot be parsed. */ + /** + * A default format to use when a number pattern cannot be parsed. + */ private Format defaultNumFormat; /** @@ -203,10 +215,14 @@ public class DataFormatter1 implements Observer { private final boolean emulateCSV; - /** stores the locale valid it the last formatting call */ + /** + * stores the locale valid it the last formatting call + */ private Locale locale; - /** stores if the locale should change according to {@link LocaleUtil#getUserLocale()} */ + /** + * stores if the locale should change according to {@link LocaleUtil#getUserLocale()} + */ private boolean localeIsAdapting; private class LocaleChangeObservable extends Observable { @@ -215,19 +231,21 @@ public class DataFormatter1 implements Observer { } void checkForLocaleChange(Locale newLocale) { - if (!localeIsAdapting) - return; - if (newLocale.equals(locale)) - return; + if (!localeIsAdapting) {return;} + if (newLocale.equals(locale)) {return;} super.setChanged(); notifyObservers(newLocale); } } - /** the Observable to notify, when the locale has been changed */ + /** + * the Observable to notify, when the locale has been changed + */ private final LocaleChangeObservable localeChangedObservable = new LocaleChangeObservable(); - /** For logging any problems we find */ + /** + * For logging any problems we find + */ private static final Logger logger = LoggerFactory.getLogger(ExcelAnalyserImpl.class); /** @@ -240,8 +258,7 @@ public class DataFormatter1 implements Observer { /** * Creates a formatter using the {@link Locale#getDefault() default locale}. * - * @param emulateCSV - * whether to emulate CSV output. + * @param emulateCSV whether to emulate CSV output. */ public DataFormatter1(boolean emulateCSV) { this(LocaleUtil.getUserLocale(), true, emulateCSV); @@ -257,8 +274,7 @@ public class DataFormatter1 implements Observer { /** * Creates a formatter using the given locale. * - * @param emulateCSV - * whether to emulate CSV output. + * @param emulateCSV whether to emulate CSV output. */ public DataFormatter1(Locale locale, boolean emulateCSV) { this(locale, false, emulateCSV); @@ -267,10 +283,8 @@ public class DataFormatter1 implements Observer { /** * Creates a formatter using the given locale. * - * @param localeIsAdapting - * (true only if locale is not user-specified) - * @param emulateCSV - * whether to emulate CSV output. + * @param localeIsAdapting (true only if locale is not user-specified) + * @param emulateCSV whether to emulate CSV output. */ private DataFormatter1(Locale locale, boolean localeIsAdapting, boolean emulateCSV) { this.localeIsAdapting = true; @@ -293,13 +307,11 @@ public class DataFormatter1 implements Observer { *
  • the format string cannot be recognized as either a number or date
  • * * - * @param cell - * The cell to retrieve a Format for + * @param cell The cell to retrieve a Format for * @return A Format for the format String */ private Format getFormat(Cell cell, ConditionalFormattingEvaluator cfEvaluator) { - if (cell == null) - return null; + if (cell == null) {return null;} ExcelNumberFormat numFmt = ExcelNumberFormat.from(cell, cfEvaluator); @@ -340,7 +352,7 @@ public class DataFormatter1 implements Observer { // CellFormat requires callers to identify date vs not, so do so Object cellValueO = Double.valueOf(cellValue); if (DateUtil.isADateFormat(formatIndex, formatStr) && - // don't try to handle Date value 0, let a 3 or 4-part format take care of it + // don't try to handle Date value 0, let a 3 or 4-part format take care of it ((Double)cellValueO).doubleValue() != 0.0) { cellValueO = DateUtil.getJavaDate(cellValue); } @@ -377,8 +389,7 @@ public class DataFormatter1 implements Observer { * Create and return a Format based on the format string from a cell's style. If the pattern cannot be parsed, * return a default pattern. * - * @param cell - * The Excel cell + * @param cell The Excel cell * @return A Format representing the excel format. May return null. */ public Format createFormat(Cell cell) { @@ -400,11 +411,9 @@ public class DataFormatter1 implements Observer { // Paranoid replacement... int at = formatStr.indexOf(colour); - if (at == -1) - break; + if (at == -1) {break;} String nFormatStr = formatStr.substring(0, at) + formatStr.substring(at + colour.length()); - if (nFormatStr.equals(formatStr)) - break; + if (nFormatStr.equals(formatStr)) {break;} // Try again in case there's multiple formatStr = nFormatStr; @@ -433,7 +442,7 @@ public class DataFormatter1 implements Observer { return generalNumberFormat; } - if ("".equals("")||(DateUtil.isADateFormat(formatIndex, formatStr) && DateUtil.isValidExcelDate(cellValue))) { + if ("".equals("") || (DateUtil.isADateFormat(formatIndex, formatStr) && DateUtil.isValidExcelDate(cellValue))) { return createDateFormat(formatStr, cellValue); } // Excel supports fractions in format strings, which Java doesn't @@ -761,8 +770,7 @@ public class DataFormatter1 implements Observer { /** * Returns a default format for a cell. * - * @param cell - * The cell + * @param cell The cell * @return a default format */ public Format getDefaultFormat(Cell cell) { @@ -796,10 +804,8 @@ public class DataFormatter1 implements Observer { * number format, or no rules apply, the cell's style format is used. If the style does not have a format, the * default date format is applied. * - * @param cell - * to format - * @param cfEvaluator - * ConditionalFormattingEvaluator (if available) + * @param cell to format + * @param cfEvaluator ConditionalFormattingEvaluator (if available) * @return Formatted value */ private String getFormattedDateString(Cell cell, ConditionalFormattingEvaluator cfEvaluator) { @@ -820,10 +826,8 @@ public class DataFormatter1 implements Observer { * Format comes from either the highest priority conditional format rule with a specified format, or from the cell * style. * - * @param cell - * The cell - * @param cfEvaluator - * if available, or null + * @param cell The cell + * @param cfEvaluator if available, or null * @return a formatted number string */ private String getFormattedNumberString(Cell cell, ConditionalFormattingEvaluator cfEvaluator) { @@ -906,8 +910,7 @@ public class DataFormatter1 implements Observer { * will not be evaluated. *

    * - * @param cell - * The cell + * @param cell The cell * @return the formatted cell value as a String */ public String formatCellValue(Cell cell) { @@ -925,10 +928,8 @@ public class DataFormatter1 implements Observer { * String will be returned. The caller is responsible for setting the currentRow on the evaluator *

    * - * @param cell - * The cell (can be null) - * @param evaluator - * The FormulaEvaluator (can be null) + * @param cell The cell (can be null) + * @param evaluator The FormulaEvaluator (can be null) * @return a string value of the cell */ public String formatCellValue(Cell cell, FormulaEvaluator evaluator) { @@ -954,12 +955,9 @@ public class DataFormatter1 implements Observer { * The two evaluators should be from the same context, to avoid inconsistencies in cached values. *

    * - * @param cell - * The cell (can be null) - * @param evaluator - * The FormulaEvaluator (can be null) - * @param cfEvaluator - * ConditionalFormattingEvaluator (can be null) + * @param cell The cell (can be null) + * @param evaluator The FormulaEvaluator (can be null) + * @param cfEvaluator ConditionalFormattingEvaluator (can be null) * @return a string value of the cell */ public String formatCellValue(Cell cell, FormulaEvaluator evaluator, ConditionalFormattingEvaluator cfEvaluator) { @@ -979,10 +977,10 @@ public class DataFormatter1 implements Observer { switch (cellType) { case NUMERIC: -// if (DateUtil.isCellDateFormatted(cell, cfEvaluator)) { - return getFormattedDateString(cell, cfEvaluator); -// } -// return getFormattedNumberString(cell, cfEvaluator); + // if (DateUtil.isCellDateFormatted(cell, cfEvaluator)) { + return getFormattedDateString(cell, cfEvaluator); + // } + // return getFormattedNumberString(cell, cfEvaluator); case STRING: return cell.getRichStringCellValue().getString(); @@ -1010,8 +1008,7 @@ public class DataFormatter1 implements Observer { * Number value. *

    * - * @param format - * A Format instance to be used as a default + * @param format A Format instance to be used as a default * @see java.text.Format#format */ public void setDefaultNumberFormat(Format format) { @@ -1031,10 +1028,8 @@ public class DataFormatter1 implements Observer { * Number value. *

    * - * @param excelFormatStr - * The data format string - * @param format - * A Format instance + * @param excelFormatStr The data format string + * @param format A Format instance */ public void addFormat(String excelFormatStr, Format format) { formats.put(excelFormatStr, format); @@ -1062,10 +1057,8 @@ public class DataFormatter1 implements Observer { /** * Enables custom rounding mode on the given Decimal Format. * - * @param format - * DecimalFormat - * @param roundingMode - * RoundingMode + * @param format DecimalFormat + * @param roundingMode RoundingMode */ public static void setExcelStyleRoundingMode(DecimalFormat format, RoundingMode roundingMode) { format.setRoundingMode(roundingMode); @@ -1087,17 +1080,13 @@ public class DataFormatter1 implements Observer { /** * Update formats when locale has been changed * - * @param observable - * usually this is our own Observable instance - * @param localeObj - * only reacts on Locale objects + * @param observable usually this is our own Observable instance + * @param localeObj only reacts on Locale objects */ public void update(Observable observable, Object localeObj) { - if (!(localeObj instanceof Locale)) - return; + if (!(localeObj instanceof Locale)) {return;} Locale newLocale = (Locale)localeObj; - if (!localeIsAdapting || newLocale.equals(locale)) - return; + if (!localeIsAdapting || newLocale.equals(locale)) {return;} locale = newLocale; @@ -1142,7 +1131,9 @@ public class DataFormatter1 implements Observer { // enforce singleton } - /** Format a number as an SSN */ + /** + * Format a number as an SSN + */ public static String format(Number num) { String result = df.format(num); return result.substring(0, 3) + '-' + result.substring(3, 5) + '-' + result.substring(5, 9); @@ -1173,7 +1164,9 @@ public class DataFormatter1 implements Observer { // enforce singleton } - /** Format a number as Zip + 4 */ + /** + * Format a number as Zip + 4 + */ public static String format(Number num) { String result = df.format(num); return result.substring(0, 5) + '-' + result.substring(5, 9); @@ -1204,7 +1197,9 @@ public class DataFormatter1 implements Observer { // enforce singleton } - /** Format a number as a phone number */ + /** + * Format a number as a phone number + */ public static String format(Number num) { String result = df.format(num); StringBuilder sb = new StringBuilder(); @@ -1244,7 +1239,6 @@ public class DataFormatter1 implements Observer { * * This format is used to simulate Excel's handling of a format string of all # when the value is 0. Excel will * output "", Java will output "0". - * */ @SuppressWarnings("serial") private static final class ConstantStringFormat extends Format { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/fill/FillTempTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/fill/FillTempTest.java index 2c201863..352e4600 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/fill/FillTempTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/fill/FillTempTest.java @@ -16,16 +16,15 @@ 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; +import org.junit.jupiter.api.Test; /** * 写的填充写法 * - * @since 2.1.1 * @author Jiaju Zhuang + * @since 2.1.1 */ -@Ignore + public class FillTempTest { /** * 最简单的填充 @@ -35,7 +34,7 @@ public class FillTempTest { @Test public void simpleFill() { // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替 - String templateFileName ="/Users/zhuangjiaju/Downloads/simple.xlsx"; + String templateFileName = "/Users/zhuangjiaju/Downloads/simple.xlsx"; // 方案1 根据对象填充 String fileName = TestFileUtil.getPath() + "simpleFill" + System.currentTimeMillis() + ".xlsx"; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Issue1662Test.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Issue1662Test.java index cea7e5ee..466227fe 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Issue1662Test.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Issue1662Test.java @@ -7,10 +7,8 @@ import java.util.List; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; -@Ignore public class Issue1662Test { @Test public void test1662() { @@ -19,7 +17,6 @@ public class Issue1662Test { EasyExcel.write(fileName).head(head()).sheet("模板").doWrite(dataList()); } - private List> head() { List> list = new ArrayList>(); List head0 = new ArrayList(); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue1663/FillTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue1663/FillTest.java index 7f89621c..f9c2a2b4 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue1663/FillTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue1663/FillTest.java @@ -15,13 +15,11 @@ 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; +import org.junit.jupiter.api.Test; -@Ignore public class FillTest { @Test - public void TestFillNullPoint(){ + public void TestFillNullPoint() { String templateFileName = TestFileUtil.getPath() + "temp/issue1663" + File.separator + "template.xlsx"; @@ -37,8 +35,10 @@ public class FillTest { excelWriter.fill(map, writeSheet); excelWriter.finish(); } + private List data() { - List list = new ArrayList(); + List list + = new ArrayList(); for (int i = 0; i < 10; i++) { com.alibaba.easyexcel.test.demo.fill.FillData fillData = new FillData(); list.add(fillData); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue2443/Issue2443Test.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue2443/Issue2443Test.java index b9e2c965..d23f9fb8 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue2443/Issue2443Test.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue2443/Issue2443Test.java @@ -1,19 +1,20 @@ package com.alibaba.easyexcel.test.temp.issue2443; +import java.io.File; +import java.text.ParseException; + import com.alibaba.easyexcel.test.util.TestFileUtil; -import com.alibaba.excel.metadata.property.ExcelContentProperty; -import com.alibaba.excel.util.NumberUtils; import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.read.listener.PageReadListener; +import com.alibaba.excel.util.NumberUtils; import com.alibaba.fastjson2.JSON; + import lombok.extern.slf4j.Slf4j; -import org.junit.Ignore; -import org.junit.Test; -import static org.junit.Assert.*; -import java.io.File; -import java.text.ParseException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + -@Ignore @Slf4j public class Issue2443Test { //CS304 (manually written) Issue link: https://github.com/alibaba/easyexcel/issues/2443 @@ -26,6 +27,7 @@ public class Issue2443Test { } })).sheet().doRead(); } + //CS304 (manually written) Issue link: https://github.com/alibaba/easyexcel/issues/2443 @Test public void IssueTest2() { @@ -41,16 +43,16 @@ public class Issue2443Test { public void parseIntegerTest1() throws ParseException { String string = "1.00"; ExcelContentProperty contentProperty = null; - int Int = NumberUtils.parseInteger(string,contentProperty); - assertEquals(1, Int); + int Int = NumberUtils.parseInteger(string, contentProperty); + Assertions.assertEquals(1, Int); } @Test public void parseIntegerTest2() throws ParseException { String string = "2.00"; ExcelContentProperty contentProperty = null; - int Int = NumberUtils.parseInteger(string,contentProperty); - assertEquals(2, Int); + int Int = NumberUtils.parseInteger(string, contentProperty); + Assertions.assertEquals(2, Int); } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeDataListener.java index 25176101..b62ee1f6 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeDataListener.java @@ -1,12 +1,12 @@ package com.alibaba.easyexcel.test.temp.large; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson2.JSON; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/NoModelLargeDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/NoModelLargeDataListener.java index 2d532bb2..5e180624 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/NoModelLargeDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/NoModelLargeDataListener.java @@ -2,13 +2,13 @@ package com.alibaba.easyexcel.test.temp.large; 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.fastjson2.JSON; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/TempLargeDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/TempLargeDataTest.java index 934b62f5..b88bf06e 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/TempLargeDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/TempLargeDataTest.java @@ -19,17 +19,16 @@ import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author Jiaju Zhuang */ -@Ignore + public class TempLargeDataTest { private static final Logger LOGGER = LoggerFactory.getLogger(LargeDataTest.class); @@ -42,7 +41,7 @@ public class TempLargeDataTest { private static File fileWriteTemp07; private static File fileWritePoi07; - @BeforeClass + @BeforeAll public static void init() { fileFill07 = TestFileUtil.createNewFile("largefill07.xlsx"); fileWrite07 = TestFileUtil.createNewFile("large" + File.separator + "fileWrite07.xlsx"); @@ -124,7 +123,7 @@ public class TempLargeDataTest { long costPoi = System.currentTimeMillis() - start; LOGGER.info("poi write cost:{}", System.currentTimeMillis() - start); LOGGER.info("{} vs {}", cost, costPoi); - Assert.assertTrue(costPoi * 2 > cost); + Assertions.assertTrue(costPoi * 2 > cost); } @Test @@ -167,9 +166,9 @@ public class TempLargeDataTest { for (int j = 0; j < 25; j++) { String str = "str-" + j + "-" + i; //if (i + 10000 == j) { - SXSSFCell cell = row.createCell(j); - cell.setCellValue(str); - //System.out.println(str); + SXSSFCell cell = row.createCell(j); + cell.setCellValue(str); + //System.out.println(str); //} } if (i % 5000 == 0) { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/Poi2Test.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/Poi2Test.java index 6e1933ab..a2567e11 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/Poi2Test.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/Poi2Test.java @@ -8,8 +8,7 @@ import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,7 +17,7 @@ import org.slf4j.LoggerFactory; * * @author Jiaju Zhuang **/ -@Ignore + public class Poi2Test { private static final Logger LOGGER = LoggerFactory.getLogger(Poi2Test.class); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/Poi3Test.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/Poi3Test.java index 4eb7714c..b9cac73f 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/Poi3Test.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/Poi3Test.java @@ -4,6 +4,8 @@ import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; +import com.alibaba.easyexcel.test.util.TestFileUtil; + import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.opc.OPCPackage; @@ -12,19 +14,16 @@ import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.crypt.EncryptionMode; import org.apache.poi.poifs.crypt.Encryptor; import org.apache.poi.poifs.filesystem.POIFSFileSystem; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.alibaba.easyexcel.test.util.TestFileUtil; - /** * 测试poi * * @author Jiaju Zhuang **/ -@Ignore + public class Poi3Test { private static final Logger LOGGER = LoggerFactory.getLogger(Poi3Test.class); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiEncryptTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiEncryptTest.java index 67b6cfc7..5484a614 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiEncryptTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiEncryptTest.java @@ -17,28 +17,24 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * TODO * * @author Jiaju Zhuang */ -@Ignore + public class PoiEncryptTest { @Test public void encrypt() throws Exception { - - XSSFWorkbook workbook = new XSSFWorkbook(); SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(workbook); Sheet sheet = sxssfWorkbook.createSheet("sheet1"); sheet.createRow(0).createCell(0).setCellValue("T2"); - POIFSFileSystem fs = new POIFSFileSystem(); EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile); @@ -62,7 +58,7 @@ public class PoiEncryptTest { @Test public void encryptExcel() throws Exception { EasyExcel.write(TestFileUtil.createNewFile("encryptv2" + System.currentTimeMillis() + ".xlsx"), - EncryptData.class).password("123456") + EncryptData.class).password("123456") .sheet().doWrite(data()); } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiFormatTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiFormatTest.java index b20004a9..4dfef5b1 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiFormatTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiFormatTest.java @@ -11,8 +11,7 @@ import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,7 +20,7 @@ import org.slf4j.LoggerFactory; * * @author Jiaju Zhuang **/ -@Ignore + public class PoiFormatTest { private static final Logger LOGGER = LoggerFactory.getLogger(PoiFormatTest.class); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java index e27136d4..6c3f229a 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java @@ -28,8 +28,7 @@ import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,7 +37,7 @@ import org.slf4j.LoggerFactory; * * @author Jiaju Zhuang **/ -@Ignore + public class PoiTest { private static final Logger LOGGER = LoggerFactory.getLogger(PoiTest.class); @@ -84,7 +83,6 @@ public class PoiTest { LOGGER.info("bbb:{}", cellStyle1.getFont().getBold()); LOGGER.info("bbb:{}", cellStyle1.getFont().getFontName()); - XSSFFont xssfFont = xssfWorkbook.createFont(); xssfFont.setColor(myColor); @@ -94,7 +92,6 @@ public class PoiTest { cellStyle1.setFont(xssfFont); cellStyle1.setFillForegroundColor(IndexedColors.PINK.getIndex()); - LOGGER.info("aaa:{}", cellStyle1.getFont().getColor()); row.getCell(1).setCellStyle(cellStyle1); @@ -118,7 +115,6 @@ public class PoiTest { xssfWorkbook.close(); } - @Test public void lastRowNumXSSFv22() throws IOException { @@ -142,7 +138,6 @@ public class PoiTest { LOGGER.info("bbb:{}", cellStyle1.getFont(xssfWorkbook).getColor()); - HSSFFont xssfFont = xssfWorkbook.createFont(); xssfFont.setColor(cellStyle1.getFont(xssfWorkbook).getColor()); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java index 7ce3f587..98e7509a 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java @@ -14,8 +14,7 @@ import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -24,7 +23,7 @@ import org.slf4j.LoggerFactory; * * @author Jiaju Zhuang **/ -@Ignore + public class PoiWriteTest { private static final Logger LOGGER = LoggerFactory.getLogger(PoiWriteTest.class); @@ -110,11 +109,12 @@ public class PoiWriteTest { @Test public void part4() throws IOException { //URL url=new URL("http://120.55.161.4/group1/M00/00/00/i8QJ8WFfwMiAXKYrAAACqC1MFiY641.png"); - URL url=new URL("https://img-blog.csdn.net/20160729002743309?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"); + URL url = new URL( + "https://img-blog.csdn.net/20160729002743309?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T" + + "/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"); InputStream in = new BufferedInputStream(url.openStream()); - } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java index 780b1ce4..b05fd540 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java @@ -4,21 +4,20 @@ import java.io.File; import java.util.List; import java.util.Map; -import org.junit.Ignore; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.excel.EasyExcel; import com.alibaba.excel.metadata.data.CellData; import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * 临时测试 * * @author Jiaju Zhuang **/ -@Ignore + public class CommentTest { private static final Logger LOGGER = LoggerFactory.getLogger(CommentTest.class); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HDListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HDListener.java index 7ee174d9..094c58f5 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HDListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HDListener.java @@ -2,13 +2,13 @@ package com.alibaba.easyexcel.test.temp.read; 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.fastjson2.JSON; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * 模板的读取类 * diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java index 711e5b9b..4d1eb8ba 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java @@ -2,13 +2,13 @@ package com.alibaba.easyexcel.test.temp.read; 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.fastjson2.JSON; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * 模板的读取类 * diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadData.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadData.java index 90a721a2..a942763f 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadData.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadData.java @@ -15,7 +15,7 @@ import lombok.Setter; @Setter @EqualsAndHashCode public class HeadReadData { - @ExcelProperty({"主标题","数据1"}) + @ExcelProperty({"主标题", "数据1"}) private String h1; @ExcelProperty({"主标题", "数据2"}) private String h2; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadTest.java index 6649c2fe..ab004258 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadTest.java @@ -6,8 +6,7 @@ import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.cache.Ehcache; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,7 +15,7 @@ import org.slf4j.LoggerFactory; * * @author Jiaju Zhuang **/ -@Ignore + public class HeadReadTest { private static final Logger LOGGER = LoggerFactory.getLogger(HeadReadTest.class); @@ -38,5 +37,4 @@ public class HeadReadTest { EasyExcel.read(file, HeadReadData.class, new HDListener()).readCache(new Ehcache(20)).sheet(0).doRead(); } - } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java index fcb75f11..4e1f40c5 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java @@ -6,27 +6,27 @@ import java.io.InputStream; import java.net.URL; import java.util.List; -import org.junit.Ignore; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.excel.EasyExcel; import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * 测试poi * * @author Jiaju Zhuang **/ -@Ignore + public class HgTest { private static final Logger LOGGER = LoggerFactory.getLogger(HgTest.class); @Test public void hh() throws IOException { List list = - EasyExcel.read(new FileInputStream("D:\\test\\201909301017rule.xlsx")).headRowNumber(2).sheet().doReadSync(); + EasyExcel.read(new FileInputStream("D:\\test\\201909301017rule.xlsx")).headRowNumber(2).sheet() + .doReadSync(); for (Object data : list) { LOGGER.info("返回数据:{}", JSON.toJSONString(data)); } @@ -34,7 +34,11 @@ public class HgTest { @Test public void hh5() throws IOException { - URL url = new URL("http://hotelcontractfil.oss-cn-beijing.aliyuncs.com/2019/%E5%98%89%E6%83%A0-%E4%B8%AD%E4%BA%A4%E5%BB%BA_2019-09-01_2019-09-30_1569055677522.xlsx?Expires=1884415681&OSSAccessKeyId=LTAIGZDkqZfPArBr&Signature=Rf0gbO8vl3l%2Brj1KdyzHHMsUhCE%3D"); + URL url = new URL( + "http://hotelcontractfil.oss-cn-beijing.aliyuncs" + + ".com/2019/%E5%98%89%E6%83%A0-%E4%B8%AD%E4%BA%A4%E5%BB%BA_2019-09-01_2019-09-30_1569055677522" + + ".xlsx?Expires=1884415681&OSSAccessKeyId=LTAIGZDkqZfPArBr&Signature=Rf0gbO8vl3l%2Brj1KdyzHHMsUhCE" + + "%3D"); InputStream is = url.openStream(); List list = EasyExcel.read(is).headRowNumber(0).sheet().doReadSync(); @@ -45,7 +49,8 @@ public class HgTest { @Test public void hh2() throws IOException { - EasyExcel.read(new FileInputStream("D:\\test\\商户不匹配工单信息收集表格.xlsx")).registerReadListener(new HgListener()) + EasyExcel.read(new FileInputStream("D:\\test\\商户不匹配工单信息收集表格.xlsx")).registerReadListener( + new HgListener()) .headRowNumber(0).sheet().doRead(); } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatListener.java index eab1e1ae..6755c94d 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatListener.java @@ -3,15 +3,15 @@ package com.alibaba.easyexcel.test.temp.simple; import java.util.ArrayList; import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.easyexcel.test.demo.read.DemoDataListener; import com.alibaba.easyexcel.test.temp.LockData; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson2.JSON; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * 模板的读取类 * diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatTest.java index d7cf0080..e1e669e4 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatTest.java @@ -3,22 +3,21 @@ package com.alibaba.easyexcel.test.temp.simple; import java.io.FileInputStream; import java.io.IOException; -import org.junit.Ignore; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.easyexcel.test.temp.LockData; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelReader; import com.alibaba.excel.read.metadata.ReadSheet; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * 测试poi * * @author Jiaju Zhuang **/ -@Ignore + public class RepeatTest { private static final Logger LOGGER = LoggerFactory.getLogger(RepeatTest.class); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/Write.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/Write.java index d9b59a52..760a638e 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/Write.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/Write.java @@ -16,8 +16,7 @@ import com.alibaba.excel.write.metadata.WriteTable; import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,7 +25,7 @@ import org.slf4j.LoggerFactory; * * @author Jiaju Zhuang **/ -@Ignore + @Slf4j public class Write { private static final Logger LOGGER = LoggerFactory.getLogger(Write.class); @@ -68,8 +67,9 @@ public class Write { String fileName = TestFileUtil.getPath() + "t33" + System.currentTimeMillis() + ".xlsx"; // 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 如果这里想使用03 则 传入excelType参数即可 - EasyExcel.write(fileName).head(head()).inMemory(true).sheet("模板").registerWriteHandler(new WriteCellHandler()).doWrite( - data1()); + EasyExcel.write(fileName).head(head()).inMemory(true).sheet("模板").registerWriteHandler(new WriteCellHandler()) + .doWrite( + data1()); } @Test diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java index d9a035ce..f4ced8cb 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java @@ -15,8 +15,8 @@ import lombok.Setter; @Setter @EqualsAndHashCode public class WriteData { -// @ContentStyle(locked = true) + // @ContentStyle(locked = true) private Date dd; -// @ContentStyle(locked = false) + // @ContentStyle(locked = false) private float f1; } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java index a8603f12..8c75deab 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java @@ -1,34 +1,20 @@ package com.alibaba.easyexcel.test.temp.write; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.InputStream; -import java.net.URL; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; import com.alibaba.easyexcel.test.demo.read.CustomStringStringConverter; -import com.alibaba.easyexcel.test.demo.write.DemoData; -import com.alibaba.easyexcel.test.demo.write.ImageDemoData; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; -import com.alibaba.excel.ExcelWriter; -import com.alibaba.excel.enums.CellDataTypeEnum; -import com.alibaba.excel.metadata.data.ImageData; -import com.alibaba.excel.metadata.data.ImageData.ImageType; -import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.util.BeanMapUtils; import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.util.ListUtils; -import com.alibaba.excel.write.metadata.WriteSheet; import lombok.extern.slf4j.Slf4j; import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.CreationHelper; -import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Picture; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.streaming.SXSSFCell; @@ -36,17 +22,10 @@ import org.apache.poi.xssf.streaming.SXSSFDrawing; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; -import org.apache.poi.xssf.usermodel.XSSFCellStyle; -import org.apache.poi.xssf.usermodel.XSSFColor; -import org.apache.poi.xssf.usermodel.XSSFFont; -import org.apache.poi.xssf.usermodel.XSSFRow; -import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.cglib.beans.BeanMap; -@Ignore @Slf4j public class TempWriteTest { @@ -166,7 +145,8 @@ public class TempWriteTest { public void large() throws Exception { String file = "/Users/zhuangjiaju/test/imagetest" + System.currentTimeMillis() + ".xlsx"; SXSSFWorkbook workbook = new SXSSFWorkbook(new XSSFWorkbook( - new File("/Users/zhuangjiaju/IdeaProjects/easyexcel/easyexcel-test/src/test/resources/large/large07.xlsx"))); + new File( + "/Users/zhuangjiaju/IdeaProjects/easyexcel/easyexcel-test/src/test/resources/large/large07.xlsx"))); SXSSFSheet sheet = workbook.createSheet("测试"); SXSSFRow row = sheet.createRow(500000); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java index d4665b44..72c98397 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java @@ -2,9 +2,12 @@ package com.alibaba.easyexcel.test.util; import java.io.File; import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; -public class TestFileUtil { +import org.apache.commons.collections4.CollectionUtils; +public class TestFileUtil { public static InputStream getResourcesFileInputStream(String fileName) { return Thread.currentThread().getContextClassLoader().getResourceAsStream("" + fileName); @@ -14,6 +17,10 @@ public class TestFileUtil { return TestFileUtil.class.getResource("/").getPath(); } + public static TestPathBuild pathBuild() { + return new TestPathBuild(); + } + public static File createNewFile(String pathName) { File file = new File(getPath() + pathName); if (file.exists()) { @@ -33,4 +40,37 @@ public class TestFileUtil { public static File readUserHomeFile(String pathName) { return new File(System.getProperty("user.home") + File.separator + pathName); } + + /** + * build to test file path + **/ + public static class TestPathBuild { + private TestPathBuild() { + subPath = new ArrayList<>(); + } + + private final List subPath; + + public TestPathBuild sub(String dirOrFile) { + subPath.add(dirOrFile); + return this; + } + + public String getPath() { + if (CollectionUtils.isEmpty(subPath)) { + return TestFileUtil.class.getResource("/").getPath(); + } + if (subPath.size() == 1) { + return TestFileUtil.class.getResource("/").getPath() + subPath.get(0); + } + StringBuilder path = new StringBuilder(TestFileUtil.class.getResource("/").getPath()); + path.append(subPath.get(0)); + for (int i = 1; i < subPath.size(); i++) { + path.append(File.separator).append(subPath.get(i)); + } + return path.toString(); + } + + } + } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/util/TestUtil.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/util/TestUtil.java index 8246f5b9..02077f3a 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/util/TestUtil.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/util/TestUtil.java @@ -1,7 +1,5 @@ package com.alibaba.easyexcel.test.util; -import java.io.File; -import java.io.InputStream; import java.text.ParseException; import java.time.LocalDate; import java.time.LocalDateTime; @@ -10,7 +8,6 @@ import java.util.Date; import com.alibaba.excel.util.DateUtils; import lombok.extern.slf4j.Slf4j; -import org.junit.Assert; /** * test util diff --git a/easyexcel-test/src/test/resources/bom/no_bom.csv b/easyexcel-test/src/test/resources/bom/no_bom.csv new file mode 100644 index 00000000..461c1eb0 --- /dev/null +++ b/easyexcel-test/src/test/resources/bom/no_bom.csv @@ -0,0 +1,11 @@ +姓名,年纪 +姓名0,20 +姓名1,20 +姓名2,20 +姓名3,20 +姓名4,20 +姓名5,20 +姓名6,20 +姓名7,20 +姓名8,20 +姓名9,20 \ No newline at end of file diff --git a/easyexcel-test/src/test/resources/bom/office_bom.csv b/easyexcel-test/src/test/resources/bom/office_bom.csv new file mode 100644 index 00000000..b6a80992 --- /dev/null +++ b/easyexcel-test/src/test/resources/bom/office_bom.csv @@ -0,0 +1,11 @@ +姓名,年纪 +姓名0,20 +姓名1,20 +姓名2,20 +姓名3,20 +姓名4,20 +姓名5,20 +姓名6,20 +姓名7,20 +姓名8,20 +姓名9,20 \ No newline at end of file diff --git a/easyexcel-test/src/test/resources/compatibility/t09.xlsx b/easyexcel-test/src/test/resources/compatibility/t09.xlsx new file mode 100644 index 00000000..0b29141d Binary files /dev/null and b/easyexcel-test/src/test/resources/compatibility/t09.xlsx differ diff --git a/easyexcel-test/src/test/resources/logback.xml b/easyexcel-test/src/test/resources/logback.xml index 602049a9..74ebe753 100644 --- a/easyexcel-test/src/test/resources/logback.xml +++ b/easyexcel-test/src/test/resources/logback.xml @@ -1,13 +1,15 @@ - - - - ${LOG_PATTERN} + + + + + ${CONSOLE_LOG_PATTERN} + utf8 + - + diff --git a/pom.xml b/pom.xml index 917ac555..62bc605b 100644 --- a/pom.xml +++ b/pom.xml @@ -127,6 +127,12 @@ org.projectlombok lombok + 1.18.20 + + + commons-io + commons-io + 2.11.0 1.18.26 @@ -245,6 +251,7 @@ com/alibaba/excel/metadata/format/ExcelGeneralNumberFormat.java com/alibaba/excel/metadata/csv/CsvDataFormat.java com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java + com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java diff --git a/style/codestyle/idea/codestyle.xml b/style/codestyle/idea/codestyle.xml index 98159561..5ee5b77b 100644 --- a/style/codestyle/idea/codestyle.xml +++ b/style/codestyle/idea/codestyle.xml @@ -1,27 +1,91 @@ - + \ No newline at end of file diff --git a/update.md b/update.md index 0bbdef26..029b7a5c 100644 --- a/update.md +++ b/update.md @@ -5,6 +5,29 @@ * `slf4j-api`由`1.7.32`升级到`1.7.36` * `ehcache`由`3.9.9`升级到`3.10.8` +# 3.3.2 + +* 修复`includeColumnIndexes`和`includeColumnFieldNames`在sheet后面失效的问题 + +# 3.3.1 + +* 修改版本发布问题 + +# 3.3.0 + +* 读csv会忽略BOM数据 [Issue #3137](https://github.com/alibaba/easyexcel/issues/3137) +* 解决csv用office打开乱码的问题,写csv默认带上BOM数据 +* xlsx存在隐藏字符时需要忽略,确保和展示看到的一样 +* 新增`commons-io` 2.11.0 包 +* 在`easyexcel-parent` 包中移除测试包的`dependencyManagement` +* 删除`org.apache.poi.hssf.usermodel.PoiUtils.java`, + 使用反射获取 [Issue #2804](https://github.com/alibaba/easyexcel/issues/2804) +* 默认对象反射缓存改成`ThreadLocal` + ,并支持设置反射缓存类型 [Issue #2792](https://github.com/alibaba/easyexcel/issues/2792) +* 支持根据`includeColumnIndexes`和`includeColumnFieldNames` + 排序 [Issue #2697](https://github.com/alibaba/easyexcel/issues/2697) +* 根据文件流解析,由抛出异常改为,默认识别为csv + # 3.2.1 * 兼容`LocalDate` [Issue #2908](https://github.com/alibaba/easyexcel/issues/2908)