excel import export field converter

1. Convert java data to other formats

eg: date format conversion, enumeration type conversion
1, date format conversion, the date format is displayed as yyyy-MM-dd HH:mm:ss in the exported place

package com.onehealthpro.oemr.modules.medicalrecord.utils;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *  easyExcel 日期转换
 */
public class ConvertToExcelDate implements Converter<Date> {


    private static  final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd HH:mm:ss";

    @Override
    public Class<?> supportJavaTypeKey() {
        return Converter.super.supportJavaTypeKey();
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return Converter.super.supportExcelTypeKey();
    }

    @Override
    public WriteCellData<?> convertToExcelData(Date value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_YYYY_MM_DD);
        String dateValue = sdf.format(value);
        return new WriteCellData<>(dateValue);
    }

}

2. Enumeration type conversion, when the java data is 11 , the form displays the call

package com.onehealthpro.oemr.modules.medicalrecord.utils;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 *  easyExcel 日期转换
 */
public class ConvertToExcelFollowWay implements Converter<Integer> {


    @Override
    public Class<?> supportJavaTypeKey() {
        return Converter.super.supportJavaTypeKey();
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return Converter.super.supportExcelTypeKey();
    }

    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String dateValue = null;
        if (Objects.equals(value,11)){
            dateValue = "打电话";
        }
        if (Objects.equals(value,12)){
            dateValue = "微信消息";
        }
        if (Objects.equals(value,13)){
            dateValue = "面对面";
        }
        if (Objects.isNull(dateValue)){
            return null;
        }
        return new WriteCellData<>(dateValue);
    }

}

2. Convert other formats to java data types

package com.onehealthpro.oemr.modules.medicalrecord.utils;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.onehealthpro.common.exception.BizException;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * 物资分类转换器
 */
@Slf4j
public class MaterialClassificationConvert implements Converter<Integer> {

    @Override
    public Integer convertToJavaData(ReadConverterContext<?> context) throws Exception {
        String data = context.getReadCellData().getStringValue();
        if (null == data) {
            throw new BizException("物资分类不能为空");
        };//非空判断
        if (Objects.equals(data,"药品")){
            return 1;
        }else if (Objects.equals(data,"耗材")){
            return 2;
        }else if (Objects.equals(data,"设备") || Objects.equals(data,"其它")){
            return 3;
        }
        return null;
    }
}

Guess you like

Origin blog.csdn.net/qq_44798321/article/details/130823156