This commit is contained in:
Looly 2022-09-24 00:52:31 +08:00
parent 4eaca121f3
commit 782037c4de
5 changed files with 29 additions and 23 deletions

View File

@ -325,15 +325,15 @@ public class EnumUtil {
}
/**
* 判断某个值是存在枚举中
* 判断指定名称的枚举是否存在
*
* @param <E> 枚举类型
* @param enumClass 枚举类
* @param val 需要查找的值
* @param name 需要查找的枚举名
* @return 是否存在
*/
public static <E extends Enum<E>> boolean contains(final Class<E> enumClass, final String val) {
return EnumUtil.getEnumMap(enumClass).containsKey(val);
public static <E extends Enum<E>> boolean contains(final Class<E> enumClass, final String name) {
return getEnumMap(enumClass).containsKey(name);
}
/**

View File

@ -19,23 +19,31 @@ public class ExcelDateUtil {
*/
private static final int[] customFormats = new int[]{28, 30, 31, 32, 33, 55, 56, 57, 58};
public static boolean isDateFormat(final Cell cell){
/**
* 是否日期格式
*
* @param cell 单元格
* @return 是否日期格式
*/
public static boolean isDateFormat(final Cell cell) {
return isDateFormat(cell, null);
}
/**
* 判断是否日期格式
* @param cell 单元格
*
* @param cell 单元格
* @param cfEvaluator {@link ConditionalFormattingEvaluator}
* @return 是否日期格式
*/
public static boolean isDateFormat(final Cell cell, final ConditionalFormattingEvaluator cfEvaluator){
public static boolean isDateFormat(final Cell cell, final ConditionalFormattingEvaluator cfEvaluator) {
final ExcelNumberFormat nf = ExcelNumberFormat.from(cell, cfEvaluator);
return isDateFormat(nf);
}
/**
* 判断是否日期格式
*
* @param numFmt {@link ExcelNumberFormat}
* @return 是否日期格式
*/

View File

@ -1,6 +1,5 @@
package cn.hutool.poi.excel.cell.values;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.CharUtil;
import cn.hutool.poi.excel.ExcelDateUtil;
import cn.hutool.poi.excel.cell.CellValue;
@ -8,7 +7,7 @@ import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.util.NumberToTextConverter;
import java.util.Date;
import java.time.LocalDateTime;
/**
* 数字类型单元格值<br>
@ -38,13 +37,12 @@ public class NumericCellValue implements CellValue<Object> {
if (null != style) {
// 判断是否为日期
if (ExcelDateUtil.isDateFormat(cell)) {
final LocalDateTime date = cell.getLocalDateTimeCellValue();
// 1899年写入会导致数据错乱读取到1899年证明这个单元格的信息不关注年月日
final Date dateCellValue = cell.getDateCellValue();
if ("1899".equals(DateUtil.format(dateCellValue, "yyyy"))) {
return DateUtil.format(dateCellValue, style.getDataFormatString());
if(1899 == date.getYear()){
return date.toLocalTime();
}
// 使用Hutool的DateTime包装
return DateUtil.date(dateCellValue);
return date;
}
final String format = style.getDataFormatString();

View File

@ -1,18 +1,18 @@
package cn.hutool.poi.excel;
import cn.hutool.core.lang.Console;
import org.junit.Ignore;
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
public class IssueI5Q1TWTest {
@Test
@Ignore
public void readTest(){
final ExcelReader reader = ExcelUtil.getReader("d:/test/I5Q1TW.xlsx");
final List<List<Object>> read = reader.read();
Console.log(reader.readCellValue(0, 0));
public void readTest() {
final ExcelReader reader = ExcelUtil.getReader("I5Q1TW.xlsx");
// 自定义时间格式1
Assert.assertEquals("18:56", reader.readCellValue(0, 0).toString());
// 自定义时间格式2
Assert.assertEquals("18:56", reader.readCellValue(1, 0).toString());
}
}

Binary file not shown.