大多数业务项目中必然会遇到的一个需求,导出数据文件---Excel。
这里简单贴一下自己开发使用的方式方法,抛砖引玉。
也可小白直接大胆食用。
功能Maven依赖:
XML
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.5.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.5.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.5.0</version>
</dependency>
业务入口controller,这里很简单
java
@GetMapping("/exportApply")
public void exportApply(BusBasicFlowQuery query, HttpServletResponse response,
@RequestHeader(required = false) String lang) throws IOException {
query.setSize(20000);
// query:这里就是个查询条件,根据个人业务实操就行
// 这里就是根据业务需求查询出想要导出的数据集合信息
// 重点说明一下:Bd3OrderExcel里面包好你要导出的表格列说明
List<Bd3OrderExcel> bd3OrderExcelList = busBasicFlowService.exportApply(query, lang);
ExportParams exportParams = new ExportParams();
ExcelUtils.exportExcel(response, "北三卡申请", bd3OrderExcelList, Bd3OrderExcel.class, exportParams);
}
重点说明一下Bd3OrderExcel,这个类注明的导出表格字段名称、顺序,宽度等。
java
package com.rdss.bus.model.excel;
import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class Bd3OrderExcel {
private Integer id;
/**
* 申请人身份证号
*/
@Excel(name = "申请人姓名",width = 30.0)
private String operatorName;
/**
* 申请时间
*/
@Excel(name = "申请时间",format = "yyyy-MM-dd HH:mm:ss", databaseFormat="yyyy-MM-dd HH:mm:ss" ,width = 30.0)
private LocalDateTime applyTime;
/**
* 申请原因
*/
@Excel(name = "申请原因",width = 20.0)
private String remark;
/**
* 申请人手机号
*/
@Excel(name = "申请人手机号",width = 20.0)
private String operatorMobile;
/**
* 申请人身份证号
*/
@Excel(name = "申请人身份证号",width = 30.0)
private String operatorIdcardNum;
/**
* 申请人邮箱
*/
@Excel(name = "申请人邮箱",width = 30.0)
private String operatorEmail;
/**
* 申请人机构
*/
@Excel(name = "申请人机构",width = 30.0)
private String operatorOrgName;
/**
* 审批结果(0-审核中,1-通过,2-不通过
*/
@Excel(name = "一级审批状态",dict = "approval_result",width = 15.0)
private String approveResult_first;
/**
* 审批人姓名
*/
@Excel(name = "一级审批人姓名",width = 30.0)
private String approveUserName_first;
}
导出封装工具类:
java
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.rdss.bus.service.handler.DictHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* excel工具类
*/
@Slf4j
public class ExcelUtils {
private ExcelUtils(){}
@Autowired
private static DictHandler dictHandler;
/**
* Excel导出
*
* @param response response
* @param fileName 文件名
* @param list 数据List
* @param pojoClass 对象Class
*/
public static void exportExcel(HttpServletResponse response, String fileName, Collection<?> list, Class<?> pojoClass, ExportParams exportParams ) throws IOException {
exportParams.setStyle(ExcelStyleUtil.class);
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
}
/**
* Excel导出,先sourceList转换成List<targetClass>,再导出
*
* @param response response
* @param fileName 文件名
* @param sourceList 原数据List
* @param targetClass 目标对象Class
*/
public static void exportExcelToTarget(HttpServletResponse response, String fileName, Collection<?> sourceList,
Class<?> targetClass) throws IllegalAccessException, InstantiationException, IOException {
List<Object> targetList = new ArrayList<>(sourceList.size());
for(Object source : sourceList){
Object target = targetClass.newInstance();
BeanUtils.copyProperties(source, target);
targetList.add(target);
}
exportExcel(response, fileName, targetList, targetClass, new ExportParams());
}
}