当前位置:   article > 正文

EasyExcel导入导出的数据格式转换_java easyexcel导入转化excel是否的中文为01数字

java easyexcel导入转化excel是否的中文为01数字
  1. import com.alibaba.excel.converters.Converter;
  2. import com.alibaba.excel.enums.CellDataTypeEnum;
  3. import com.alibaba.excel.metadata.CellData;
  4. import com.alibaba.excel.metadata.GlobalConfiguration;
  5. import com.alibaba.excel.metadata.property.ExcelContentProperty;
  6. public class LoginStatusConverter implements Converter<String> {
  7. //在java中状态是用 0 1 来标识的 所以是int
  8. @Override
  9. public Class supportJavaTypeKey() {return Integer.class;}
  10. // 在excel中是“登录”“未登录” 所以是string
  11. @Override
  12. public CellDataTypeEnum supportExcelTypeKey() {return CellDataTypeEnum.STRING;}
  13. //将excel的数据类型转为java数据类型
  14. @Override
  15. public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
  16. String stringValue = cellData.getStringValue();
  17. if (stringValue == null) {
  18. throw new RuntimeException("登录状态填写为空");
  19. }
  20. if ("登录".equals(stringValue)) {
  21. return 1;
  22. }
  23. return 0;
  24. }
  25. //将java的数据类型转为excel数据类型
  26. @Override
  27. public CellData convertToExcelData(Integer s, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
  28. if (s == 0){
  29. return new CellData("未登录");
  30. }
  31. return new CellData("登录");
  32. }
  33. }

然后在实体类的字段上标注 converter

  1. @ExcelProperty(value = "登录状态",index=3,converter=LoginStatusConverter.class)
  2. private Integer loginStatus;

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/472726
推荐阅读
相关标签
  

闽ICP备14008679号