欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > EasyExcel导入导出数据类型转换

EasyExcel导入导出数据类型转换

2024/11/30 14:50:21 来源:https://blog.csdn.net/qq_41481924/article/details/140522344  浏览:    关键词:EasyExcel导入导出数据类型转换

前言:

1、基本数据类型转换:当前原始的数据类型是interger类型,需要在导出时将其映射为对应的字符串,并且导入时可以将字符串重新映射为interger类型。

2、时间格式转换:数据从数据库中获取的类型为LocalDate类型,即yyyy-mm-dd,导出时需要将其格式化为yyyy-mm类型,导入时可以将yyyy-mm串重新映射为yyyy-mm-dd。

1、基本数据类型转换

实体类

    /** 产品类型  0-电芯 1-pack */@ExcelProperty(value = "产品类型",index = 2, converter = ProductTypeConverter.class)@ColumnWidth(15)private Integer productType;

配置数据类型转换器

import com.alibaba.excel.converters.Converter;
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;public class ProductTypeConverter implements Converter<Integer> {@Overridepublic WriteCellData<Integer> convertToExcelData(Integer i, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {if (i == 0) {return new WriteCellData<>("电芯");} else if (i == 1) {return new WriteCellData<>("PACK");}return new WriteCellData<>(" ");}@Overridepublic Integer convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {if("电芯".equals(cellData.getStringValue())){return 0;} else if("PACK".equals(cellData.getStringValue())){return 1;}else {return -1;}}}

2、时间格式转换

实体类

    /** 统计时间 */@ExcelProperty(value = "统计时间",index = 7,converter = DateConverter.class)@JsonFormat(pattern = "yyyy-MM-dd")@DateTimeFormat(pattern = "yyyy-MM-dd")@ColumnWidth(20)private LocalDate statisticsTime;

配置的时间格式转换器

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
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;import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Date;public class DateConverter implements Converter<LocalDate> {private static final String PATTERN_YYYY_MM = "yyyy-MM";@Overridepublic Class<LocalDate> supportJavaTypeKey() {return LocalDate.class;}@Overridepublic WriteCellData<String> convertToExcelData(WriteConverterContext<LocalDate> context)  {LocalDate localDate = context.getValue();if (localDate == null) {return null;}Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_YYYY_MM);return new WriteCellData<>(sdf.format(date));}@Overridepublic LocalDate convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {if(null==cellData) {return null;}LocalDate result=null;if(cellData.getType()== CellDataTypeEnum.NUMBER) {if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {Date date= DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),globalConfiguration.getUse1904windowing(), null);result =date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();} else {Date date=  DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null);result =date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();}}if(cellData.getType()==CellDataTypeEnum.STRING) {String value=cellData.getStringValue();if(value.contains("-")) {DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd").parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
//                        .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
//                        .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
//                        .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0).toFormatter();result = LocalDate.parse(value+"-01", formatter);}else if(value.contains("/")) {DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("yyyy/MM/dd").parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
//                        .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
//                        .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
//                        .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0).toFormatter();result = LocalDate.parse(value+"-01", formatter);}}return result;}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com