From ae9f3861c485675bda8e7fc965f543db316e1fd1 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Tue, 6 Aug 2024 17:32:04 +0800 Subject: [PATCH] =?UTF-8?q?*=20=E5=85=BC=E5=AE=B9=E6=9F=90=E4=BA=9B?= =?UTF-8?q?=E7=89=B9=E6=AE=8A=E7=9A=84xls:=20=E4=BF=AE=E6=94=B9=E4=BA=86?= =?UTF-8?q?=E5=86=85=E7=BD=AE=E7=9A=84=E6=A0=B7=E5=BC=8F=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E6=A0=B7=E5=BC=8F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/constant/BuiltinFormats.java | 127 +++++++++++++++++- .../com/alibaba/excel/util/DateUtils.java | 23 +--- .../easyexcel/test/temp/Lock2Test.java | 3 +- .../test/temp/poi/PoiDateFormatTest.java | 40 ++++++ pom.xml | 2 +- update.md | 6 +- 6 files changed, 174 insertions(+), 27 deletions(-) create mode 100644 easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiDateFormatTest.java diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java b/easyexcel-core/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java index ec7d5c9a..7e071af5 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java @@ -4,6 +4,7 @@ import java.util.Locale; import java.util.Map; import com.alibaba.excel.util.MapUtils; +import com.alibaba.excel.util.StringUtils; /** * Excel's built-in format conversion.Currently only supports Chinese. @@ -22,6 +23,112 @@ public class BuiltinFormats { public static short GENERAL = 0; + public static final String[] BUILTIN_FORMATS_ALL_LANGUAGES = { + // 0 + "General", + // 1 + "0", + // 2 + "0.00", + // 3 + "#,##0", + // 4 + "#,##0.00", + // 5 + "\"¥\"#,##0_);(\"¥\"#,##0)", + // 6 + "\"¥\"#,##0_);[Red](\"¥\"#,##0)", + // 7 + "\"¥\"#,##0.00_);(\"¥\"#,##0.00)", + // 8 + "\"¥\"#,##0.00_);[Red](\"¥\"#,##0.00)", + // 9 + "0%", + // 10 + "0.00%", + // 11 + "0.00E+00", + // 12 + "# ?/?", + // 13 + "# ??/??", + // 14 + // The official documentation shows "m/d/yy", but the actual test is "yyyy/m/d". + "yyyy/m/d", + // 15 + "d-mmm-yy", + // 16 + "d-mmm", + // 17 + "mmm-yy", + // 18 + "h:mm AM/PM", + // 19 + "h:mm:ss AM/PM", + // 20 + "h:mm", + // 21 + "h:mm:ss", + // 22 + // The official documentation shows "m/d/yy h:mm", but the actual test is "yyyy-m-d h:mm". + "yyyy-m-d h:mm", + // 23-36 No specific correspondence found in the official documentation. + // 23 + null, + // 24 + null, + // 25 + null, + // 26 + null, + // 27 + null, + // 28 + null, + // 29 + null, + // 30 + null, + // 31 + null, + // 32 + null, + // 33 + null, + // 34 + null, + // 35 + null, + // 36 + null, + // 37 + "#,##0_);(#,##0)", + // 38 + "#,##0_);[Red](#,##0)", + // 39 + "#,##0.00_);(#,##0.00)", + // 40 + "#,##0.00_);[Red](#,##0.00)", + // 41 + "_(* #,##0_);_(* (#,##0);_(* \"-\"_);_(@_)", + // 42 + "_(\"¥\"* #,##0_);_(\"¥\"* (#,##0);_(\"¥\"* \"-\"_);_(@_)", + // 43 + "_(* #,##0.00_);_(* (#,##0.00);_(* \"-\"??_);_(@_)", + // 44 + "_(\"¥\"* #,##0.00_);_(\"¥\"* (#,##0.00);_(\"¥\"* \"-\"??_);_(@_)", + // 45 + "mm:ss", + // 46 + "[h]:mm:ss", + // 47 + "mm:ss.0", + // 48 + "##0.0E+0", + // 49 + "@", + }; + public static final String[] BUILTIN_FORMATS_CN = { // 0 "General", @@ -371,8 +478,26 @@ public class BuiltinFormats { public static final short MIN_CUSTOM_DATA_FORMAT_INDEX = 82; public static String getBuiltinFormat(Short index, String defaultFormat, Locale locale) { + if (index == null || index <= 0) { + return defaultFormat; + } + + // Give priority to checking if it is the default value for all languages + if (index < BUILTIN_FORMATS_ALL_LANGUAGES.length) { + String format = BUILTIN_FORMATS_ALL_LANGUAGES[index]; + if (format != null) { + return format; + } + } + + // In other cases, give priority to using the externally provided format + if (!StringUtils.isEmpty(defaultFormat) && !defaultFormat.startsWith("reserved-")) { + return defaultFormat; + } + + // Finally, try using the built-in format String[] builtinFormat = switchBuiltinFormats(locale); - if (index == null || index < 0 || index >= builtinFormat.length) { + if (index >= builtinFormat.length) { return defaultFormat; } return builtinFormat[index]; diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java b/easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java index cab7e4d0..3184d087 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java @@ -470,31 +470,10 @@ public class DateUtils { case 0x14: case 0x15: case 0x16: - // 27-36 - case 0x1b: - case 0x1c: - case 0x1d: - case 0x1e: - case 0x1f: - case 0x20: - case 0x21: - case 0x22: - case 0x23: - case 0x24: - // 45-47 + // 45-47 case 0x2d: case 0x2e: case 0x2f: - // 50-58 - case 0x32: - case 0x33: - case 0x34: - case 0x35: - case 0x36: - case 0x37: - case 0x38: - case 0x39: - case 0x3a: return true; } return false; 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 db1ead44..508f85cb 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 @@ -52,8 +52,7 @@ 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/Downloads/证券投资基金估值表_外贸信托-稳盈淳享37号集合资金信托计划_2024-07-23.xls") //.useDefaultListener(false) .sheet(0) .headRowNumber(0).doReadSync(); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiDateFormatTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiDateFormatTest.java new file mode 100644 index 00000000..dbef4a08 --- /dev/null +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiDateFormatTest.java @@ -0,0 +1,40 @@ +package com.alibaba.easyexcel.test.temp.poi; + +import java.io.IOException; + +import org.apache.poi.ss.usermodel.DateUtil; +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.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 测试poi + * + * @author Jiaju Zhuang + **/ + +public class PoiDateFormatTest { + private static final Logger LOGGER = LoggerFactory.getLogger(PoiDateFormatTest.class); + + @Test + public void read() throws IOException { + String file + = "/Users/zhuangjiaju/IdeaProjects/easyexcel/easyexcel-test/src/test/resources/dataformat/dataformat.xlsx"; + XSSFWorkbook xssfWorkbook = new XSSFWorkbook( file); + XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); + LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); + XSSFRow row = xssfSheet.getRow(7); + XSSFCell cell = row.getCell(0); + LOGGER.info("dd{}", cell.getDateCellValue()); + LOGGER.info("dd{}", cell.getNumericCellValue()); + + LOGGER.info("dd{}", DateUtil.isCellDateFormatted(cell)); + + + } + +} diff --git a/pom.xml b/pom.xml index 44dd0796..ca57516c 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ - 4.0.1 + 4.0.2 UTF-8 1.8 true diff --git a/update.md b/update.md index 6b141fff..20295da5 100644 --- a/update.md +++ b/update.md @@ -1,4 +1,8 @@ -# 4.0.1 +# 4.0.2 + +* 兼容某些特殊的xls: 修改了内置的样式导致判断样式错误 + +* # 4.0.1 * `commons-io` 修改为依赖 `poi`的版本 * 修复临时目录被清理可能提示`NoSuchFileException`的异常