From 2c00b09ce8e02ddd697b2ff39add13691f2ff048 Mon Sep 17 00:00:00 2001 From: Husky <2466896229@qq.com> Date: Sat, 30 Apr 2022 18:32:00 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E6=94=B9=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=202=E3=80=81=E5=A2=9E=E5=8A=A0isMergedRegion?= =?UTF-8?q?=E7=A9=BA=E6=8C=87=E9=92=88=E5=88=A4=E6=96=AD=203=E3=80=81?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96CellRangeAddress=E6=96=B9?= =?UTF-8?q?=E6=B3=95=204=E3=80=81=E5=A2=9E=E5=8A=A0=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=90=88=E5=B9=B6=E5=8D=95=E5=85=83=E6=A0=BC=E6=A0=B7=E5=BC=8F?= =?UTF-8?q?=E6=96=B9=E6=B3=95(=E7=9B=AE=E5=89=8D=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E5=85=88=E5=90=88=E5=B9=B6=E5=86=8D=E8=AE=BE=E7=BD=AE=E6=A0=B7?= =?UTF-8?q?=E5=BC=8F,=E4=BD=86=E6=98=AF=E6=9C=89=E7=A7=8D=E6=83=85?= =?UTF-8?q?=E5=86=B5=E4=B8=BA=E8=AF=A5=E5=8D=95=E5=85=83=E6=A0=BC=E6=9C=AC?= =?UTF-8?q?=E6=9D=A5=E5=B0=B1=E6=98=AF=E5=90=88=E5=B9=B6=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=A0=BC,=E5=8F=AA=E9=9C=80=E8=A6=81=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F)=205=E3=80=81=E6=8F=90=E5=8F=96=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=A0=B9=E6=8D=AEcellStyle=E8=AE=BE=E7=BD=AE=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=A0=BC=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/poi/excel/cell/CellUtil.java | 109 +++++++++++++++--- 1 file changed, 90 insertions(+), 19 deletions(-) diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java index f4747bae3..1c7b59405 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java @@ -199,7 +199,7 @@ public class CellUtil { * * @param row Excel表的行 * @param cellIndex 列号 - * @return {@link Row} + * @return {@link Cell} * @since 5.5.0 */ public static Cell getCell(Row row, int cellIndex) { @@ -218,7 +218,7 @@ public class CellUtil { * * @param row Excel表的行 * @param cellIndex 列号 - * @return {@link Row} + * @return {@link Cell} * @since 4.0.2 */ public static Cell getOrCreateCell(Row row, int cellIndex) { @@ -265,18 +265,78 @@ public class CellUtil { * @return 是否是合并单元格 */ public static boolean isMergedRegion(Sheet sheet, int x, int y) { - final int sheetMergeCount = sheet.getNumMergedRegions(); - CellRangeAddress ca; - for (int i = 0; i < sheetMergeCount; i++) { - ca = sheet.getMergedRegion(i); - if (y >= ca.getFirstRow() && y <= ca.getLastRow() - && x >= ca.getFirstColumn() && x <= ca.getLastColumn()) { - return true; + if (sheet != null) { + final int sheetMergeCount = sheet.getNumMergedRegions(); + CellRangeAddress ca; + for (int i = 0; i < sheetMergeCount; i++) { + ca = sheet.getMergedRegion(i); + if (y >= ca.getFirstRow() && y <= ca.getLastRow() + && x >= ca.getFirstColumn() && x <= ca.getLastColumn()) { + return true; + } } } return false; } + /** + * 获取合并单元格{@link CellRangeAddress},如果不是返回null + * + * @param sheet {@link Sheet} + * @param locationRef 单元格地址标识符,例如A11,B5 + * @return {@link CellRangeAddress} + */ + public static CellRangeAddress getCellRangeAddress(Sheet sheet, String locationRef) { + final CellLocation cellLocation = ExcelUtil.toLocation(locationRef); + return getCellRangeAddress(sheet, cellLocation.getX(), cellLocation.getY()); + } + + /** + * 获取合并单元格{@link CellRangeAddress},如果不是返回null + * + * @param cell {@link Cell} + * @return {@link CellRangeAddress} + */ + public static CellRangeAddress getCellRangeAddress(Cell cell) { + return getCellRangeAddress(cell.getSheet(), cell.getColumnIndex(), cell.getRowIndex()); + } + + /** + * 获取合并单元格{@link CellRangeAddress},如果不是返回null + * + * @param sheet {@link Sheet} + * @param x 列号,从0开始 + * @param y 行号,从0开始 + * @return {@link CellRangeAddress} + */ + public static CellRangeAddress getCellRangeAddress(Sheet sheet, int x, int y) { + if (sheet != null) { + final int sheetMergeCount = sheet.getNumMergedRegions(); + CellRangeAddress ca; + for (int i = 0; i < sheetMergeCount; i++) { + ca = sheet.getMergedRegion(i); + if (y >= ca.getFirstRow() && y <= ca.getLastRow() + && x >= ca.getFirstColumn() && x <= ca.getLastColumn()) { + return ca; + } + } + } + return null; + } + + /** + * 设置合并单元格样式,如果不是则不设置 + * + * @param cell {@link Cell} + * @param cellStyle {@link CellStyle} + */ + public static void setMergedRegionStyle(Cell cell, CellStyle cellStyle) { + CellRangeAddress cellRangeAddress = getCellRangeAddress(cell); + if (cellRangeAddress != null) { + setMergeCellStyle(cellStyle, cellRangeAddress, cell.getSheet()); + } + } + /** * 合并单元格,可以根据设置的值来合并行和列 * @@ -310,16 +370,7 @@ public class CellUtil { lastColumn // last column (0-based) ); - if (null != cellStyle) { - RegionUtil.setBorderTop(cellStyle.getBorderTop(), cellRangeAddress, sheet); - RegionUtil.setBorderRight(cellStyle.getBorderRight(), cellRangeAddress, sheet); - RegionUtil.setBorderBottom(cellStyle.getBorderBottom(), cellRangeAddress, sheet); - RegionUtil.setBorderLeft(cellStyle.getBorderLeft(), cellRangeAddress, sheet); - RegionUtil.setTopBorderColor(cellStyle.getTopBorderColor(),cellRangeAddress,sheet); - RegionUtil.setRightBorderColor(cellStyle.getRightBorderColor(),cellRangeAddress,sheet); - RegionUtil.setLeftBorderColor(cellStyle.getLeftBorderColor(),cellRangeAddress,sheet); - RegionUtil.setBottomBorderColor(cellStyle.getBottomBorderColor(),cellRangeAddress,sheet); - } + setMergeCellStyle(cellStyle, cellRangeAddress, sheet); return sheet.addMergedRegion(cellRangeAddress); } @@ -435,5 +486,25 @@ public class CellUtil { } return null; } + + /** + * 根据{@link CellStyle}设置合并单元格边框样式 + * + * @param cellStyle {@link CellStyle} + * @param cellRangeAddress {@link CellRangeAddress} + * @param sheet {@link Sheet} + */ + private static void setMergeCellStyle(CellStyle cellStyle, CellRangeAddress cellRangeAddress, Sheet sheet) { + if (null != cellStyle) { + RegionUtil.setBorderTop(cellStyle.getBorderTop(), cellRangeAddress, sheet); + RegionUtil.setBorderRight(cellStyle.getBorderRight(), cellRangeAddress, sheet); + RegionUtil.setBorderBottom(cellStyle.getBorderBottom(), cellRangeAddress, sheet); + RegionUtil.setBorderLeft(cellStyle.getBorderLeft(), cellRangeAddress, sheet); + RegionUtil.setTopBorderColor(cellStyle.getTopBorderColor(), cellRangeAddress, sheet); + RegionUtil.setRightBorderColor(cellStyle.getRightBorderColor(), cellRangeAddress, sheet); + RegionUtil.setLeftBorderColor(cellStyle.getLeftBorderColor(), cellRangeAddress, sheet); + RegionUtil.setBottomBorderColor(cellStyle.getBottomBorderColor(), cellRangeAddress, sheet); + } + } // -------------------------------------------------------------------------------------------------------------- Private method end }