Java使用FastExcel導(dǎo)入支持多種時(shí)間格式
簡(jiǎn)介
FastExcel 是一個(gè)采用純 java 開發(fā)的 excel 文件讀寫組件。支持 Excel'97(-2003)(BIFF8)文件格式。FastExcel 主要關(guān)注 excel 內(nèi)容的處理,所以 FastExcel 只能讀取單元格的字符 信息,而其它屬性如顏色,字體等就不支持了。由于不讀取,解析和存儲(chǔ)這些額外信息,因此 FastExcel 只需很小的內(nèi)存。
示例代碼:
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 ,導(dǎo)入支持多種時(shí)間格式
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; /** * 日期格式轉(zhuǎn)換器 */ 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. 第一種解析,傳入的是數(shù)字形式的日期,形如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年開始計(jì)算,最終通過計(jì)算與1900年間隔的天數(shù)計(jì)算目標(biāo)日期 LocalDate localDate = LocalDate.of(1900, 1, 1); //excel 有些奇怪的bug, 導(dǎo)致日期數(shù)差2 localDate = localDate.plusDays(cellIntValue - 2); // 轉(zhuǎn)換為ZonedDateTime(如果需要時(shí)區(qū)信息) 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) { // 如果有異常,捕捉異常后繼續(xù)解析 log.error(e.getMessage(), e); } } } // 如果有異常,捕捉異常后繼續(xù)解析 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); } }
到此這篇關(guān)于Java使用FastExcel導(dǎo)入支持多種時(shí)間格式的文章就介紹到這了,更多相關(guān)Java FastExcel時(shí)間格式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Gradle項(xiàng)目中的資源正確獲取方式
這篇文章主要介紹了Java Gradle項(xiàng)目中的資源正確獲取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11SpringBoot在項(xiàng)目停止(服務(wù)停止/關(guān)閉退出)之后執(zhí)行的方法
這篇文章主要給大家介紹了SpringBoot在項(xiàng)目停止(服務(wù)停止/關(guān)閉退出)之后執(zhí)行的兩種方法,實(shí)現(xiàn)DisposableBean接口和使用@PreDestroy注解,文中有詳細(xì)的代碼講解,具有一定的參考價(jià)值,需要的朋友可以參考下2023-12-12詳解Spring Cloud Feign 熔斷配置的一些小坑
這篇文章主要介紹了詳解Spring Cloud Feign 熔斷配置的一些小坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04SpringBoot多數(shù)據(jù)源配置的全過程記錄
在用SpringBoot開發(fā)項(xiàng)目時(shí),隨著業(yè)務(wù)量的擴(kuò)大,我們通常會(huì)進(jìn)行數(shù)據(jù)庫(kù)拆分或是引入其他數(shù)據(jù)庫(kù),從而我們需要配置多個(gè)數(shù)據(jù)源,下面這篇文章主要給大家介紹了關(guān)于SpringBoot多數(shù)據(jù)源配置的相關(guān)資料,需要的朋友可以參考下2021-11-11Java實(shí)現(xiàn)寵物商店管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)寵物商店管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10skywalking源碼解析javaAgent工具ByteBuddy應(yīng)用
這篇文章主要為大家介紹了skywalking源碼解析javaAgent工具ByteBuddy應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03