Java使用FastExcel导入支持多种时间格式
作者:VipSoft
FastExcel 是一个采用纯 java 开发的 excel 文件读写组件,支持 Excel'97(-2003)(BIFF8)文件格式,本文主要介绍了如何使用FastExcel导入支持多种时间格式,感兴趣的可以了解
简介
FastExcel 是一个采用纯 java 开发的 excel 文件读写组件。支持 Excel'97(-2003)(BIFF8)文件格式。FastExcel 主要关注 excel 内容的处理,所以 FastExcel 只能读取单元格的字符 信息,而其它属性如颜色,字体等就不支持了。由于不读取,解析和存储这些额外信息,因此 FastExcel 只需很小的内存。
示例代码:
public void testDump() throws ParserException, ReadException {
Workbook workBook;
workBook = FastExcel.createReadableWorkbook(new File("test.xls"));
workBook.open();
Sheet s;
s = workBook.getSheet(0);
System.out.println("SHEET:"+s);
for (int i = s.getFirstRow(); i <= s.getLastRow(); i++) {
System.out.print(i+"#");
for (int j = s.getFirstColumn(); j <=s.getLastColumn(); j++) {
System.out.print(","+s.getCell(i, j));
}
System.out.println();
}
workBook.close();
}EasyExcel => FastExcel ,导入支持多种时间格式
InfoExcelDTO
/** * 合作开始日期* */ @ExcelProperty(index = 22,converter = ExcelDateConverter.class) private Date cooperationDate;
ExcelDateConverter
package com.vipsoft.base.util;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import cn.idev.excel.converters.Converter;
import cn.idev.excel.enums.CellDataTypeEnum;
import cn.idev.excel.metadata.GlobalConfiguration;
import cn.idev.excel.metadata.data.ReadCellData;
import cn.idev.excel.metadata.property.ExcelContentProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 日期格式转换器
*/
public class ExcelDateConverter implements Converter<Date> {
private static final Logger log = LoggerFactory.getLogger(ExcelDateConverter.class);
// 定义所有要尝试的日期格式
SimpleDateFormat[] formats = {
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"),
new SimpleDateFormat("yyyy/MM/dd"),
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("yyyy/MM"),
new SimpleDateFormat("yyyy/MM"),
new SimpleDateFormat("yyyyMMdd")
};
@Override
public Class<Date> supportJavaTypeKey() {
return Date.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public Date convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) throws Exception {
String cellValue = "";
if (cellData.getType().equals(CellDataTypeEnum.NUMBER)) {
long cellIntValue = cellData.getNumberValue().longValue();
if (cellIntValue > 19900100) {
try {
// 1. 第一种解析,传入的是数字形式的日期,形如yyyyMMdd
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");
return originalFormat.parse(String.valueOf(cellIntValue));
} catch (Exception e) {
log.warn("exception when parse numerical time with format yyyyMMdd");
cellValue=String.valueOf(cellIntValue);
}
}
// 2. 第二种解析, excel是从1900年开始计算,最终通过计算与1900年间隔的天数计算目标日期
LocalDate localDate = LocalDate.of(1900, 1, 1);
//excel 有些奇怪的bug, 导致日期数差2
localDate = localDate.plusDays(cellIntValue - 2);
// 转换为ZonedDateTime(如果需要时区信息)
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
return Date.from(zonedDateTime.toInstant());
} else if (cellData.getType().equals(CellDataTypeEnum.STRING)) {
// 3. 第三种解析
Date date = null;
cellValue = cellData.getStringValue();
for (SimpleDateFormat format : formats) {
try {
date = format.parse(cellValue);
if (date != null) {
// 这一步是将日期格式化为Java期望的格式
return date;
}
} catch (Exception e) {
// 如果有异常,捕捉异常后继续解析
log.error(e.getMessage(), e);
}
}
}
// 如果有异常,捕捉异常后继续解析
throw new UnsupportedOperationException("The current operation is not supported by the current converter." + cellValue);
}
@Override
public WriteCellData<?> convertToExcelData(Date value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateValue = sdf.format(value);
return new WriteCellData<>(dateValue);
}
}到此这篇关于Java使用FastExcel导入支持多种时间格式的文章就介绍到这了,更多相关Java FastExcel时间格式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
