有时候博客内容会有变动,首发博客是最新的,其他博客地址可能未同步,认准 https://blog.zysicyj.top。
全网最细面试题手册,支持艾宾浩斯记忆法。这是一份最全面、最详细、最高质量的 Java 面试题,不建议死记硬背,只需每天复习一遍,有个大概印象即可。详见 https://store.amazingmemo.com/chapterDetail/1685324709017001。
处理 LocalDateTime 与 EasyExcel 的兼容性问题
在使用 EasyExcel 进行数据导入导出时,公司要求使用 LocalDateTime 类型,但 EasyExcel 仅支持 Date 类型,这会导致如下报错:
Can not find 'Converter' support class LocalDateTime.
经过多次搜索无果后,最终解决方案是自定义转换器。具体操作如下:
Step 1: 创建 User 类并添加 EasyExcel 注解
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
@ExcelProperty(value = "姓名", index = 0)
private String name;
@ExcelProperty(value = "年龄", index = 1)
private Integer age;
@ExcelProperty(value = "性别", index = 2)
private Integer sex;
@ExcelProperty(value = "创建时间", index = 3, converter = LocalDateTimeConverter.class)
private LocalDateTime createTime;
}
Step 2: 创建 Controller 进行测试
@Slf4j
@RestController
public class ExcelController {
@PostMapping("/importData")
public void importData(@RequestParam("file") MultipartFile file) throws IOException {
if (file == null) return;
List<Object> 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<User> 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).sheet("test").doWrite(list);
} catch (Exception e) {
log.error("下载报表异常:{}", e.getMessage());
throw new RuntimeException("下载报表异常");
}
}
private List<User> getList() {
List<User> list = new ArrayList<>();
LocalDateTime now = LocalDateTime.now();
list.add(new User("熊大", 10, 1, now));
list.add(new User("牛二", 20, 0, now));
list.add(new User("张三", 30, 1, now));
list.add(new User("李四", 40, 1, now));
list.add(new User("王五", 50, 0, now));
return list;
}
}
Step 3: 创建 LocalDateTime 转换器
public class LocalDateTimeConverter implements Converter<LocalDateTime> {
@Override
public Class<LocalDateTime> supportJavaTypeKey() {
return LocalDateTime.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
@Override
public CellData<String> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return new CellData<>(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
}
激活自定义转换器的三种方法
- 在需要转换的字段上添加
@ExcelProperty注解的converter属性。 - 使用
ExcelWriterBuilder为单次操作添加转换器。 - 全局配置转换器。
上述三种方法任选其一即可。修改代码并重启后,无论是导入还是导出,时间字段均可正常处理。赶快试试吧!
加油!
本文是原创文章,采用 CC BY-NC-SA 4.0 协议,完整转载请注明来自 小朱
评论
隐私政策
0/500
滚动到此处加载评论...


