依赖版本
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.1</version>
</dependency>
数据对象类
public class KsClAdReportBo {
@ExcelProperty("计划状态")
private String unitStatus;
@ExcelProperty("投放状态")
private String putStatus;
@ExcelProperty("出价类型")
private String bidType;
@ExcelProperty("优化目标")
private String ocpxActionType;
@ExcelProperty("投放方式")
private String speed;
@ExcelProperty("订单提交数")
private Long eventOrderSubmit;
@ExcelProperty("新增付费人数")
private Long eventNewUserPay;
@ExcelProperty("3秒播放数")
private Long playDuration3s;
@ExcelProperty("3秒播放率")
private Double playDuration3sRate;
//有效播放数,对标快手的played_three_seconds
@ExcelProperty("有效播放数")
private Long validPlay;
//视频播放完成数,对标快手的played_end
@ExcelProperty("完播数")
private Long play100FeedBreak;
//播放完成率
@ExcelProperty("完播率")
private Double playOverRate;
public String getUnitStatus() {
return unitStatus;
}
public void setUnitStatus(String unitStatus) {
this.unitStatus = unitStatus;
}
public String getPutStatus() {
return putStatus;
}
public void setPutStatus(String putStatus) {
this.putStatus = putStatus;
}
public String getBidType() {
return bidType;
}
public void setBidType(String bidType) {
this.bidType = bidType;
}
public String getOcpxActionType() {
return ocpxActionType;
}
public void setOcpxActionType(String ocpxActionType) {
this.ocpxActionType = ocpxActionType;
}
public String getSpeed() {
return speed;
}
public void setSpeed(String speed) {
this.speed = speed;
}
public Long getEventOrderSubmit() {
return eventOrderSubmit;
}
public void setEventOrderSubmit(Long eventOrderSubmit) {
this.eventOrderSubmit = eventOrderSubmit;
}
public Long getEventNewUserPay() {
return eventNewUserPay;
}
public void setEventNewUserPay(Long eventNewUserPay) {
this.eventNewUserPay = eventNewUserPay;
}
public Long getPlayDuration3s() {
return playDuration3s;
}
public void setPlayDuration3s(Long playDuration3s) {
this.playDuration3s = playDuration3s;
}
public Double getPlayDuration3sRate() {
return playDuration3sRate;
}
public void setPlayDuration3sRate(Double playDuration3sRate) {
this.playDuration3sRate = DoubleUtil.getDouble2(playDuration3sRate);
}
public Long getValidPlay() {
return validPlay;
}
public void setValidPlay(Long validPlay) {
this.validPlay = validPlay;
}
public Long getPlay100FeedBreak() {
return play100FeedBreak;
}
public void setPlay100FeedBreak(Long play100FeedBreak) {
this.play100FeedBreak = play100FeedBreak;
}
public Double getPlayOverRate() {
return playOverRate;
}
public void setPlayOverRate(Double playOverRate) {
this.playOverRate = DoubleUtil.getDouble2(playOverRate);
}
}
导出下载方法
/**
* excl 文件导出响应下载
*
* @param response 响应体
* @param exportData 导出数据
* @param fileName 文件名
* @param columnNames 自定义字段名
*/
public static <E> void write(HttpServletResponse response, List<E> exportData, String fileName,
List<String> columnNames) throws IOException {
if (CollectionUtils.isEmpty(exportData)) throw new HMException("暂无可导出数据");
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
//获取对象,并且根据自定义列的顺序反射赋值index顺序
Class<?> aClass = exportData.get(0).getClass();
//设置导出表格的字段顺序
setExcelIndex(aClass, columnNames);
//设置输出流和对象类型
ExcelWriterBuilder write = EasyExcel.write(response.getOutputStream(), aClass);
if (!CollectionUtils.isEmpty(columnNames)) {
//设置参数字段
write.includeColumnFieldNames(columnNames);
}
// 写入数据
write.sheet("sheet1").doWrite(() -> exportData);
write.excelType(ExcelTypeEnum.XLSX);
write.autoCloseStream(true);
} catch (Exception e) {
LOGGER.error("write Exception:", e);
}
}
/**
* 设置导出表格的字段顺序
*
* @param aClass 需要写入Excel的数据对象
* @param columnNames 需要展示的自定义列
*/
private static void setExcelIndex(Class<?> aClass, List<String> columnNames) throws NoSuchFieldException, IllegalAccessException {
//获取当前对象的字段
ArrayList<Field> fields = Lists.newArrayList(aClass.getDeclaredFields());
Class<?> superclass = aClass.getSuperclass();
if (superclass != null) {//有父类则获取父类对象的字段
fields.addAll(Lists.newArrayList(superclass.getDeclaredFields()));
}
for (int i = 0; i < columnNames.size(); i++) {
for (Field field : fields) {
if (!field.getName().equals(columnNames.get(i))) continue;
ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
if (annotation == null) continue;
InvocationHandler handler = Proxy.getInvocationHandler(annotation);
//获取 AnnotationInvocationHandler 的 memberValues 字段
Field fieldMv = handler.getClass().getDeclaredField("memberValues");
//因为这个字段事 private final 修饰,所以要打开权限
fieldMv.setAccessible(true);
Map<Object, Object> memberValues = (Map<Object, Object>) fieldMv.get(handler);
if (!memberValues.containsKey("value")) continue;
memberValues.put("index", i);
}
}
}
效果展示