在EasyExcel中,Converter
接口用于定义如何在Java对象和Excel单元格之间进行转换。
也就是说EasyExcel可以根据数据库中的值来填充Excel中对应的文本内容。
比如数据库1,2,3可以填充到excel中:男,女,其他
使用easyExcel的之前,请导入相关pom依赖以及创建一个表实体的映射类
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.2.1</version>
</dependency>
@Data
public class UserData {
// ... 其他字段 ...
@ExcelProperty(value = "性别", converter = GenderConverter.class)
private Integer gender;
在上面的代码中,GenderConverter是一个自定义转换器
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
public class GenderConverter implements Converter<Integer> {
@Override
public Class<Integer> supportJavaTypeKey() {
return Integer.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
if(cellData.getStringValue().equals("男")){
return 1;
}else if(cellData.getStringValue().equals("女")){
return 2;
}else {
return 3;
}
}
@Override
public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
if(value == 1){
return new WriteCellData<>("男");
}else if(value == 2){
return new WriteCellData<>("女");
}else {
return new WriteCellData<>("其他");
}
}
}
supportJavaTypeKey()
:
这个方法用于指定转换器支持的Java类型。在这个例子中,GenderConverter
转换器支持Integer
类型的Java对象。当EasyExcel在处理Excel文件并遇到需要转换为Integer
类型的字段时,它会查找是否有对应的转换器可以处理这种转换。supportExcelTypeKey()
:
这个方法用于指定转换器支持的Excel数据类型。CellDataTypeEnum.STRING
表示这个转换器可以将Java对象转换为Excel中的字符串类型。这意味着,即使你的Java对象是一个整数(在这个例子中是性别代码),转换器也会将其转换为Excel单元格中的文本形式。这两个方法一起帮助EasyExcel确定在读取或写入Excel文件时应该使用哪个转换器来处理特定类型的字段。当你使用注解(如
@ExcelProperty
)来指定字段的转换器时,EasyExcel会根据这些注解和转换器中的supportJavaTypeKey()
和supportExcelTypeKey()
方法来确定正确的转换逻辑。在我的例子中,
GenderConverter
的作用是将整数类型的性别代码(如1、2、3)转换为Excel单元格中的文本表示(如“男”、“女”、“其他”)。因此,supportJavaTypeKey()
返回Integer.class
,而supportExcelTypeKey()
返回CellDataTypeEnum.STRING
,以确保EasyExcel在导出时使用这个转换器来将性别代码转换为文本。convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)
:这个方法用于将Excel单元格中的数据转换为Java对象。在这个例子中,它根据Excel单元格中的字符串值来返回相应的整数。如果单元格的值是"男",则返回整数1。如果单元格的值是"女",则返回整数2。如果单元格的值不是"男"也不是"女",则返回整数3。这个方法通常用于数据导入场景,当你从Excel文件中读取数据时,EasyExcel会使用这个方法来将Excel中的文本转换为Java对象中的相应字段值。
convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)
:
这个方法用于将Java对象中的数据转换为Excel单元格数据。在这个例子中,它根据Java对象中的整数值来返回相应的字符串,这些字符串将被写入Excel单元格中。如果Java对象的值是1,则返回字符串"男"。如果Java对象的值是2,则返回字符串"女"。如果Java对象的值既不是1也不是2,则返回字符串“其他”。
这个方法通常用于数据导出场景,当你将数据写入Excel文件时,EasyExcel会使用这个方法来将Java对象中的字段值转换为Excel单元格中的文本。
通过实现这两个方法,你可以自定义Java对象和Excel单元格数据之间的转换逻辑,以满足特定的业务需求。
然后就没什么好说的了,就直接导出,写好查询方法,在接口调用exportExcel的方法就行
List<UserData> userDataList = userService.findAll(); // 假设你有一个userService用于查询数据
import com.alibaba.excel.EasyExcel;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
public void exportExcel(List<UserData> userDataList, HttpServletResponse response) {
// 设置响应头信息
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = "用户数据.xlsx";
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
try (OutputStream outputStream = response.getOutputStream()) {
// 导出Excel文件
EasyExcel.write(outputStream, UserData.class).sheet("用户数据").doWrite(userDataList);
} catch (IOException e) {
e.printStackTrace();
}
}