有關Java POI導出excel表格中,單元格合並之后顯示不全的解決方法。


我在table變換excel之后發現合並的單元格不能顯示全部邊框,在網上找了半天解決方案,終於解決了。具體解決代碼如下;

/**
     * 設置合並單元格的邊框樣式
     * 
     * @param sheet 當前表格
     * @param region  要合並的元素
     * @param cs  設置的樣式(使用未合並的單元格的樣式即可)
     */
    public static void setRegionStyle(HSSFSheet sheet, CellRangeAddress region, HSSFCellStyle cs) {
     for (int i = region.getFirstRow(); i <= region.getLastRow(); i++) {
      HSSFRow row = HSSFCellUtil.getRow(i, sheet);
      for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) {
       HSSFCell cell = HSSFCellUtil.getCell(row, (short) j);
       cell.setCellStyle(cs);
      }
     }
    }

調用方法

setRegionStyle(sheet, new CellRangeAddress(cell.getFirstRow(), cell.getLastRow(), cell.getFirstCol(), cell.getLastCol()),style);

new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
firstRow 開始行
lastRow 結束行
firstCol 開始列
lastCol 結束列


免責聲明!

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



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