1. 程式人生 > >HSSFWorkbook(poi)匯出excel表格

HSSFWorkbook(poi)匯出excel表格

本文與另一篇文章關聯:

csv格式匯出excel報表

其中:

String accountDate   入參(日期)
AccountInfoEntityResp accountInfoEntityResp  匯出的xml報文內容(轉換成obj物件)

xml報文解析見另一篇:xml報文解析

HttpServletRequest request  

HttpServletResponse response  

主要核心程式碼如下:

/**
     * 匯出exc方法
     */
    public void exportExcel(String accountDate, AccountInfoEntityResp accountInfoEntityResp,HttpServletRequest request,HttpServletResponse response) throws IOException {

        // 宣告一個工作簿
        HSSFWorkbook wb = new HSSFWorkbook();
        // 宣告一個工作單並命名
        HSSFSheet sheet = wb.createSheet("accountInfo");
        // 建立標題樣式
        HSSFCellStyle headerStyle = (HSSFCellStyle) wb.createCellStyle();
        headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
        headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直對齊居中
        headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下邊框
        headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
        headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
        headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
        //建立字型
        HSSFFont header_Font = (HSSFFont) wb.createFont();
        header_Font.setFontName("微軟雅黑");
        header_Font.setFontHeightInPoints((short) 15);
        headerStyle.setFont(header_Font);

        // 建立單元格樣式
        HSSFCellStyle cell_Style = (HSSFCellStyle) wb.createCellStyle();// 設定字型樣式
        cell_Style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cell_Style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直對齊居中
        cell_Style.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下邊框
        cell_Style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
        cell_Style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
        cell_Style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
        cell_Style.setWrapText(true); // 設定為自動換行
        HSSFFont cell_Font = (HSSFFont) wb.createFont();
        cell_Font.setFontName("微軟雅黑");
        cell_Font.setFontHeightInPoints((short) 12);
        cell_Style.setFont(cell_Font);

        //建立自由樣式
        HSSFCellStyle free_Style = (HSSFCellStyle) wb.createCellStyle();// 設定字型樣式
        free_Style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        free_Style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFFont free_Font = (HSSFFont) wb.createFont();
        free_Font.setFontName("微軟雅黑");
        free_Font.setFontHeightInPoints((short) 12);
        free_Style.setFont(free_Font);

        //【結算場次列表】單元格行數:SttlList->SttlInf集合size大小+1(標題行)
        int sttlInfCount = accountInfoEntityResp.getSttlList().size()+1;
        //【批次列表】單元格行數:SttlList->SttlInf->BatchList->BatchInf的集合大小+1(標題行)
        int batchInfCount = 1;
        //【分項列表】單元格行數:SttlList->SttlInf->BatchList->BatchInf->SubItemList->SubItemInf的集合大小+1(標題行)
        int subItemInfCount = 0;
        if(accountInfoEntityResp.getSttlList() != null){
            for(int i = 0; i< accountInfoEntityResp.getSttlList().size() ;i++){
                //批次列表大小
                batchInfCount = batchInfCount + accountInfoEntityResp.getSttlList().get(i).getBatchList().size();
            }
        }
        //結算場次列表+批次列表+分項列表 =明細單元格行數
        int detailCount = sttlInfCount + batchInfCount + subItemInfCount ;

        //1.1建立合併單元格物件
        //頭部"賬務日期"
        CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,7);//起始行,結束行,起始列,結束列
        //第二行
        CellRangeAddress callRangeAddress1 = new CellRangeAddress(1,1,0,1);//起始行,結束行,起始列,結束列
        CellRangeAddress callRangeAddress1_2 = new CellRangeAddress(1,1,2,3);//起始行,結束行,起始列,結束列
        CellRangeAddress callRangeAddress1_3 = new CellRangeAddress(1,1,4,5);//起始行,結束行,起始列,結束列
        CellRangeAddress callRangeAddress1_4 = new CellRangeAddress(1,1,6,7);//起始行,結束行,起始列,結束列
        //第三行
        CellRangeAddress callRangeAddress2 = new CellRangeAddress(2,2,0,1);//起始行,結束行,起始列,結束列
        CellRangeAddress callRangeAddress2_3 = new CellRangeAddress(2,2,2,3);//起始行,結束行,起始列,結束列
        CellRangeAddress callRangeAddress2_4 = new CellRangeAddress(2,2,4,5);//起始行,結束行,起始列,結束列
        CellRangeAddress callRangeAddress2_5 = new CellRangeAddress(2,2,6,7);//起始行,結束行,起始列,結束列
        //明細
        CellRangeAddress callRangeAddress3 = new CellRangeAddress(3,3,0,7);//起始行,結束行,起始列,結束列
        //結算場次列表
        CellRangeAddress callRangeAddress4 = new CellRangeAddress(4,4,0,7);//起始行,結束行,起始列,結束列
        //批次列表
        CellRangeAddress callRangeAddress5 = new CellRangeAddress(sttlInfCount+4+1,sttlInfCount+4+1,0,7);//起始行,結束行,起始列,結束列
        //分項列表
        CellRangeAddress callRangeAddress6 = new CellRangeAddress(sttlInfCount+4+1+batchInfCount+1,sttlInfCount+4+1+batchInfCount+1,0,7);//起始行,結束行,起始列,結束列

        //2.1載入合併單元格物件
        sheet.addMergedRegion(callRangeAddress);
        sheet.addMergedRegion(callRangeAddress1);
        sheet.addMergedRegion(callRangeAddress1_2);
        sheet.addMergedRegion(callRangeAddress1_3);
        sheet.addMergedRegion(callRangeAddress1_4);
        sheet.addMergedRegion(callRangeAddress2);
        sheet.addMergedRegion(callRangeAddress2_3);
        sheet.addMergedRegion(callRangeAddress2_4);
        sheet.addMergedRegion(callRangeAddress2_5);
        sheet.addMergedRegion(callRangeAddress3);
        sheet.addMergedRegion(callRangeAddress4);
        sheet.addMergedRegion(callRangeAddress5);
        sheet.addMergedRegion(callRangeAddress6);

        //3.1建立頭標題行;並且設定頭標題
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        //載入單元格樣式
        cell.setCellStyle(headerStyle);
        cell.setCellValue("財務日期:"+accountDate);
        //4.1建立第二行
        HSSFRow rower = sheet.createRow(1);
        //結算總筆數
        HSSFCell celler_1 = rower.createCell(0);
        //借方金額
        HSSFCell celler_2 = rower.createCell(2);
        //貸方金額
        HSSFCell celler_3 = rower.createCell(4);
        //結算場次明細
        HSSFCell celler_4 = rower.createCell(6);
        //載入單元格樣式
        celler_1.setCellStyle(cell_Style);
        celler_1.setCellValue("結算總筆數");
        celler_2.setCellStyle(cell_Style);
        celler_2.setCellValue("借方金額");
        celler_3.setCellStyle(cell_Style);
        celler_3.setCellValue("貸方金額");
        celler_4.setCellStyle(cell_Style);
        celler_4.setCellValue("結算場次明細");
        //建立第三行
        HSSFRow rowsan = sheet.createRow(2);
        //結算總筆數
        HSSFCell cellsan_1 = rowsan.createCell(0);
        //借方金額
        HSSFCell cellsan_2 = rowsan.createCell(2);
        //貸方金額
        HSSFCell cellsan_3 = rowsan.createCell(4);
        //結算場次明細
        HSSFCell cellsan_4 = rowsan.createCell(6);
        //載入單元格樣式
        cellsan_1.setCellStyle(cell_Style);
        cellsan_1.setCellValue(accountInfoEntityResp.getSttlCntNb());
        cellsan_2.setCellStyle(cell_Style);
        String tmpVal = String.valueOf(accountInfoEntityResp.getDebitCntAmt());
        cellsan_2.setCellValue(tmpVal);
        cellsan_3.setCellStyle(cell_Style);
        String tmpVal2 = String.valueOf(accountInfoEntityResp.getCreditCntAmt());
        cellsan_3.setCellValue(tmpVal2);
        cellsan_4.setCellStyle(cell_Style);
        cellsan_4.setCellValue("");

        //建立明細標題、結算場次列表標題、批次列表標題、分項列表標題
        //第四行:明細
        HSSFRow rowsi = sheet.createRow(3);
        HSSFCell cellsi = rowsi.createCell(0);
        //載入單元格樣式
        cellsi.setCellStyle(cell_Style);
        cellsi.setCellValue("明細");
        //第五行:結算場次列表
        HSSFRow rowwu = sheet.createRow(4);
        HSSFCell cellwu = rowwu.createCell(0);
        //載入單元格樣式
        cellwu.setCellStyle(cell_Style);
        cellwu.setCellValue("結算場次列表");
        //第六行:結算場次列表-標題欄
        HSSFRow rowsix = sheet.createRow(5);
        HSSFCell cellsix_1 = rowsix.createCell(0);
        HSSFCell cellsix_2 = rowsix.createCell(1);
        HSSFCell cellsix_3 = rowsix.createCell(2);
        //載入單元格樣式
        cellsix_1.setCellStyle(cell_Style);
        cellsix_1.setCellValue("報文標識號");
        cellsix_2.setCellStyle(cell_Style);
        cellsix_2.setCellValue("場次借貸標識");
        cellsix_3.setCellStyle(cell_Style);
        cellsix_3.setCellValue("場次金額");
        //第7行:批次列表
        HSSFRow rowseven = sheet.createRow(sttlInfCount+4+1);
        HSSFCell cellseven = rowseven.createCell(0);
        //載入單元格樣式
        cellseven.setCellStyle(cell_Style);
        cellseven.setCellValue("批次列表");
        //第8行:批次列表-->標題欄
        HSSFRow rowsegiht = sheet.createRow(sttlInfCount+4+2);
        HSSFCell cellegiht_1 = rowsegiht.createCell(0);
        HSSFCell cellegiht_2 = rowsegiht.createCell(1);
        HSSFCell cellegiht_3 = rowsegiht.createCell(2);
        HSSFCell cellegiht_4 = rowsegiht.createCell(3);
        //載入單元格樣式
        cellegiht_1.setCellStyle(cell_Style);
        cellegiht_1.setCellValue("報文標識號");
        cellegiht_2.setCellStyle(cell_Style);
        cellegiht_2.setCellValue("批次號");
        cellegiht_3.setCellStyle(cell_Style);
        cellegiht_3.setCellValue("批次借貸標識");
        cellegiht_4.setCellStyle(cell_Style);
        cellegiht_4.setCellValue("批次金額");
        //第9行:分項列表
        HSSFRow rowba = sheet.createRow(sttlInfCount+4+1+batchInfCount+1);
        HSSFCell cellba = rowba.createCell(0);
        //載入單元格樣式
        cellba.setCellStyle(cell_Style);
        cellba.setCellValue("分項列表");
        //第10行:分項列表—>標題欄
        HSSFRow rowsten = sheet.createRow(sttlInfCount+4+1+batchInfCount+2);
        HSSFCell cellten_1 = rowsten.createCell(0);
        HSSFCell cellten_2 = rowsten.createCell(1);
        HSSFCell cellten_3 = rowsten.createCell(2);
        HSSFCell cellten_4 = rowsten.createCell(3);
        HSSFCell cellten_5 = rowsten.createCell(4);
        HSSFCell cellten_6 = rowsten.createCell(5);
        HSSFCell cellten_7 = rowsten.createCell(6);
        HSSFCell cellten_8 = rowsten.createCell(7);
        //載入單元格樣式
        cellten_1.setCellStyle(cell_Style);
        cellten_1.setCellValue("批次號");
        cellten_2.setCellStyle(cell_Style);
        cellten_2.setCellValue("業務型別");
        cellten_3.setCellStyle(cell_Style);
        cellten_3.setCellValue("銀行金額機構標識");
        cellten_4.setCellStyle(cell_Style);
        cellten_4.setCellValue("賬戶型別");
        cellten_5.setCellStyle(cell_Style);
        cellten_5.setCellValue("分項借方發生額");
        cellten_6.setCellStyle(cell_Style);
        cellten_6.setCellValue("分項借方發生筆數");
        cellten_7.setCellStyle(cell_Style);
        cellten_7.setCellValue("分項貸方發生額");
        cellten_8.setCellStyle(cell_Style);
        cellten_8.setCellValue("分項貸方發生筆數");

        //結算場次列表、批次列表資料、分項列表資料
        List<SttlInf> sttlList = new ArrayList<SttlInf>();
        if(accountInfoEntityResp.getSttlList() != null ){
            sttlList = accountInfoEntityResp.getSttlList();
        }
        //5.操作單元格;將列表資料寫入excel
        if(sttlList != null){
            //結算場次列表資料
            //批次列表迴圈計數器
            int batInfCount = 0;
            //分項列表迴圈計數器
            int subItInfCount = 0;
            for(int j=0; j< sttlList.size(); j++)
            {
                HSSFRow row3 = sheet.createRow(j+6);
                HSSFCell cell0 = row3.createCell(0);
                cell0.setCellStyle(cell_Style);
                cell0.setCellValue(sttlList.get(j).getSttlReptFlg());
                HSSFCell cell1 = row3.createCell(1);
                cell1.setCellStyle(cell_Style);
                cell1.setCellValue(DCFlag(sttlList.get(j).getSttlDCFlg()));
                HSSFCell cell2 = row3.createCell(2);
                cell2.setCellStyle(cell_Style);
                cell2.setCellValue(String.valueOf(sttlList.get(j).getSttlAmt()));

                List<BatchInf> BatchList = new ArrayList<BatchInf>();
                if(sttlList.get(j).getBatchList() != null){
                    BatchList = sttlList.get(j).getBatchList();
                }
                if(BatchList != null){
                    //批次列表資料
                    for(int i = 0; i< BatchList.size(); i++){
                        batInfCount++;
                        HSSFRow row4 = sheet.createRow(sttlList.size()+6+1+batInfCount);
                        HSSFCell cell3 = row4.createCell(0);
                        cell3.setCellStyle(cell_Style);
                        cell3.setCellValue(sttlList.get(j).getSttlReptFlg());
                        HSSFCell cell4 = row4.createCell(1);
                        cell4.setCellStyle(cell_Style);
                        cell4.setCellValue(BatchList.get(i).getBatchId());
                        HSSFCell cell5 = row4.createCell(2);
                        cell5.setCellStyle(cell_Style);
                        cell5.setCellValue(DCFlag(BatchList.get(i).getBatchDCFlg()));
                        HSSFCell cell6 = row4.createCell(3);
                        cell6.setCellStyle(cell_Style);
                        cell6.setCellValue(String.valueOf(BatchList.get(i).getBatchNetAmt()));

                        List<SubItemInf> SubItemList = new ArrayList<SubItemInf>();
                        if(BatchList.get(i).getSubItemList() != null){
                            SubItemList = BatchList.get(i).getSubItemList();
                        }
                        //分項列表資料
                        if(SubItemList != null) {
                            for (int f = 0; f < SubItemList.size(); f++) {
                                subItInfCount++;
                                HSSFRow row5 = sheet.createRow((sttlInfCount+4+1+batchInfCount+2)+subItInfCount);
                                HSSFCell cell7 = row5.createCell(0);
                                cell7.setCellStyle(cell_Style);
                                cell7.setCellValue(BatchList.get(i).getBatchId());
                                String SubItemInf = SubItemList.get(f).getSubItemInf().replace("CNY","");
                                String[] subArray = {};
                                subArray = SubItemInf.split("\\|");
                                HSSFCell cell8 = row5.createCell(1);
                                cell8.setCellStyle(cell_Style);
                                cell8.setCellValue(busiType(subArray[0]));
                                HSSFCell cell9 = row5.createCell(2);
                                cell9.setCellStyle(cell_Style);
                                cell9.setCellValue(subArray[1]);
                                HSSFCell cell10 = row5.createCell(3);
                                cell10.setCellStyle(cell_Style);
                                cell10.setCellValue(accountType(subArray[2]));
                                HSSFCell cell11 = row5.createCell(4);
                                cell11.setCellStyle(cell_Style);
                                cell11.setCellValue(subArray[3]);
                                HSSFCell cell12 = row5.createCell(5);
                                cell12.setCellStyle(cell_Style);
                                cell12.setCellValue(subArray[4]);
                                HSSFCell cell13 = row5.createCell(6);
                                cell13.setCellStyle(cell_Style);
                                cell13.setCellValue(subArray[5]);
                                HSSFCell cell14 = row5.createCell(7);
                                cell14.setCellStyle(cell_Style);
                                cell14.setCellValue(subArray[6]);
                            }
                        }
                    }
                }
            }
        }
        String downFilename = "accountInfo.xls";
        // 獲取檔案的MIME型別:
       ServletContext servletContext = request.getServletContext();
        String contentType = servletContext.getMimeType(downFilename);
        // 將MIME型別放入響應
        response.setContentType(contentType);
        // 瀏覽器型別
        String agent = request.getHeader("user-agent");
        // 獲取附件的名字和下載方式
        String contentDisposition = "attachment;filename=" + downFilename;
        // 將附件名字和下載方式放入響應頭資訊中
        response.setHeader("Content-Disposition", contentDisposition);
        // 告訴瀏覽器用什麼軟體可以開啟此檔案
        /*response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下載檔案的預設名稱
        response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(downFilename, "utf-8"));*/
        ServletOutputStream out = response.getOutputStream();
        wb.write(out);
        out.close();
     }

最終匯出excel表格如下: