1. 程式人生 > 程式設計 >Apache Poi 4.1.0合併單元格後,RegionUtil設定邊框無效問題解決

Apache Poi 4.1.0合併單元格後,RegionUtil設定邊框無效問題解決

背景

使用Apache Poi做Excel複雜表頭匯出,涉及表格合併,合併後呼叫RegionUtil設定邊框效果無效。如下所示

解決

查了一波資料後都是採用RegionUtil。沒辦法,採用將每個單元格設定樣式,包括合併的每個單元格。

// 第二行表頭:涉及表頭合併
row = sheet.createRow(1);
// 增加此部分程式碼,將合併的,沒有填值的也設定邊框樣式
int preColNum = 16;
for (int j = 0; j < preColNum; j++) {
	HSSFCell cell = row.createCell(j);
	cell.setCellStyle(style);
}
// 設定有值得單元樣式
for (int i = 0; i < excelHeader1.length; i++) { HSSFCell cell = row.createCell(i + preColNum); cell.setCellValue(excelHeader1[i]); cell.setCellStyle(style); sheet.autoSizeColumn(i + preColNum,true);// 自動調整寬度 } // 動態合併單元格 for (int i = 0; i < headnum1.length; i++) { sheet.autoSizeColumn(i,true); String[] temp = headnum1[i].split(","
); Integer startrow = Integer.parseInt(temp[0]); Integer overrow = Integer.parseInt(temp[1]); Integer startcol = Integer.parseInt(temp[2]); Integer overcol = Integer.parseInt(temp[3]); CellRangeAddress cra = new CellRangeAddress(startrow,overrow,startcol,overcol); sheet.addMergedRegion(cra); // 呼叫RegionUtil設定無效
// this.setRegionBorder(BorderStyle.THIN,cra,sheet); } 複製程式碼

完整程式碼

整理後,完整程式碼如下:

public class RoomReserveExportUtil {
    private void setRegionBorder(BorderStyle border,CellRangeAddress region,Sheet sheet) {
        RegionUtil.setBorderBottom(border,region,sheet);
        RegionUtil.setBorderLeft(border,sheet);
        RegionUtil.setBorderRight(border,sheet);
        RegionUtil.setBorderTop(border,sheet);
    }

    /**
     * 動態合併單元格
     * @param headNum 表頭數字,“0,2,0”  ===>  “起始行,截止行,起始列,截止列”
     * @param sheet
     */
    private void mergeCell(String[] headNum,Sheet sheet) {
        // 動態合併單元格
        for (int i = 0; i < headNum.length; i++) {
            sheet.autoSizeColumn(i,true);
            String[] temp = headNum[i].split(",");
            Integer startrow = Integer.parseInt(temp[0]);
            Integer overrow = Integer.parseInt(temp[1]);
            Integer startcol = Integer.parseInt(temp[2]);
            Integer overcol = Integer.parseInt(temp[3]);

            CellRangeAddress cra = new CellRangeAddress(startrow,overcol);
            sheet.addMergedRegion(cra);
//            this.setRegionBorder(BorderStyle.THIN,sheet);
        }
    }

    /**
     * 設定合併表格,空缺單元格樣式
     * @param row
     * @param startNum
     * @param endNum
     * @param style
     */
    private void setEmptyCellStyle(HSSFRow row,int startNum,int endNum,HSSFCellStyle style) {
        for (int j = startNum; j < endNum; j++) {
            HSSFCell cell = row.createCell(j);
            cell.setCellStyle(style);
        }
    }

    public HSSFWorkbook exportGoods(List<RoomReserveExtend> list) {
        //int titleRow = 6;//表頭標題及副標題佔6行
        //int tableBody = titleRow+1;//表頭開始
        // 宣告String陣列,並初始化元素(表頭名稱)
        //第一行表頭欄位,合併單元格時欄位跨幾列就將該欄位重複幾次
        String[] excelHeader0 = {
                "專案名稱","招標編號","招標單位","專案類別","專案區域","開標時間","評標時間","稽核狀態","招標方式","招標組織形式","招標代理機構","出席開標人員姓名(代理機構)","繳款單位(中標單位)","中標金額(元)","中標金額說明","繳款通知書時間","繳款金額(元)","換票情況","換票情況"

        };
        // “0,0”  ===>  “起始行,截止行,起始列,截止列”
        String[] headnum0 = {
                "0,0","0,1,1",2",3,3",4,4",5,5",6,6",7,7",8,8",9,9",10,10",11,11",12,12",13,13",14,14",15,15",16,20",21,22"
        };
        // 第二行表頭欄位,其中的空的雙引號是為了補全表格邊框
        String[] excelHeader1 = {
                "合計5=(1+2)","場地租賃費(1)","服務費","時間","發票號碼"
        };
        // 合併單元格
        String[] headnum1 = {
                "1,16","1,17,17",18,21",22,22"
        };

        // 第三行表頭欄位
        String[] excelHeader2 = {
                "新標準(2)","舊標準(2)","減負情況4=(3-2)"
        };

//        String[] headnum2 = {
//                "2,18",
//                "2,19,19",20,20"
//        };

        // 宣告一個工作簿
        HSSFWorkbook wb = new HSSFWorkbook();
        // 生成一個表格
        HSSFSheet sheet = wb.createSheet("專案匯出結果");

        // 生成一種樣式style
        HSSFCellStyle style = wb.createCellStyle();
        // 設定樣式
        style.setFillForegroundColor(IndexedColors.SKY_BLUE.index);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);

        // 生成一種字型
        HSSFFont font = wb.createFont();
        // 設定字型
        font.setFontName("微軟雅黑");
        // 設定字型大小
        font.setFontHeightInPoints((short) 12);
        // 在樣式中引用這種字型
        style.setFont(font);

//        // 生成標題樣式style1
//        HSSFCellStyle style1 = wb.createCellStyle();
//        // 設定樣式
//        style1.setAlignment(HorizontalAlignment.CENTER);
//        style1.setVerticalAlignment(VerticalAlignment.CENTER);
//
//        // 生成標題字型1
//        HSSFFont font1 = wb.createFont();
//        // 設定字型
//        font1.setFontName("微軟雅黑");
//        // 設定字型大小
//        font1.setFontHeightInPoints((short) 25);
//        // 字型加粗
//        font1.setBold(true);
//        // 在樣式中引用這種字型
//        style1.setFont(font1);

        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < excelHeader0.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(excelHeader0[i]);
            cell.setCellStyle(style);
            sheet.autoSizeColumn(i,true);// 根據欄位長度自動調整列的寬度
        }
        // 動態合併單元格
        this.mergeCell(headnum0,sheet);

        // 第二行表頭
        row = sheet.createRow(1);
        int preColNum = 16;
        // 設定合併單元格,空缺單元格樣式
        this.setEmptyCellStyle(row,0,preColNum,style);

        for (int i = 0; i < excelHeader1.length; i++) {
            HSSFCell cell = row.createCell(i + preColNum);
            cell.setCellValue(excelHeader1[i]);
            cell.setCellStyle(style);
            sheet.autoSizeColumn(i + preColNum,true);// 自動調整寬度
        }
        // 動態合併單元格
        this.mergeCell(headnum1,sheet);

        // 第三行表頭
        row = sheet.createRow(2);
        preColNum = 18;
        // 設定合併單元格,空缺單元格樣式
        this.setEmptyCellStyle(row,style);

        for (int i = 0; i < excelHeader2.length; i++) {
            HSSFCell cell = row.createCell(i + preColNum);
            cell.setCellValue(excelHeader2[i]);
            cell.setCellStyle(style);
            sheet.autoSizeColumn(i + preColNum,true);// 自動調整寬度
        }
        // 動態合併單元格
//        this.mergeCell(headnum2,sheet);

        // 設定合併單元格,空缺單元格樣式
        this.setEmptyCellStyle(row,21,23,style);

        for (int i = 0; i < list.size(); i++) {
            RoomReserveExtend vo = list.get(i);
            row = sheet.createRow(i + 3);

            HSSFCell cell0 = row.createCell(0);
            cell0.setCellValue(vo.getProjectName());

            HSSFCell cell1 = row.createCell(1);
            cell1.setCellValue(vo.getTenderCode());

            HSSFCell cell2 = row.createCell(2);
            cell2.setCellValue(vo.getTenderUnit());

            HSSFCell cell3 = row.createCell(3);
            cell3.setCellValue(vo.getProjectType());

            HSSFCell cell4 = row.createCell(4);
            cell4.setCellValue(vo.getArea());

            HSSFCell cell5 = row.createCell(5);
            cell5.setCellValue(vo.getApplyOpenTime());

            HSSFCell cell6 = row.createCell(6);
            cell6.setCellValue(vo.getEvalTime());

            HSSFCell cell7 = row.createCell(7);
            cell7.setCellValue(vo.getManageStatus());

            HSSFCell cell8 = row.createCell(8);
            cell8.setCellValue(vo.getTenderType());

            HSSFCell cell9 = row.createCell(9);
            cell9.setCellValue(vo.getTenderOrgType());

            HSSFCell cell10 = row.createCell(10);
            cell10.setCellValue(vo.getAgencyName());

            HSSFCell cell11 = row.createCell(11);
            cell11.setCellValue(vo.getOpenUsersShow());

            HSSFCell cell12 = row.createCell(12);
            cell12.setCellValue(vo.getAgencyName());

            HSSFCell cell13 = row.createCell(13);
            cell13.setCellValue(String.valueOf(vo.getTradePrice()));

            HSSFCell cell14 = row.createCell(14);
            cell14.setCellValue(vo.getTradePriceDesc());

            HSSFCell cell15 = row.createCell(15);
            cell15.setCellValue("");

            HSSFCell cell16 = row.createCell(16);
            cell16.setCellValue(vo.getAgencyName());

            HSSFCell cell17 = row.createCell(17);
            cell17.setCellValue(vo.getAgencyName());

            HSSFCell cell18 = row.createCell(18);
            cell18.setCellValue(vo.getAgencyName());

            HSSFCell cell19 = row.createCell(19);
            cell19.setCellValue(vo.getAgencyName());

            HSSFCell cell20 = row.createCell(20);
            cell20.setCellValue(vo.getAgencyName());

            HSSFCell cell21 = row.createCell(21);
            cell21.setCellValue(vo.getAgencyName());

            HSSFCell cell22 = row.createCell(22);
            cell22.setCellValue(vo.getAgencyName());
        }
        return wb;
    }
}
複製程式碼

最終效果

轉載請註明:溜爸 » Apache Poi 4.1.0合併單元格後,RegionUtil設定邊框無效問題解決