直接上代码:
1 /** 2 * 导出 3 * 4 * @param policyQueryVo 5 * @param headtitle 6 * @return 7 */ 8 public HSSFWorkbook findExcel(PolicyQueryVo policyQueryVo, String headtitle, String fieldName) { 9 //获取要导出的内容 10 List<PolicyQueryVo> policyQueryList = policyQueryDao.getPolicyQueryList(null, null, policyQueryVo); 11 HSSFWorkbook workbook = new HSSFWorkbook(); 12 //创建表头单元格并添加相应的内容 13 String[] cells = headtitle.split(","); 14 //判断集合是否为空 15 if (policyQueryList != null) { 16 outputColums(cells, policyQueryList, workbook, fieldName); 17 } 18 return workbook; 19 }
1 /** 2 * 设置每行数据 3 * 4 * @param headers 5 * @param colums 6 * @param hssfWorkbook 7 */ 8 public static void outputColums(String[] headers, List<?> colums, HSSFWorkbook hssfWorkbook, String fieldName) { 9 //声明一个sheet 10 HSSFSheet sheet = hssfWorkbook.createSheet(); 11 //创建表头 12 HSSFRow row = sheet.createRow(0); 13 for (int i = 0; i < headers.length; i++) { 14 HSSFCell cell = row.createCell(i); 15 cell.setCellValue(headers[i]); 16 } 17 //循环多少行 18 String[] fieldNames = fieldName.split(","); 19 for (int i = 0; i < colums.size(); i++) { 20 row = sheet.createRow(i + 1); 21 //从rowIndex行开始创建行 22 Object obj = colums.get(i); 23 //循环多少列 24 for (int j = 0; j < headers.length; j++) { 25 Object value = getFieldValueByName(fieldNames[j], obj); 26 if (fieldNames[j].equals("policyBtn")) { 27 HSSFCell cell = row.createCell(j); 28 cell.setCellType(HSSFCell.CELL_TYPE_FORMULA); 29 /** 30 * 这里使用Excle的HYPERLINK()函数, 31 * 函数的参数1:要访问的地址,参数2:展示Excle上的文字, 32 * 都需要用引号包含起来,所以这里用到了转义 33 */ 34 cell.setCellFormula("HYPERLINK(" + ""https://www.baidu.com/"" + "," + ""下载")"); 35 cell.setCellStyle(linkStyle(hssfWorkbook)); 36 } else if (value != null) { 37 row.createCell(j).setCellValue(value.toString()); 38 } else { 39 row.createCell(j).setCellValue(""); 40 } 41 } 42 } 43 }
1 /** 2 * 根据对象的属性获取值 3 * 4 * @param fieldName 5 * @param obj 6 * @return 7 */ 8 private static Object getFieldValueByName(String fieldName, Object obj) { 9 String firstLetter = fieldName.substring(0, 1).toUpperCase(); 10 String getter = "get" + firstLetter + fieldName.substring(1); 11 try { 12 Method method = obj.getClass().getMethod(getter, new Class[]{}); 13 Object value = method.invoke(obj, new Object[]{}); 14 return value; 15 } catch (Exception e) { 16 ////System.out.println("属性不存在"); 17 return null; 18 } 19 }
1 /** 2 * 设置超链接等样式 3 * 4 * @param hssfWorkbook 5 * @return 6 */ 7 public static HSSFCellStyle linkStyle(HSSFWorkbook hssfWorkbook) { 8 // 生成并设置另一个样式 9 HSSFCellStyle linkStyle = hssfWorkbook.createCellStyle(); 10 //设置单元格边框 11 // linkStyle.setBorderBottom((short) 1); 12 // linkStyle.setBorderLeft((short) 1); 13 // linkStyle.setBorderRight((short) 1); 14 // linkStyle.setBorderTop((short) 1); 15 //设置单元格背景颜色 16 // linkStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index); 17 // linkStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 18 HSSFFont font = hssfWorkbook.createFont(); 19 font.setFontName(HSSFFont.FONT_ARIAL); 20 //设置字体下划线 21 font.setUnderline((byte) 1); 22 //设置字体颜色 23 font.setColor(HSSFColor.BLUE.index); 24 //设置字体 25 linkStyle.setFont(font); 26 // 生成另一个字体 27 // font.setBoldweight(Font.BOLDWEIGHT_NORMAL); 28 // 把字体应用到当前的样式 29 linkStyle.setFont(font); 30 return linkStyle; 31 }