EasyExcel自定义Converter解决性别转换问题

您所在的位置:网站首页 excel数字转成汉字 EasyExcel自定义Converter解决性别转换问题

EasyExcel自定义Converter解决性别转换问题

2024-07-18 00:42:43| 来源: 网络整理| 查看: 265

上篇文章已经介绍了自定义LocalDateTime转换器解决了LocalDateTime导入导出的问题!

那么这篇文章继续介绍一下性别转换的问题,一般我们代码中都会使用 1/0 分别代表 男/女 ,可是Excel中都是用"男","女"表示的,怎么做才能 单独将性别转化为1和0 呢?没错还是自定义转换器! 因为这个案例有LocalDateTime,所以请查看上篇文章采取任意一种解决方案,建议采用第二种:

新建User类,然后在类上添加EasyExcel的注解

@Data @NoArgsConstructor @AllArgsConstructor public class User { @ExcelProperty(value = "姓名", index = 0) private String name; @ExcelProperty(value = "年龄", index = 1) private Integer age; // 1 男 0 女 @ExcelProperty(value = "性别", index = 2) private Integer sex; @ExcelProperty(value = "创建时间", index = 3) private LocalDateTime createTime; }

新建Controller用于测试

@Slf4j @RestController public class ExcelController { @PostMapping("/importData") public void importData(@RequestParam("file") MultipartFile file) throws IOException { if (file == null) return; ArrayList list = new ArrayList(); AnalysisEventListener listener = new AnalysisEventListener() { @Override public void invoke(Object data, AnalysisContext context) { list.add(data); } @Override public void doAfterAllAnalysed(AnalysisContext context) { log.info("导入数据完毕"); } }; try { EasyExcel.read(file.getInputStream(), User.class, listener).sheet(0).doRead(); } catch (IOException e) { log.error("导入出错:{}", e.getMessage()); } list.forEach(System.out::println); } @PostMapping("/exportData") public void exportData(HttpServletResponse response) { List list = getList(); try { response.setContentType("application/vnd.ms-excel; charset=utf-8"); response.setCharacterEncoding("utf-8"); String fileName = "三好学生表"; response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8") + ".xlsx"); EasyExcel.write(response.getOutputStream(), User.class).registerConverter(new LocalDateTimeConverter()).sheet("test").doWrite(list); } catch (Exception e) { log.error("下载报表异常:{}", e.getMessage()); throw new RuntimeException("下载报表异常"); } } private List getList() { List list = new ArrayList(); LocalDateTime now = LocalDateTime.now(); User xd = new User("熊大", 10, 1, now); User ne = new User("牛二", 20, 0, now); User zs = new User("张三", 30, 1, now); User ls = new User("李四", 40, 1, now); User ww = new User("王五", 50, 0, now); list.add(xd); list.add(ne); list.add(zs); list.add(ls); list.add(ww); return list; } }

访问exportData接口会发现不是我们想要的结果:为什么导出的文件性别那一列只有0 1 没有男 女呢?好吧!那我们就自定义转换器,因为我们要转换0和1即Integer,所以编写如下转换器!注意要实现的是com.alibaba.excel.converters.Converter接口,别引用错包了!

public class SexConverter implements Converter { @Override public Class supportJavaTypeKey() { return Integer.class; } @Override public CellDataTypeEnum supportExcelTypeKey() { return CellDataTypeEnum.STRING; } @Override public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { return "男".equals(cellData.getStringValue()) ? 1 : 0; } @Override public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { return new CellData(value.equals(1) ? "男" : "女"); } } 划重点!Sex的converter编写完了,怎么才能让它生效呢?有一种解决方案! 找到需要转换的字段,在@ExcelProperty上添加converter属性

修改代码完毕之后再重启就可以了,不管是导入还是导出都没问题!Excel中的男/女导入之后在代码中会自动变为1/0,代码中的1/0导出之后会自动变为男/女!赶快试试吧!

细心的小伙伴已经发现了,LocalDateTime的converter有三种生效方案,但是sex的converter只有一种!点击查看Converter使用总结并带你追踪一下源码并给出具体的解决方案!

加油!



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭