使用poi導出時設置某個字段為外部超鏈接,例如打開百度


直接上代碼:

 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     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM