forked from fanruan/easyexcel
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
2.0 KiB
55 lines
2.0 KiB
package com.alibaba.excel.converters.date; |
|
|
|
import java.math.BigDecimal; |
|
import java.util.Date; |
|
|
|
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 org.apache.poi.ss.usermodel.DateUtil; |
|
|
|
/** |
|
* Date and number converter |
|
* |
|
* @author Jiaju Zhuang |
|
*/ |
|
public class DateNumberConverter implements Converter<Date> { |
|
|
|
@Override |
|
public Class<?> supportJavaTypeKey() { |
|
return Date.class; |
|
} |
|
|
|
@Override |
|
public CellDataTypeEnum supportExcelTypeKey() { |
|
return CellDataTypeEnum.NUMBER; |
|
} |
|
|
|
@Override |
|
public Date convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, |
|
GlobalConfiguration globalConfiguration) { |
|
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { |
|
return DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(), |
|
globalConfiguration.getUse1904windowing(), null); |
|
} else { |
|
return DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(), |
|
contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null); |
|
} |
|
} |
|
|
|
@Override |
|
public WriteCellData<?> convertToExcelData(Date value, ExcelContentProperty contentProperty, |
|
GlobalConfiguration globalConfiguration) { |
|
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { |
|
return new WriteCellData<>( |
|
BigDecimal.valueOf(DateUtil.getExcelDate(value, globalConfiguration.getUse1904windowing()))); |
|
} else { |
|
return new WriteCellData<>(BigDecimal.valueOf( |
|
DateUtil.getExcelDate(value, contentProperty.getDateTimeFormatProperty().getUse1904windowing()))); |
|
} |
|
} |
|
}
|
|
|