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

背景

使用Apache Poi作Excel複雜表頭導出,涉及表格合併,合併後調用RegionUtil設置邊框效果無效。以下所示 php

解決

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

// 第二行表頭:涉及表頭合併
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);
}
複製代碼

完整代碼

整理後,完整代碼以下:apache

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

    /** * 動態合併單元格 * @param headNum 表頭數字,「0,2,0,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, overrow, startcol, overcol);
            sheet.addMergedRegion(cra);
// this.setRegionBorder(BorderStyle.THIN, cra, 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,2,0,0」 ===> 「起始行,截止行,起始列,截止列」
        String[] headnum0 = {
                "0,2,0,0",
                "0,2,1,1",
                "0,2,2,2",
                "0,2,3,3",
                "0,2,4,4",
                "0,2,5,5",
                "0,2,6,6",
                "0,2,7,7",
                "0,2,8,8",
                "0,2,9,9",
                "0,2,10,10",
                "0,2,11,11",
                "0,2,12,12",
                "0,2,13,13",
                "0,2,14,14",
                "0,2,15,15",
                "0,0,16,20",
                "0,0,21,22"
        };
        // 第二行表頭字段,其中的空的雙引號是爲了補全表格邊框
        String[] excelHeader1 = {
                "合計5=(1+2)",
                "場地租賃費(1)",
                "服務費", "服務費", "服務費",
                "時間",
                "發票號碼"
        };
        // 合併單元格
        String[] headnum1 = {
                "1,2,16,16",
                "1,2,17,17",
                "1,1,18,20",
                "1,2,21,21",
                "1,2,22,22"
        };

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

// String[] headnum2 = {
// "2,2,18,18",
// "2,2,19,19",
// "2,2,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, 0, preColNum, 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設置邊框無效問題解決數組

相關文章
相關標籤/搜索