[英]Add border to merged cells in excel Apache poi java.?
我正在使用 Apache POI,我需要在一系列單元格或合並區域中放置邊框。 我正在合並三行五列的單元格。 但我無法為其添加邊框。 那么我該怎么做呢?
我的解決方案是按位置合並單元格,然后創建一個單元格(引用合並單元格的第一個塊)來分配一個值,然后通過HSSFRegionUtil設置邊框
// Merges the cells
CellRangeAddress cellRangeAddress = new CellRangeAddress(start, start, j, j + 1);
sheet.addMergedRegion(cellRangeAddress);
// Creates the cell
Cell cell = CellUtil.createCell(row, j, entry.getKey());
// Sets the borders to the merged cell
HSSFRegionUtil.setBorderTop(CellStyle.BORDER_MEDIUM, cellRangeAddress, sheet, workbook);
HSSFRegionUtil.setBorderLeft(CellStyle.BORDER_MEDIUM, cellRangeAddress, sheet, workbook);
HSSFRegionUtil.setBorderRight(CellStyle.BORDER_MEDIUM, cellRangeAddress, sheet, workbook);
HSSFRegionUtil.setBorderBottom(CellStyle.BORDER_THIN, cellRangeAddress, sheet, workbook);
您會發現 RegionUtil 類對於設置一系列單元格的邊框很有用。看這里:
首先,很高興知道您要創建哪種工作表格式。 因為在 HSSF 中合並空單元格是完全正常的,而 XSSF 會創建格式錯誤的文件,導致在 Microsoft EXCEL 中打開它時出錯。 在這兩種情況下,樣式的行為往往相同。 您需要為要合並的每個單元格分配相同的樣式(在您的案例樣式中包括邊框)。 我的建議是創建一個函數來檢查和更正合並區域中的所有單元格,設置樣式。 這是我自己的例子:
private static final XSSFColor COLOR_ORANGE = new XSSFColor(new java.awt.Color(254, 253, 189));
private static final XSSFColor COLOR_GREY = new XSSFColor(new java.awt.Color(191, 190, 154));
. . .
XSSFCellStyle styleSubHeader = (XSSFCellStyle) wb.createCellStyle();
styleSubHeader.setFont(fontBold);
styleSubHeader.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
styleSubHeader.setAlignment(CellStyle.ALIGN_CENTER);
styleSubHeader.setFillForegroundColor(COLOR_ORANGE);
styleSubHeader.setFillPattern(CellStyle.SOLID_FOREGROUND);
styleSubHeader.setBorderBottom(CellStyle.BORDER_THIN);
styleSubHeader.setBottomBorderColor(COLOR_GREY);
styleSubHeader.setBorderLeft(CellStyle.BORDER_THIN);
styleSubHeader.setLeftBorderColor(COLOR_GREY);
styleSubHeader.setBorderRight(CellStyle.BORDER_THIN);
styleSubHeader.setRightBorderColor(COLOR_GREY);
styleSubHeader.setBorderTop(CellStyle.BORDER_THIN);
styleSubHeader.setTopBorderColor(COLOR_GREY);
. . .
/**
* Checking if every row and cell in merging region exists, and create those which are not
* @param sheet in which check is performed
* @param region to check
* @param cellStyle cell style to apply for whole region
*/
private void cleanBeforeMergeOnValidCells(XSSFSheet sheet,CellRangeAddress region, XSSFCellStyle cellStyle )
{
for(int rowNum =region.getFirstRow();rowNum<=region.getLastRow();rowNum++){
XSSFRow row= sheet.getRow(rowNum);
if(row==null){
sheet.createRow(rowNum);
logger.trace("while check row "+rowNum+" was created");
}
for(int colNum=region.getFirstColumn();colNum<=region.getLastColumn();colNum++){
XSSFCell currentCell = row.getCell(colNum);
if(currentCell==null){
currentCell = row.createCell(colNum);
logger.trace("while check cell "+rowNum+":"+colNum+" was created");
}
currentCell.setCellStyle(cellStyle);
}
}
}
最后你在實際合並調用之前調用它,如下所示:
CellRangeAddress region = new CellRangeAddress(rowIndex, rowIndex, mergeStart, cellIndex+cellOffset);
cleanBeforeMergeOnValidCells(row.getSheet(),region,styleSubHeader );
row.getSheet().addMergedRegion(region);// merging cells that has a title name
希望能幫助到你。
我建議您使用 getMergedRegions,該方法將返回工作表中合並區域的列表。 然后您可以迭代每個區域以應用邊框。 例如:
private void setBordersToMergedCells(Workbook workBook, Sheet sheet) {
List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
for (CellRangeAddress rangeAddress : mergedRegions) {
RegionUtil.setBorderTop(CellStyle.BORDER_THIN, rangeAddress, sheet, workBook);
RegionUtil.setBorderLeft(CellStyle.BORDER_THIN, rangeAddress, sheet, workBook);
RegionUtil.setBorderRight(CellStyle.BORDER_THIN, rangeAddress, sheet, workBook);
RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, rangeAddress, sheet, workBook);
}
}
然后,您可以在完成 mySheet 中所需的所有合並后調用此方法。
采用:
int rownumm=0;
rownumm++;
Row row = sheet.createRow(rownumm);
Cell cell = row.createCell(0);
cell.setCellValue(web.getUrl());
cell.setCellStyle(styles.get("font"));//font for text
CellRangeAddress region = CellRangeAddress.valueOf("$A$"+ (rownumm) + ":$E$+" + (rownumm));
frame(region, sheet, wb);
及使用方法:
private static void frame(CellRangeAddress region,Sheet sheet, Workbook wb){
sheet.addMergedRegion(region);
final short borderMediumDashed = CellStyle.BORDER_MEDIUM;
RegionUtil.setBorderBottom(borderMediumDashed, region, sheet, wb);
RegionUtil.setBorderTop(borderMediumDashed, region, sheet, wb);
RegionUtil.setBorderLeft(borderMediumDashed, region, sheet, wb);
RegionUtil.setBorderRight(borderMediumDashed, region, sheet, wb);
}
另請參閱鏈接上的“使用便利功能”:
https://poi.apache.org/spreadsheet/quick-guide.html#FooterPageNumbers
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
總的來說,我知道使用 RegionUtil 會讓你的生活更輕松,因為它是一個易於使用的類,但它非常占用 CPU。 只要有可能,總是嘗試使用 cellstyle 來執行這些程序。 最好使用自己的方法來設置表格邊框,而不是使用 RegionUtils。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.