使用POI導出Excel(二)-利用模板

 

1、基本操做見: 使用POI導出Excelhtml

 

2、本次功能需求ide

  給了一個模板,裏面有6個sheet頁,每頁裏面都須要填充相應的數據。如圖:函數

 

 

3、需求分析測試

  一、分了6個sheet頁,每頁的數據都不同,首先代碼裏要得到它們的數據,而後6個sheet頁只能一個個填進去,無法批量操做。this

  二、若是直接代碼建立Excel並畫表格樣式和數據,那麼工程量將會很大,並且會致使代碼很亂。我採用的方法是把模板保存到項目裏,再經過路徑獲取到該模板文件,把其內容所有複製到新建立的Excel中,再往裏填充數據。spa

  三、注意到第一張圖中有總計這一行,個人建議是先在代碼中計算出相應的數據,再填充,我也是這樣作的。固然,個人第一想法是給它設置函數公式,可是後來我並無這麼作。excel

  四、從第二張圖中能夠看得出要填充的數據的行是不固定的,除非規定了只填充前幾條數據。而在十一、12行中的數據是已經有了的,當填充的數據太多,就會覆蓋掉那行數據。有兩種方法解決,一是把它獲取到,先填充數據覆蓋掉,再在最後一條數據的下一行把它再填充回去。二是根據數據的總條數經過代碼往中間插入空白行。兩種方法均可行,我採用了第二種。code

 

4、上代碼orm

 1     public void exportAll() {
 2         OutputStream out = null;
 3         try {
 4             // 獲取數據
 5             List<BaseTalentFlowAnalysisGridVo> talentFlows = this.talentFlowQuestionnaireService.listAnalysis();
 6             List<BaseBasicSituationAnalysisGridVo> basicSituations = this.basicSituationService.listAnalysis();
 7             List<BaseTalentInputAnalysisGridVo> talentInputs = this.talentInputService.listAnalysis();
 8             List<BaseTalentDemandAnalysisGridVo> talentDemands = this.talentDemandService.listAnalysis();
 9             List<BaseHighLevelTalentsRosterAnalysisGridVo> talentRosters = this.rosterService.listAnalysis();
10             Map<String, Object> results = new HashMap<>();
11             results.put("talentFlows", talentFlows);
12             results.put("basicSituations", basicSituations);
13             results.put("talentInputs", talentInputs);
14             results.put("talentDemands", talentDemands);
15             results.put("talentRosters", talentRosters);
16 
17             // 拿到模板文件
18             String path = ServletActionContext.getServletContext().getRealPath("/");
19             String filePath = path + "\\resources\\temp\\人才統計報表模板.xls";
20             FileInputStream tps = new FileInputStream(new File(filePath));
21             final HSSFWorkbook tpWorkbook = new HSSFWorkbook(tps);
22             out = response.getOutputStream();
23             response.reset();
24             response.setHeader("content-disposition",
25                     "attachment;filename=" + new String(("人才統計報表").getBytes("gb2312"), "ISO8859-1") + ".xls");
26             response.setContentType("APPLICATION/msexcel");
27             // 新建一個Excel的工做空間
28             HSSFWorkbook workbook = new HSSFWorkbook();
29             // 把模板複製到新建的Excel
30             workbook = tpWorkbook;
31             // 填充數據
32             this.excelService.addData(workbook, results);
33             // 輸出Excel內容,生成Excel文件
34             workbook.write(out);
35         } catch (final IOException e) {
36             LOGGER.error(e);
37         } catch (final IllegalArgumentException e) {
38             LOGGER.error(e);
39         } catch (final Exception e) {
40             LOGGER.error(e);
41         } finally {
42             try {
43                 // 最後記得關閉輸出流
44                 response.flushBuffer();
45                 if (out != null) {
46                     out.flush();
47                     out.close();
48                 }
49             } catch (final IOException e) {
50                 LOGGER.error(e);
51             }
52         }
53     }

 

 

 

 1     /**
 2      * 第二個sheet頁:人才流動狀況表
 3      * 
 4      * @param workbook
 5      * @param talentFlows
 6      */
 7     private void addTalentFlows(HSSFWorkbook workbook, List<BaseTalentFlowAnalysisGridVo> talentFlows) {
 8         // 獲取第二個sheet頁
 9         Sheet talentFlowSheet = workbook.getSheetAt(1);
10         Row talentFlowRow = talentFlowSheet.getRow(0);
11         // 聲明總計的那幾個數據
12         Integer totalLastYearTotal = 0;
13         Integer totalHighLevelTalent = 0;
14         Integer totalUndergraduateAndGraduate = 0;
15         Integer totalCollegeStudents = 0;
16         Integer totalSocialTalent = 0;
17         Integer totalMilitaryTransferCadres = 0;
18         Integer totalReturnees = 0;
19         Integer totalRetirement = 0;
20         Integer totalResignation = 0;
21         Integer totalDismiss = 0;
22         Integer totalOther = 0;
23         Integer totalAverageIncrease = 0;
24         Integer totalAverageReduction = 0;
25         // 循環數據
26         for (BaseTalentFlowAnalysisGridVo baseTalentFlowAnalysisGridVo : talentFlows) {
27             // 循環行
28             for (int tr = 4; tr < 11; tr++) {
29                 talentFlowRow = talentFlowSheet.getRow(tr);
30                 // 當數據的項目字段和行的第一列內容相等,則把該條數據填入該行
31                 if (talentFlowRow.getCell(0).getStringCellValue()
32                         .equals(baseTalentFlowAnalysisGridVo.getFkProjectName())) {
33                     talentFlowRow.getCell(1).setCellValue(baseTalentFlowAnalysisGridVo.getLastYearTotal());
34                     // 給總計作計算
35                     totalLastYearTotal += baseTalentFlowAnalysisGridVo.getLastYearTotal();
36                     talentFlowRow.getCell(2).setCellValue(baseTalentFlowAnalysisGridVo.getHighLevelTalent());
37                     totalHighLevelTalent += baseTalentFlowAnalysisGridVo.getHighLevelTalent();
38                     talentFlowRow.getCell(3).setCellValue(baseTalentFlowAnalysisGridVo.getUndergraduateAndGraduate());
39                     totalUndergraduateAndGraduate += baseTalentFlowAnalysisGridVo.getUndergraduateAndGraduate();
40                     talentFlowRow.getCell(4).setCellValue(baseTalentFlowAnalysisGridVo.getCollegeStudents());
41                     totalCollegeStudents += baseTalentFlowAnalysisGridVo.getCollegeStudents();
42                     talentFlowRow.getCell(5).setCellValue(baseTalentFlowAnalysisGridVo.getSocialTalent());
43                     totalSocialTalent += baseTalentFlowAnalysisGridVo.getSocialTalent();
44                     talentFlowRow.getCell(6).setCellValue(baseTalentFlowAnalysisGridVo.getMilitaryTransferCadres());
45                     totalMilitaryTransferCadres += baseTalentFlowAnalysisGridVo.getMilitaryTransferCadres();
46                     talentFlowRow.getCell(7).setCellValue(baseTalentFlowAnalysisGridVo.getReturnees());
47                     totalReturnees += baseTalentFlowAnalysisGridVo.getReturnees();
48                     talentFlowRow.getCell(8).setCellValue(baseTalentFlowAnalysisGridVo.getRetirement());
49                     totalRetirement += baseTalentFlowAnalysisGridVo.getRetirement();
50                     talentFlowRow.getCell(9).setCellValue(baseTalentFlowAnalysisGridVo.getResignation());
51                     totalResignation += baseTalentFlowAnalysisGridVo.getResignation();
52                     talentFlowRow.getCell(10).setCellValue(baseTalentFlowAnalysisGridVo.getDismiss());
53                     totalDismiss += baseTalentFlowAnalysisGridVo.getDismiss();
54                     talentFlowRow.getCell(11).setCellValue(baseTalentFlowAnalysisGridVo.getOther());
55                     totalOther += baseTalentFlowAnalysisGridVo.getOther();
56                     talentFlowRow.getCell(12).setCellValue(baseTalentFlowAnalysisGridVo.getAverageIncrease());
57                     totalAverageIncrease += baseTalentFlowAnalysisGridVo.getAverageIncrease();
58                     talentFlowRow.getCell(13).setCellValue(baseTalentFlowAnalysisGridVo.getAverageReduction());
59                     totalAverageReduction += baseTalentFlowAnalysisGridVo.getAverageReduction();
60                     break;
61                 }
62             }
63         }
64         // 給總計行填充數據
65         talentFlowRow = talentFlowSheet.getRow(11);
66         talentFlowRow.getCell(1).setCellValue(totalLastYearTotal);
67         talentFlowRow.getCell(2).setCellValue(totalHighLevelTalent);
68         talentFlowRow.getCell(3).setCellValue(totalUndergraduateAndGraduate);
69         talentFlowRow.getCell(4).setCellValue(totalCollegeStudents);
70         talentFlowRow.getCell(5).setCellValue(totalSocialTalent);
71         talentFlowRow.getCell(6).setCellValue(totalMilitaryTransferCadres);
72         talentFlowRow.getCell(7).setCellValue(totalReturnees);
73         talentFlowRow.getCell(8).setCellValue(totalRetirement);
74         talentFlowRow.getCell(9).setCellValue(totalResignation);
75         talentFlowRow.getCell(10).setCellValue(totalDismiss);
76         talentFlowRow.getCell(11).setCellValue(totalOther);
77         talentFlowRow.getCell(12).setCellValue(totalAverageIncrease);
78         talentFlowRow.getCell(13).setCellValue(totalAverageReduction);
79     }

 

 

 1     /**
 2      * 第四個sheet頁:人才需求狀況調查表
 3      * 
 4      * @param workbook
 5      * @param talentFlows
 6      */
 7     private void addalentDemands(HSSFWorkbook workbook, List<BaseTalentDemandAnalysisGridVo> talentDemands)
 8             throws IllegalArgumentException, IllegalAccessException {
 9         Sheet talentDemandSheet = workbook.getSheetAt(3);
10         Row talentDemandRow = talentDemandSheet.getRow(4);
11         // 若是數據大於模板中的行數,插入行並複製第一行數據的格式
12         if (talentDemands.size() > 5) {
13             // 插入行,5是模板中已有的行數
14             talentDemandSheet.shiftRows(5, talentDemandSheet.getLastRowNum(), talentDemands.size() - 5, true, false);
15             Row sourceRow = talentDemandSheet.getRow(4);
16             for (int i = 0; i < talentDemands.size() - 5; i++) {
17                 Row newRow = talentDemandSheet.createRow(4 + i + 1);
18                 newRow.setHeight(sourceRow.getHeight());
19                 for (int j = 0; j < sourceRow.getLastCellNum(); j++) {
20                     Cell templateCell = sourceRow.getCell(j);
21                     if (templateCell != null) {
22                         Cell newCell = newRow.createCell(j);
23                         copyCell(templateCell, newCell);
24                     }
25                 }
26             }
27         }
28         // 填充數據
29         for (int i = 0; i < talentDemands.size(); i++) {
30             talentDemandRow = talentDemandSheet.getRow(4 + i);
31             talentDemandRow.getCell(0).setCellValue(talentDemands.get(i).getPositionTitle());
32             talentDemandRow.getCell(2).setCellValue(talentDemands.get(i).getDemand());
33             talentDemandRow.getCell(3).setCellValue(talentDemands.get(i).getAge());
34             talentDemandRow.getCell(4).setCellValue(talentDemands.get(i).getFkAcademicDegreeName());
35             talentDemandRow.getCell(5).setCellValue(talentDemands.get(i).getTechnicalTitles());
36             talentDemandRow.getCell(6).setCellValue(talentDemands.get(i).getProfession());
37             talentDemandRow.getCell(7).setCellValue(talentDemands.get(i).getFkTalentCategoryName());
38             talentDemandRow.getCell(8).setCellValue(talentDemands.get(i).getFkServiceFormName());
39             talentDemandRow.getCell(9).setCellValue(talentDemands.get(i).getProvide());
40             talentDemandRow.getCell(10).setCellValue(talentDemands.get(i).getOtherCases());
41             talentDemandRow.getCell(11).setCellValue(talentDemands.get(i).getFkIntentionToChooseName());
42         }
43     }

 

 

5、缺點:一手爛代碼,應該給對象設置對應的中文註釋,和模板中的列頭同樣,而後再經過循環填充數據。這樣一個個填上去太傻了。htm

 

6、經過實驗測試,在getCell的時候,若是那是個合併的單元格,那麼該單元格的數據存在了左上角,其餘的格內容爲空。

相關文章
相關標籤/搜索