!611 CellUtil修改错误注释,增加相关方法

Merge pull request !611 from Husky/v5-dev
This commit is contained in:
Looly 2022-05-05 01:29:36 +00:00 committed by Gitee
commit c89b895618
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -199,7 +199,7 @@ public class CellUtil {
* *
* @param row Excel表的行 * @param row Excel表的行
* @param cellIndex 列号 * @param cellIndex 列号
* @return {@link Row} * @return {@link Cell}
* @since 5.5.0 * @since 5.5.0
*/ */
public static Cell getCell(Row row, int cellIndex) { public static Cell getCell(Row row, int cellIndex) {
@ -218,7 +218,7 @@ public class CellUtil {
* *
* @param row Excel表的行 * @param row Excel表的行
* @param cellIndex 列号 * @param cellIndex 列号
* @return {@link Row} * @return {@link Cell}
* @since 4.0.2 * @since 4.0.2
*/ */
public static Cell getOrCreateCell(Row row, int cellIndex) { public static Cell getOrCreateCell(Row row, int cellIndex) {
@ -265,18 +265,78 @@ public class CellUtil {
* @return 是否是合并单元格 * @return 是否是合并单元格
*/ */
public static boolean isMergedRegion(Sheet sheet, int x, int y) { public static boolean isMergedRegion(Sheet sheet, int x, int y) {
final int sheetMergeCount = sheet.getNumMergedRegions(); if (sheet != null) {
CellRangeAddress ca; final int sheetMergeCount = sheet.getNumMergedRegions();
for (int i = 0; i < sheetMergeCount; i++) { CellRangeAddress ca;
ca = sheet.getMergedRegion(i); for (int i = 0; i < sheetMergeCount; i++) {
if (y >= ca.getFirstRow() && y <= ca.getLastRow() ca = sheet.getMergedRegion(i);
&& x >= ca.getFirstColumn() && x <= ca.getLastColumn()) { if (y >= ca.getFirstRow() && y <= ca.getLastRow()
return true; && x >= ca.getFirstColumn() && x <= ca.getLastColumn()) {
return true;
}
} }
} }
return false; return false;
} }
/**
* 获取合并单元格{@link CellRangeAddress}如果不是返回null
*
* @param sheet {@link Sheet}
* @param locationRef 单元格地址标识符例如A11B5
* @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) lastColumn // last column (0-based)
); );
if (null != cellStyle) { setMergeCellStyle(cellStyle, cellRangeAddress, sheet);
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);
}
return sheet.addMergedRegion(cellRangeAddress); return sheet.addMergedRegion(cellRangeAddress);
} }
@ -435,5 +486,25 @@ public class CellUtil {
} }
return null; 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 // -------------------------------------------------------------------------------------------------------------- Private method end
} }