1、配置文件准备
1.1 pom.xml 导入EasyExcel 依赖包
org.apache.commons
commons-lang3
3.9
com.alibaba
easyexcel
1.1.1
1.2 配置i18n 国际化文件
在resources 目录下,点击右键-->new --> Resource Bundle 创建国际化文件,如下图所示:
1.3 在yml 文件中配置国际化文件地址messages: 和 需要导出的excel文件头的国际化key
二、编写excel 配置文件
2.1 构造导出对象
import com.alibaba.excel.metadata.BaseRowModel;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
public class EasyExcelParams {
/**
* excel文件名(不带拓展名)
*/
private String excelNameWithoutExt;
/**
* sheet名称
*/
private String sheetName ="sheet1";
/**
* 是否需要表头
*/
private boolean needHead = true;
/**
* 数据
*/
private List extends BaseRowModel> data;
/**
* 数据模型类型
*/
private Class extends BaseRowModel> dataModelClazz;
/**
* 响应
*/
private HttpServletResponse response;
public EasyExcelParams(String excelNameWithoutExt, String sheetName, List extends BaseRowModel> data, Class extends BaseRowModel> dataModelClazz, HttpServletResponse response ){
this.excelNameWithoutExt = excelNameWithoutExt;
this.sheetName = sheetName;
this.data = data;
this.dataModelClazz = dataModelClazz;
this.response = response;
}
public EasyExcelParams(String excelNameWithoutExt, List extends BaseRowModel> data, Class extends BaseRowModel> dataModelClazz, HttpServletResponse response ){
this.excelNameWithoutExt = excelNameWithoutExt;
this.data = data;
this.dataModelClazz = dataModelClazz;
this.response = response;
}
/**
* 检查不允许为空的属性
*/
public boolean isValid() {
return ObjectUtils.allNotNull(excelNameWithoutExt, data, dataModelClazz, response);
}
public String getExcelNameWithoutExt() {
return excelNameWithoutExt;
}
public void setExcelNameWithoutExt(String excelNameWithoutExt) {
this.excelNameWithoutExt = excelNameWithoutExt;
}
public String getSheetName() {
return sheetName;
}
public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}
public boolean isNeedHead() {
return needHead;
}
public void setNeedHead(boolean needHead) {
this.needHead = needHead;
}
public List extends BaseRowModel> getData() {
return data;
}
public void setData(List extends BaseRowModel> data) {
this.data = data;
}
public Class extends BaseRowModel> getDataModelClazz() {
return dataModelClazz;
}
public void setDataModelClazz(Class extends BaseRowModel> dataModelClazz) {
this.dataModelClazz = dataModelClazz;
}
public HttpServletResponse getResponse() {
return response;
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
}
2.2 配置获取excel 头的国际化key
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
@Data
@Configuration
@ConfigurationProperties("excel")
public class ExcelHeaderName {
private List userInfoHeader;
private List exportFailure;
}
2.3 编写EasyExcelUtil 导出工具类
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.google.common.collect.Lists;
import com.***.fsd.ums.common.context.SpringBeanContext;
import com.***.fsd.ums.core.model.dto.EasyExcelParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.springframework.context.MessageSource;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* EasyExcel工具类
*
* @author [email protected]
* @date 2018/11/19
*/
@Slf4j
public class EasyExcelUtil {
/**
* 读取 Excel(多个 sheet)
*
* @param excel 文件
* @param clazz 实体类映射,继承 BaseRowModel 类
* @return Excel 数据 list
*/
public static List readExcel(MultipartFile excel, Class extends BaseRowModel> clazz) {
ExcelListener excelListener = new ExcelListener();
ExcelReader reader = getReader(excel, excelListener);
if (reader == null) {
return null;
}
for (Sheet sheet : reader.getSheets()) {
sheet.setClazz(clazz);
reader.read(sheet);
}
return excelListener.getDatas();
}
/**
* 读取某个 sheet 的 Excel
*
* @param excel 文件
* @param rowModel 实体类映射,继承 BaseRowModel 类
* @param sheetNo sheet 的序号 从1开始
* @return Excel 数据 list
*/
public static List
springBeanContext.java
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringBeanContext implements ApplicationContextAware {
private static ApplicationContext springContext;
@Override
public void setApplicationContext(ApplicationContext springContext) throws BeansException {
this.springContext = springContext;
}
public static ApplicationContext getSpringContext() {
return springContext;
}
/**
* 根据bean名称获取bean
* @param name
* @return
*/
public static Object getBean(String name){
return getSpringContext().getBean(name);
}
/**
* 根据bean class获取bean
* @param clazz
* @return
*/
public static T getBean(Class clazz){
return getSpringContext().getBean(clazz);
}
/**
* 根据bean名称和bean class获取bean
* @param name
* @param clazz
* @return
*/
public static T getBean(String name, Class clazz){
return getSpringContext().getBean(name, clazz);
}
}
三、使用示例
导出对象:
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* @Author: zhenglei12
* @Data: Created in 2019/7/29 15:09
* @Description:
*/
@Data
public class UserExprotVO extends BaseRowModel {
@ApiModelProperty(value="序列号Id", hidden=false, notes="Id", dataType="Integer")
@ExcelProperty(value = "id", index = 0)
private Integer id;
@ApiModelProperty(value="用户名", hidden=false, notes="用户名", dataType="String")
@ExcelProperty(value = "姓名", index = 1)
private String username;
@ApiModelProperty(value="用户密码", hidden=false, notes="用户密码", dataType="String")
@ExcelProperty(value = "用户密码", index = 2)
private String password;
@ApiModelProperty(value="邮箱地址", hidden=false, notes="邮箱地址", dataType="String")
@ExcelProperty(value = "邮箱地址", index = 3)
private String email;
@ApiModelProperty(value="电话号码", hidden=false, notes="电话号码", dataType="String")
@ExcelProperty(value = "电话号码", index = 4)
private String phone;
@ApiModelProperty(value="创建时间", hidden=false, notes="创建时间", dataType="String")
@ExcelProperty(value = "创建时间", index = 5)
private String createTime;
@ApiModelProperty(value="更新时间", hidden=false, notes="更新时间", dataType="String")
@ExcelProperty(value = "更新时间", index = 6)
private String updateTime;
}
测试:
@PostMapping(value = "/exportUsers")
@ApiOperation(value = "导出用户信息",notes = "导出用户信息")
public DataResult exportUsers(@Valid @RequestBody UserExportQuery query, HttpServletRequest request, HttpServletResponse response){
DataResult dataResult = new DataResult<>();
List userVOList= userService.exportUsers(query);
EasyExcelParams easyExcelParams = new EasyExcelParams("userInfo", userVOList, UserExprotVO.class, response);
try {
// EasyExcelUtil.exportExcel2007Format(easyExcelParams);
String language = "zh_CN";
// String language = "en_US";
List headerList = excelHeaderName.getUserInfoHeader();
EasyExcelUtil.exportExcelWithCustomHeader(easyExcelParams, headerList,language);
} catch (IOException e) {
e.printStackTrace();
}
return dataResult;
}