Browse Source

* 兼容某些特殊的xls: 修改了内置的样式导致判断样式错误

pull/3916/head
Jiaju Zhuang 4 months ago
parent
commit
ae9f3861c4
  1. 127
      easyexcel-core/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java
  2. 23
      easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java
  3. 3
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
  4. 40
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiDateFormatTest.java
  5. 2
      pom.xml
  6. 6
      update.md

127
easyexcel-core/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java

@ -4,6 +4,7 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.MapUtils;
import com.alibaba.excel.util.StringUtils;
/** /**
* Excel's built-in format conversion.Currently only supports Chinese. * Excel's built-in format conversion.Currently only supports Chinese.
@ -22,6 +23,112 @@ public class BuiltinFormats {
public static short GENERAL = 0; 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 = { public static final String[] BUILTIN_FORMATS_CN = {
// 0 // 0
"General", "General",
@ -371,8 +478,26 @@ public class BuiltinFormats {
public static final short MIN_CUSTOM_DATA_FORMAT_INDEX = 82; public static final short MIN_CUSTOM_DATA_FORMAT_INDEX = 82;
public static String getBuiltinFormat(Short index, String defaultFormat, Locale locale) { 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); String[] builtinFormat = switchBuiltinFormats(locale);
if (index == null || index < 0 || index >= builtinFormat.length) { if (index >= builtinFormat.length) {
return defaultFormat; return defaultFormat;
} }
return builtinFormat[index]; return builtinFormat[index];

23
easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java

@ -470,31 +470,10 @@ public class DateUtils {
case 0x14: case 0x14:
case 0x15: case 0x15:
case 0x16: case 0x16:
// 27-36 // 45-47
case 0x1b:
case 0x1c:
case 0x1d:
case 0x1e:
case 0x1f:
case 0x20:
case 0x21:
case 0x22:
case 0x23:
case 0x24:
// 45-47
case 0x2d: case 0x2d:
case 0x2e: case 0x2e:
case 0x2f: 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 true;
} }
return false; return false;

3
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"); File file = new File("/Users/zhuangjiaju/IdeaProjects/easyexcel/src/test/resources/converter/converter07.xlsx");
List<Object> list = EasyExcel.read( List<Object> list = EasyExcel.read(
"/Users/zhuangjiaju/IdeaProjects/easyexcel/easyexcel-test/target/test-classes" "/Users/zhuangjiaju/Downloads/证券投资基金估值表_外贸信托-稳盈淳享37号集合资金信托计划_2024-07-23.xls")
+ "/simpleWrite1674051907397.xlsx")
//.useDefaultListener(false) //.useDefaultListener(false)
.sheet(0) .sheet(0)
.headRowNumber(0).doReadSync(); .headRowNumber(0).doReadSync();

40
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));
}
}

2
pom.xml

@ -20,7 +20,7 @@
<properties> <properties>
<revision>4.0.1</revision> <revision>4.0.2</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.8</jdk.version> <jdk.version>1.8</jdk.version>
<gpg.skip>true</gpg.skip> <gpg.skip>true</gpg.skip>

6
update.md

@ -1,4 +1,8 @@
# 4.0.1 # 4.0.2
* 兼容某些特殊的xls: 修改了内置的样式导致判断样式错误
* # 4.0.1
* `commons-io` 修改为依赖 `poi`的版本 * `commons-io` 修改为依赖 `poi`的版本
* 修复临时目录被清理可能提示`NoSuchFileException`的异常 * 修复临时目录被清理可能提示`NoSuchFileException`的异常

Loading…
Cancel
Save