EasyExcel导出,特殊类型转换
easyexcel
快速、简洁、解决大文件内存溢出的java处理Excel工具
项目地址:https://gitcode.com/gh_mirrors/ea/easyexcel

·
在写Excel导出时,我们通常会使用EasyExcel。
public void exportWipStepVO(HttpServletResponse response, WipStepQuery wipStepQuery) {
//获取需要导出的数据
List<WipStepVO> wipStepVOList = selectWipStepVOList(wipStepQuery);
//导出数据
try {
String fileName = "组计划" + LocalDate.now();
fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
EasyExcel.write(response.getOutputStream(), WipStepVO.class)
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
.registerWriteHandler(new HorizontalCellStyleStrategy(new WriteCellStyle(), contentWriteCellStyle))
.sheet("组计划")
.doWrite(wipStepVOList);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
对于导出的表格的表头定义,我们会在导出的数据的实体类的参数添加注解
@ExcelProperty(“XXX”):定义表头
@ExcelIgnore:标识不导出该字段
其中如果导出的字段为时间类型或者其他类型,我们在导出的时候可能会遇到问题,这时候我们就可以运用@ExcelProperty注解的另一个用法。
@Data
public class WipStepVO {
/**
* 工单id
*/
@JsonSerialize(using = ToStringSerializer.class)
@ApiModelProperty("工单id")
@ExcelIgnore
private Long id;
/**
* 工种
*/
@ApiModelProperty("工种")
@ExcelProperty(value = "工种", converter = WorkTypeEnumConverter.class)
private WorkTypeEnum workType;
/**
* 产品生产工单编码
*/
@ApiModelProperty("产品生产工单编码")
@ExcelProperty("产品生产工单编码")
private String workOrderCode;
/**
* 源数据编号(生产订单)
*/
@ApiModelProperty("源数据编号(生产订单)")
@ExcelProperty("源数据编号(生产订单)")
private String sourceCode;
/**
* 是否重点
*/
@ApiModelProperty("是否重点")
@ExcelProperty("是否重点")
private String isKey;
/**
* 生产组织
*/
@ApiModelProperty(value = "生产组织")
@ExcelProperty("生产组织")
private String prdOrg;
/**
* 生产厂区
*/
@ApiModelProperty(value = "生产厂区")
@ExcelProperty("生产厂区")
private String productionFactory;
/**
* 产品编码
*/
@ApiModelProperty("产品编码")
@ExcelProperty("产品编码")
private String productCode;
/**
* 项目名称
*/
@ApiModelProperty("项目名称")
@ExcelProperty("项目名称")
private String projectName;
/**
* 生产类型
*/
@ApiModelProperty("生产类型")
@ExcelProperty(value = "生产类型" , converter = ProductionTypeEnumConverter.class)
private ProductionTypeEnum productionType;
/**
* 生产数量
*/
@ApiModelProperty("生产数量")
@ExcelProperty("生产数量")
private Integer quantity;
/**
* 责任部门
*/
@ApiModelProperty("责任部门")
@ExcelIgnore
private Long responsibleDept;
/**
* 责任部门名称
*/
@ApiModelProperty("责任部门名称")
@ExcelProperty("责任部门名称")
private String responsibleDeptName;
/**
* 计划开始日期
*/
@ApiModelProperty("计划开始日期")
@ExcelProperty(value = "计划开始日期", converter = LocalDateConverterUtil.class)
private LocalDate planStartDate;
/**
* 计划结束日期
*/
@ApiModelProperty("计划结束日期")
@ExcelProperty(value = "计划结束日期", converter = LocalDateConverterUtil.class)
private LocalDate planEndDate;
/**
* 实际开始时间
*/
@ApiModelProperty("实际开始时间")
@ExcelProperty(value = "实际开始时间", converter = LocalDateTimeConverterUtil.class)
@JsonSerialize(using = LocalDateTimeToSecondSerialize.class)
private LocalDateTime actualStartTime;
/**
* 实际结束时间
*/
@ApiModelProperty("实际结束时间")
@ExcelProperty(value = "实际结束时间", converter = LocalDateTimeConverterUtil.class)
private LocalDateTime actualEndTime;
/**
* 需求人力
*/
@JsonSerialize(
using = ToStringSerializer.class
)
@ApiModelProperty("需求人力")
@ExcelProperty("需求人力")
private Long demandManpower;
/**
* 是否分配
*/
@ApiModelProperty("是否分配")
@ExcelProperty("是否分配")
private String isFinish;
@ExcelIgnore
private Integer c1;
@ExcelIgnore
private Integer c2;
}
1、时间字段的转换,例如LocaldateTime,我们可以编写一个转换工具类,然后在需要转换的字段添加注解 :
@ExcelProperty(value = "实际开始时间", converter = LocalDateTimeConverterUtil.class)
public class LocalDateTimeConverterUtil implements Converter<LocalDateTime> {
@Override
public Class supportJavaTypeKey() {
return LocalDate.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
@Override
public CellData convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return new CellData(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
}
2、枚举类型的字段,同样可以自己编写一个适用的转换类。
public class ProductionTypeEnumConverter implements Converter<ProductionTypeEnum> {
@Override
public Class supportJavaTypeKey() {
return null;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return null;
}
@Override
public ProductionTypeEnum convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
// 将从 Excel 中读取的字符串转换为枚举类型
String cellValue = cellData.getStringValue();
return ProductionTypeEnum.valueOf(cellValue);
}
@Override
public CellData<String> convertToExcelData(ProductionTypeEnum value, ExcelContentProperty contentProperty
, GlobalConfiguration configuration) {
// 将枚举类型字段转换为可导出的字符串格式
String displayValue = value.getDesc();
return new CellData<>(displayValue);
}
}




快速、简洁、解决大文件内存溢出的java处理Excel工具
最近提交(Master分支:27 天前 )
c42183df
Bugfix 1 年前
efa7dff6 * 重新加回 `commons-io`
1 年前
更多推荐
所有评论(0)