利用Java反射机制实现。
1.自定义注解:
import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Excel { public String colName(); //列明 public int order(); //顺序 }
2.在需要导出的字段的get方法上使用:因为get方法可以获取该字段的数据
@Excel(colName = "座位号", order = 17) public String getNo() { return no; }
3.编写导出Excel工具类:

package com.***.common.utils; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.lang.reflect.Method; import java.util.Date; import java.util.List; import java.util.Map; import java.util.TreeMap; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import com.***.common.annotation.Excel; public class POIExportUtils<T>{ public void ResponseInit(HttpServletResponse response, String fileName){ response.reset(); //设置content-disposition响应头控制浏览器以下载的形式打开文件 response.setHeader("Content-Disposition", "attachment;filename="+ fileName + new Date() + ".xls"); //让服务器告诉浏览器它发送的数据属于excel文件类型 response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setHeader("Prama", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); } public void POIOutPutStream(HttpServletResponse response, HSSFWorkbook wb){ try { BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); wb.write(out); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @SuppressWarnings({ "unchecked", "rawtypes" }) public void export(Class<T> objClass, List<T> dataList, HttpServletResponse response, String fileName) throws Exception{ ResponseInit(response,fileName); Class excelClass = Class.forName(objClass.toString().substring(6)); Method[] methods = excelClass.getMethods(); Map<Integer, String> mapCol = new TreeMap<>(); Map<Integer, String> mapMethod = new TreeMap<>(); for (Method method : methods) { Excel excel = method.getAnnotation(Excel.class); if (excel != null) { mapCol.put(excel.order(), excel.colName()); mapMethod.put(excel.order(), method.getName()); } } HSSFWorkbook wb = new HSSFWorkbook(); POIBuildBody(POIBuildHead(wb,"sheet1",mapCol),excelClass,mapMethod,dataList); POIOutPutStream(response,wb); } public HSSFSheet POIBuildHead(HSSFWorkbook wb, String sheetName, Map<Integer, String> mapCol){ HSSFSheet sheet01 = wb.createSheet(sheetName); HSSFRow row = sheet01.createRow(0); HSSFCell cell; int i = 0; for (Map.Entry<Integer, String> entry : mapCol.entrySet()) { cell = row.createCell(i++); cell.setCellValue(entry.getValue()); } return sheet01; } public void POIBuildBody(HSSFSheet sheet01, Class<T> excelClass, Map<Integer, String> mapMethod, List<T> dataList) throws Exception{ HSSFRow r = null; HSSFCell c = null; if(dataList != null && dataList.size() > 0){ for(int i = 0;i<dataList.size();i++){ r= sheet01.createRow(i+1); //r.setHeightInPoints(25); int j = 0; for (Map.Entry<Integer, String> entry : mapMethod.entrySet()) { c = r.createCell(j++); Object obj = excelClass.getDeclaredMethod(entry.getValue()).invoke(dataList.get(i)); c.setCellValue(obj==null?"":obj+""); } } } } }
4.测试:
new POIExportUtils<CaseInfo>().export(CaseInfo.class, dataList, response, "caseInfo_");