hutool poi 分頁導出,格式設置


 1     @RequestMapping(value = "/exportXls")
 2     public void exportXls(HttpServletRequest request, DnbUser dnbUser, HttpServletResponse response) throws IOException {
 3 
 4         ExcelWriter writer = ExcelUtil.getWriter();
 5 
 6         List<DnbUser> dnbUsers = dnbUserService.list();
 7 
 8         //每個sheet頁的數據量
 9         int num = 4;
10         //總數據條數
11         int size = dnbUsers.size();
12         //分sheet頁
13         int sheetNum = size / num + 1;
14         // 需要設置一下
15         writer.renameSheet(0, "Sheet0");
16         int startIndex = 0;
17         int endIndex = 0;
18         int styleEndIndex = num;
19         for (int i = 0; i < sheetNum; i++) {
20             //指定sheet頁
21             writer.setSheet(i);
22             //自定義Bean中key的別名,為表頭設置列名
23             writer.addHeaderAlias("no", "編號");
24             if (sheetNum == 1) {
25                 startIndex = 0;
26                 endIndex = size;
27                 styleEndIndex = size;
28             } else if (i == sheetNum - 1) {
29                 startIndex = i * num;
30                 endIndex = size;
31                 styleEndIndex = size-i * num;
32             } else {
33                 startIndex = i * num;
34                 endIndex = (i + 1) * num;
35                 styleEndIndex = 4;
36             }
37             writer.write(dnbUsers.subList(startIndex, endIndex), true);
38             //每個新的sheet頁都是行數都是從0開始的
39             generateStyle(writer, i, styleEndIndex,dnbUsers,startIndex);
40         }
41         response.setContentType("application/vnd.ms-excel;charset=utf-8");
42         response.setHeader("Content-Disposition", "attachment;filename=test.xls");
43         ServletOutputStream out = response.getOutputStream();
44 
45         writer.flush(out, true);
46         writer.close();
47         IoUtil.close(out);
48     }
49 
50     private void generateStyle(ExcelWriter writer, int sheetNo, int styleEndIndex,List<DnbUser> dnbUsers ,int dataStartIndex) {
51 //        Workbook wb = writer.getWorkbook();
52 //        Font font = writer.createFont();
53 //        font.setBold(true);
54 //        font.setColor(Font.COLOR_RED);
55 //        font.setItalic(true);
56 //
57 //        StyleSet style = writer.getStyleSet();
58 //        CellStyle cellStyle = style.getCellStyle();
59 //        cellStyle.setLeftBorderColor((short) 999);
60 //        cellStyle.setFont(font);
61 
62         Workbook wb = writer.getWorkbook();
63         CellStyle cellStyle = wb.createCellStyle();
64         cellStyle.setAlignment(HorizontalAlignment.CENTER);
65         cellStyle.setBorderBottom((short)1);
66         cellStyle.setBorderLeft((short)1);
67         cellStyle.setBorderRight((short)1);
68         cellStyle.setBorderTop((short)1);
69         cellStyle.setFillPattern((short)1);
70         cellStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.RED.getIndex());
71 
72         Sheet sheet = wb.getSheetAt(sheetNo);
73         for(int i = 1; i <= styleEndIndex; ++i,dataStartIndex++) {
74             if(dnbUsers.get(dataStartIndex).getNo().equalsIgnoreCase("1")){
75                sheet.getRow(i).getCell(13).setCellStyle(cellStyle);
76             }
77         }
78     }

 

導出結果:每sheet頁4行,特定單元格設置格式。

屬於測試數據,數據不嚴謹。hutool的依賴,記得加入pom文件。

 


免責聲明!

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



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