该工具类可以导出任何对象集合的数据到Excel(对象必须已有getter方法
),但对象属性个数、顺序要和列标题保持一致 。
废话不多说,直接上源码以及测试代码,有误或者有不理解的地方,还望一起讨论!
工具类源码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 public class ExcelUtils { public static Map<String,Object> exportExcel (List<Object> list, String[] colTitles, String[] properties, String headTitle, String fileName, HttpServletResponse resp) { Map<String, Object> result = new HashMap<>(); if (colTitles.length != properties.length){ result.put("msg" ,"标题和对象属性个数不一致,无法匹配进行导出" ); result.put("success" ,false ); return result; } HSSFWorkbook workbook = new HSSFWorkbook(); CellRangeAddress callRangeAddress = new CellRangeAddress(0 ,0 ,0 ,colTitles.length-1 ); HSSFCellStyle headStyle = createCellStyle(workbook,(short )16 ,true ); HSSFCellStyle colStyle = createCellStyle(workbook,(short )13 ,true ); HSSFCellStyle valueStyle = createCellStyle(workbook,(short )10 ,false ); HSSFSheet sheet = workbook.createSheet(); sheet.addMergedRegion(callRangeAddress); sheet.setDefaultColumnWidth(25 ); HSSFRow row1 = sheet.createRow(0 ); HSSFCell cell = row1.createCell(0 ); cell.setCellStyle(headStyle); cell.setCellValue(headTitle); HSSFRow row2 = sheet.createRow(1 ); for (int i = 0 ;i < colTitles.length; i++){ HSSFCell cell2 = row2.createCell(i); cell2.setCellStyle(colStyle); cell2.setCellValue(colTitles[i]); } if (list != null ) { for (int j=0 ;j<list.size();j++) { HSSFRow row3 = sheet.createRow(j + 2 ); for (int k = 0 ;k < properties.length; k++) { HSSFCell cell3 = row3.createCell(k); cell3.setCellStyle(valueStyle); cell3.setCellValue(String.valueOf(getGetMethod(list.get(j), properties[k]))); } } } ServletOutputStream out = null ; try { out = resp.getOutputStream(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss" ); String timer = sdf.format(new Date(System.currentTimeMillis())); resp.setHeader("Content-Disposition" , "attachment;filename=" + URLEncoder.encode(fileName+"(" +timer+")" ,"UTF-8" )+".xls" ); resp.setHeader("Content-Type" , "application/octet-stream" ); workbook.write(out); result.put("msg" ,"Excel导出成功" ); result.put("success" ,true ); } catch (Exception e) { result.put("msg" ,"Excel导出失败" ); result.put("success" ,false ); e.printStackTrace(); } finally { if (out != null ){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } private static HSSFCellStyle createCellStyle (HSSFWorkbook workbook, short fontsize, Boolean isBold) { HSSFCellStyle style = workbook.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFFont font = workbook.createFont(); if (isBold){ font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); } font.setFontHeightInPoints(fontsize); style.setFont(font); return style; } private static Object getGetMethod (Object obj, String name) { Method[] m = obj.getClass().getMethods(); for (int i = 0 ;i < m.length;i++){ if (("get" +name).toLowerCase().equals(m[i].getName().toLowerCase())){ try { return m[i].invoke(obj); } catch (Exception e) { e.printStackTrace(); } } } return null ; } }
测试代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 @RequestMapping("/exportExcel") public void exportExcel (HttpServletResponse resp) { String[] colTitles = {"姓名" ,"年龄" ,"地址" }; String[] properties ={"name" ,"age" ,"address" }; User u1 = new User("张三" ,20 ,"北京" ,175 ); User u2 = new User("李四" ,22 ,"上海" ,178 ); User u3 = new User("王五" ,24 ,"广州" ,180 ); Person p1 = new Person("小明" ,18 ,"南京" ,"男" ); Person p2 = new Person("小红" ,26 ,"武汉" ,"女" ); Person p3 = new Person("小张" ,21 ,"杭州" ,"男" ); List<Object> list = new ArrayList<>(); list.add(u1);list.add(u2);list.add(u3); list.add(p1);list.add(p2);list.add(p3); Map<String, Object> result = ExcelUtils.exportExcel(list, colTitles, properties, "用户列表" , "用户数据统计表" , resp); System.out.println(result.get("msg" )); System.out.println(result.get("success" )); }
测试结果