easyexcel 自定义格式转换 注入业务bean 为null

fzsnzjdm  于 4个月前  发布在  其他
关注(0)|答案(1)|浏览(41)

package org.jeecg.modules.aq.vo;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;
import org.jeecg.modules.aq.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**

  • String and string converter
  • @author Jiaju Zhuang
    */
    @component
    public class CustomStringStringConverter implements Converter {

@Autowired
private CommonService commonService;

@OverRide
public Class<?> supportJavaTypeKey() {
return String.class;
}

@OverRide
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}

/**

  • 这里是读的时候会调用 不用管
  • @return
    */
    @OverRide
    public String convertToJavaData(ReadConverterContext<?> context) {
    return context.getReadCellData().getStringValue();
    }

/**

  • 这里是写的时候会调用 不用管
  • @return
    */
    @OverRide
    public WriteCellData<?> convertToExcelData(WriteConverterContext context) {
    // Map<String, String> categoryMap = commonService.categoryMap();
    if (/categoryMap.containsKey(context.getValue())/true) {
    // WriteCellData writeCellData = new WriteCellData<>(categoryMap.get(context.getValue()));
    WriteCellData writeCellData = new WriteCellData<>("");
    return writeCellData;
    }
    return new WriteCellData<>("");
    }

}

k7fdbhmy

k7fdbhmy1#

@Override
public WriteCellData<?> convertToExcelData(WriteConverterContext context) {
    if (commonService != null) {
        Map<String, String> categoryMap = commonService.categoryMap();
        if (categoryMap.containsKey(context.getValue())) {
            WriteCellData writeCellData = new WriteCellData<>(categoryMap.get(context.getValue()));
            return writeCellData;
        }
    }
    return new WriteCellData<>("");
}

相关问题