一、使用场景
业务定义类里边,定义变量isIncludeThesis表示是否包含论文,1是,0否。导出的时候需要 1->是,0->否, 导入时需要是->1,否->0。
//是否包含论文 private Integer isIncludeThesis;
二、如何定义
泛型为类的属性的类型
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.GlobalConfiguration; import com.alibaba.excel.metadata.property.ExcelContentProperty; import java.util.Objects; public class IsConverter implements Converter<Integer> { //写入数据时,将excel单元格数据转化为java @Override public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { String data = (String)cellData.getData(); if (data!=null) { if (Objects.equals(data,"是")) { return 1; }else if(Objects.equals(data,"否")){ return 0; } } return null; } //导出数据到excel @Override public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { return new CellData(value==1?"是":"否"); } @Override public Class<?> supportJavaTypeKey() { return String.class; } @Override public CellDataTypeEnum supportExcelTypeKey() { return CellDataTypeEnum.STRING; } }
三、如何使用
@ExcelProperty(value = "是否包含论文", converter = IsConverter.class) private Integer isIncludeThesis;