获取合并单元格中值的一个方法POI

时间:2021-04-25 03:45:03

获取合并单元格中值的一个方法POI

    private static String getCellValueForMerginRegion(Cell cell) {

        int rowIdx=cell.getRowIndex();
Sheet sheet=cell.getSheet();
int mergedRegions=sheet.getNumMergedRegions();
for (int i = 0; i < mergedRegions; i++) {
CellRangeAddress cellRangeAddress=sheet.getMergedRegion(i); if (cellRangeAddress.getLastColumn()==0) {
int firstRowIdx=cellRangeAddress.getFirstRow();
int lastRowIdx=cellRangeAddress.getLastRow();
if (rowIdx<=lastRowIdx&&rowIdx>=firstRowIdx) {
String cellValue = null;
for (int j = firstRowIdx; j <=lastRowIdx; j++) {
cellValue=getCellValue(sheet.getRow(j).getCell(0));
if (cellValue.trim().length()==0) {
System.out.println("firstRowIdx:"+firstRowIdx+",lastRowIdx:"+lastRowIdx+":"+cellValue);
continue;
}
break;
} return cellValue;
}
} } return null;
}