forked from rongchao/epmet-cloud-rizhao
3 changed files with 84 additions and 26 deletions
@ -0,0 +1,59 @@ |
|||||
|
package com.epmet.excel.converter; |
||||
|
|
||||
|
import com.alibaba.excel.converters.Converter; |
||||
|
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
|
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
|
import com.alibaba.excel.metadata.data.ReadCellData; |
||||
|
import com.alibaba.excel.metadata.data.WriteCellData; |
||||
|
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
|
|
||||
|
import java.text.SimpleDateFormat; |
||||
|
import java.time.LocalDateTime; |
||||
|
import java.time.ZoneId; |
||||
|
import java.time.temporal.ChronoUnit; |
||||
|
import java.util.Date; |
||||
|
|
||||
|
/** |
||||
|
* 框架:easyexcel |
||||
|
* 目的:工作日志(服务),对服务时间日期类型的转换 |
||||
|
*/ |
||||
|
public class WorkDiaryServiceEasyExcelDateConverter implements Converter<Date> { |
||||
|
|
||||
|
private static final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd"; |
||||
|
|
||||
|
/** |
||||
|
* excel内容转化为java时间 |
||||
|
* @param cellData |
||||
|
* @param contentProperty |
||||
|
* @param globalConfiguration |
||||
|
* @return |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public Date convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { |
||||
|
if (cellData.getType() == CellDataTypeEnum.NUMBER) { |
||||
|
// 数字类型,是1900-01-03到excel中数据日期的天数差值
|
||||
|
LocalDateTime localDate = LocalDateTime.of(1900, 1, 1, 0, 0, 0) |
||||
|
// 不知为何,这个日期差值,多了2天,需要减去
|
||||
|
.minus(2L, ChronoUnit.DAYS) |
||||
|
.plus(cellData.getNumberValue().longValue(), ChronoUnit.DAYS); |
||||
|
return Date.from(localDate.atZone(ZoneId.systemDefault()).toInstant()); |
||||
|
} else { |
||||
|
// 字符串,正常格式化
|
||||
|
Date dateData = new SimpleDateFormat(PATTERN_YYYY_MM_DD).parse(cellData.getStringValue()); |
||||
|
return dateData; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* java日期转为excel内容 |
||||
|
* @param value |
||||
|
* @param contentProperty |
||||
|
* @param globalConfiguration |
||||
|
* @return |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public WriteCellData<?> convertToExcelData(Date value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { |
||||
|
String dataStr = new SimpleDateFormat(PATTERN_YYYY_MM_DD).format(value); |
||||
|
return new WriteCellData(dataStr); |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue