1 Maven依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.8</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
2 转换字段加载类型转换器
2.1 @ExcelProperty的converter属性加载类型转换器
package com.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import com.converter.LocalDateConverter;
import lombok.Data;
import java.time.LocalDate;
@Data
public class TestDto {
@ExcelProperty(value = "创建时间", index = 1, converter = LocalDateConverter.class)
private LocalDate createDate;
}
2.2 调试代码
/**
* 字段加载类型转换器
*
* @param response
*/
@GetMapping("/attributeConverter")
public void attributeConverter(HttpServletResponse response) throws IOException {
List<TestDto> sheetDataList = new ArrayList<>();
TestDto testDto=new TestDto();
testDto.setCreateDate(LocalDate.parse("2021-07-30", DateTimeFormatter.ofPattern("yyyy-MM-dd")));
sheetDataList.add(testDto);
response.addHeader("Content-Disposition", "filename=test.xlsx");
//设置类型,扩展名为.xls
response.setContentType("application/vnd.ms-excel");
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), TestDto.class).build();
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
excelWriter.write(sheetDataList, writeSheet);
//千万别忘记finish 会帮忙关闭流
excelWriter.finish();
}
2.3 调试结果
3 导出时加载类型转换器
3.1 调试代码
/**
* 导出时加载类型转换器
*
* @param response
*/
@GetMapping("/exportRegisterConverter")
public void exportRegisterConverter(HttpServletResponse response) throws IOException {
List<List<Object>> sheetDataList = new ArrayList<>();
List<Object> rowList = new ArrayList<>();
rowList.add(LocalDate.parse("2021-07-30", DateTimeFormatter.ofPattern("yyyy-MM-dd")));
sheetDataList.add(rowList);
response.addHeader("Content-Disposition", "filename=test.xlsx");
//设置类型,扩展名为.xls
response.setContentType("application/vnd.ms-excel");
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
//加载类型转换器
.registerConverter(new LocalDateConverter()).build();
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
excelWriter.write(sheetDataList, writeSheet);
//千万别忘记finish 会帮忙关闭流
excelWriter.finish();
}
3.2 调试结果
4 加载全局类型转换器
4.1 CustomConverterLoader
加载自定义类型转换器(加载器)。
package com.loader;
import com.alibaba.excel.converters.*;
import com.converter.LocalDateConverter;
import org.springframework.context.annotation.*;
import java.util.Map;
/**
* 加载自定义类型转换器(加载器)
*/
@Configuration
public class CustomConverterLoader {
@Bean
public DefaultConverterLoader defaultConverterLoader() {
DefaultConverterLoader converters = new DefaultConverterLoader();
LocalDateConverter localDateConverter = new LocalDateConverter();
Map<String, Converter> defaultWriteConverter = converters.loadDefaultWriteConverter();
defaultWriteConverter.put(ConverterKeyBuild.buildKey(localDateConverter.supportJavaTypeKey()), localDateConverter);
Map<String, Converter> allConverter=converters.loadAllConverter();
allConverter.put(ConverterKeyBuild.buildKey(localDateConverter.supportJavaTypeKey(), localDateConverter.supportExcelTypeKey()), localDateConverter);
return converters;
}
}
4.2 调试代码
/**
* 加载全局类型转换器
*
* @param response
*/
@GetMapping("/loadGlobalConverter")
public void loadGlobalConverter(HttpServletResponse response) throws IOException {
List<List<Object>> sheetDataList = new ArrayList<>();
List<Object> rowList = new ArrayList<>();
rowList.add(LocalDate.parse("2021-07-30", DateTimeFormatter.ofPattern("yyyy-MM-dd")));
rowList.add(new BigDecimal(0.11));
sheetDataList.add(rowList);
response.addHeader("Content-Disposition", "filename=test.xlsx");
//设置类型,扩展名为.xls
response.setContentType("application/vnd.ms-excel");
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
excelWriter.write(sheetDataList, writeSheet);
//千万别忘记finish 会帮忙关闭流
excelWriter.finish();
}
4.3 调试结果
注:
LocalDateConverter源码请查看以下博客。