pom中加入以下依赖:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.15-beta2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.15-beta2</version> </dependency>先直接贴代码 :工具类
package com.health.partner.soms.utils; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.time.DateFormatUtils; import org.apache.poi.hssf.usermodel.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import javax.servlet.http.HttpServletResponse; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.List; /** * @Description: TODO * @Author: huangW * @Date: 2019/10/28 9:55 * @Version 1.0 */ public class FilePortUtil { private static final Logger log = LoggerFactory.getLogger(FilePortUtil.class); /** * 导出功能 * 注意:泛型T类字段名和containBean集合里字段名字的一致性 * * @param response * @param title 表名 * @param headers 表头 * @param list 数据集 * @param containBean 数据集类型字段 * @param <T> * @throws Exception */ public static <T> void exportExcel(HttpServletResponse response, String title, String[] headers, List<T> list, List<String> containBean) throws Exception { HSSFWorkbook workbook = null; try { workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(title); HSSFRow row = sheet.createRow(0); /*创建第一行表头*/ for (short i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } Iterator<T> it = list.iterator(); int index = 0; while (it.hasNext()) { index++; row = sheet.createRow(index); T t = (T) it.next(); /*反射得到字段*/ Field[] fields = t.getClass().getDeclaredFields(); /*如果需要匹配*/ if (CollectionUtils.isNotEmpty(containBean)) { for (int j = 0; j < containBean.size(); j++) { for (int i = 0; i < fields.length; i++) { Field field = fields[i]; if (!field.getName().equals(containBean.get(j))) continue; /*给每一列set值*/ setCellValue(t, field, row, j); } } } else { for (int i = 0; i < fields.length; i++) { Field field = fields[i]; setCellValue(t, field, row, i); } } } // SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); //重要点 String fileName = new String(title.getBytes(), "iso8859-1") + ".xls"; /*application/vnd.ms-excel告诉浏览器要下载的是个excel*/ response.setContentType("application/vnd.ms-excel;charset=UTF-8"); /*请求头设置,Content-Disposition为下载标识,attachment标识以附件方式下载*/ response.addHeader("Content-Disposition", "attachment;filename=" + fileName); workbook.write(response.getOutputStream()); } finally { if (workbook != null) { workbook.close(); } } } /** * 设置每一行中的列 * * @param t * @param field * @param row * @param index * @param <T> */ private static <T> void setCellValue(T t, Field field, HSSFRow row, int index) { HSSFCell cell = row.createCell(index); Object value = invoke(t, field); String textValue = null; if (value != null) { if (value instanceof Date) { Date date = (Date) value; textValue = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"); } else { textValue = value.toString(); } } if (textValue != null) { cell.setCellValue(textValue); } } /** * 反射映射数据集字段 * * @param t * @param field * @param <T> * @return */ private static <T> Object invoke(T t, Field field) { try { String fieldName = field.getName(); PropertyDescriptor pd = new PropertyDescriptor(fieldName, t.getClass()); Method method = pd.getReadMethod(); return method.invoke(t); } catch (Exception e) { return null; } } } controller层代码: @PostMapping("/RankingExport") @ApiOperation(value = "活动图表->推广java后台导出", notes = "推广java后台导出") public void Rankings(@ApiParam @RequestBody UserRecomRankingReqtDto userRecomRankingReqtDto, HttpServletResponse response) throws ParseException { log.info("UserReportResource.Rankings->{}", userRecomRankingReqtDto); String title = "recom2" ; String[] headers = { "推荐人约伴ID","推荐人", "推荐人手机号", "拉新人数","部门","推广人员排名"}; List<UserRecomRankingRespDto> listObject = userReportBlogic.Rankings(userRecomRankingReqtDto); List<String> listColumn = Arrays.asList("userNumber", "name", "phoneNumber", "counts", "departmentName","ranking"); try { FilePortUtil.exportExcel(response, title, headers, listObject, listColumn); } catch (Exception e) { e.printStackTrace(); } }List listObject = userReportBlogic.Rankings(userRecomRankingReqtDto);是根据条件查询的数据,调用FilePortUtil.exportExcel(response, title, headers, listObject, listColumn);就可以了。简单粗暴!