package com.ycl.jxkg.utils; import com.alibaba.excel.EasyExcel; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; /** * excel工具类 * * @author gonghl */ public class ExcelUtils { /** * Excel导出 * * @param response response * @param fileName 文件名 * @param sheetName sheetName * @param list 数据List * @param pojoClass 对象Class */ public static void exportExcel(HttpServletResponse response, String fileName, String sheetName, List list, Class pojoClass) throws IOException { if (StringUtils.isBlank(fileName)) { fileName = Long.toString(System.currentTimeMillis()); } response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("UTF-8"); fileName = URLEncoder.encode(fileName, "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); EasyExcel.write(response.getOutputStream(), pojoClass).sheet(sheetName).doWrite(list); } /** * Excel导出,先sourceList转换成List,再导出 * * @param response response * @param fileName 文件名 * @param sheetName sheetName * @param sourceList 原数据List * @param targetClass 目标对象Class */ public static void exportExcelToTarget(HttpServletResponse response, String fileName, String sheetName, List sourceList, Class targetClass) throws Exception { List targetList = new ArrayList<>(sourceList.size()); for (Object source : sourceList) { Object target = targetClass.newInstance(); BeanUtils.copyProperties(source, target); targetList.add(target); } exportExcel(response, fileName, sheetName, targetList, targetClass); } }