mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix bug
This commit is contained in:
@@ -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 是否日期格式
|
||||
*/
|
||||
|
@@ -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();
|
||||
|
@@ -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());
|
||||
}
|
||||
}
|
||||
|
BIN
hutool-poi/src/test/resources/I5Q1TW.xlsx
Normal file
BIN
hutool-poi/src/test/resources/I5Q1TW.xlsx
Normal file
Binary file not shown.
Reference in New Issue
Block a user