Go directly to the code:
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 // Create a header cell and add the corresponding content 13 String[] cells = headtitle.split("," ); 14 // Determine whether the collection is empty 15 if (policyQueryList != null ) { 16 outputColums(cells, policyQueryList , workbook, fieldName); 17 } 18 return workbook; 19 }
1 /** 2 * Set each row of data 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 // Declare a sheet 10 HSSFSheet sheet = hssfWorkbook.createSheet(); 11 // Create header 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 // How many rows to loop through String[] fieldNames = fieldName.split("," ); 19 for ( int i = 0; i < colums.size(); i++ ) { 20 row = sheet.createRow(i + 1 ); 21 // create from rowIndex Line 22 Object obj = colums.get(i); 23 // How many columns to loop for 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 //Excle's HYPERLINK() function is used here, parameter 1 of the function: the address to be accessed, parameter 2: to display the text on Excle, both are required It is enclosed in quotation marks, so the escape is used here 30 cell.setCellFormula("HYPERLINK(" + "\"https://www.baidu.com/\"" + "," + "\"download\")" ); 31 cell.setCellStyle(linkStyle(hssfWorkbook)); 32 } else if (value != null) { 33 row.createCell(j).setCellValue(value.toString()); 34 } else { 35 row.createCell(j).setCellValue(""); 36 } 37 } 38 } 39 }
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 * Set styles such as hyperlinks 3 * 4 * @param hssfWorkbook 5 * @return 6 */ 7 public static HSSFCellStyle linkStyle(HSSFWorkbook hssfWorkbook) { 8 // Generate and set another style 9 HSSFCellStyle linkStyle = hssfWorkbook. createCellStyle(); 10 // set cell border 11 // linkStyle.setBorderBottom((short) 1); 12 // linkStyle.setBorderLeft((short) 1); 13 // linkStyle.setBorderRight((short) 1); 14 // linkStyle.setBorderTop((short) 1); 15 // Set cell background color 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 // Set font underline 21 font.setUnderline(( byte ) 1 ); 22 // Set font color twenty three font.setColor(HSSFColor.BLUE.index); 24 // Set the font 25 linkStyle.setFont(font); 26 // Generate another font 27 // font.setBoldweight(Font.BOLDWEIGHT_NORMAL); 28 // Apply the font to Current style 29 linkStyle.setFont(font); 30 return linkStyle; 31 }