我正在尝试通过apache POA值合并某些单元格,并且出现以下错误:
这是我正在尝试的:
int primeiraLinha = 0;
int ultimaLinha = 0;
for (int x = 0; x < sheet.getPhysicalNumberOfRows(); x++) {
if (sheet.getRow(x).getCell(0).toString().equals(sheet.getRow(x+1).getCell(0).toString())) {
CellRangeAddress cellRangeAddress = new CellRangeAddress(x,x+1,0,0);
sheet.addMergedRegion(cellRangeAddress);
}
}``
这是错误:
java.lang.IllegalStateException:无法将合并区域A16:A17添加到工作表中,因为它与现有合并区域(A15:A16)重叠。
答案 0 :(得分:0)
您必须分析工作表并确定要合并的区域的第一个单元格和最后一个单元格。然后,您必须将所有这些单元格合并为一张。例如:
int firstRow = -1;
int lastRow = -1;
// no need for first and last cells because you are processing only first column
// for each row
for (int x = 0; x < sheet.getPhysicalNumberOfRows(); x++) {
// if current row's first cell contains a value equal to the next one
if (sheet.getRow(x).getCell(0).toString().equals(sheet.getRow(x+1).getCell(0).toString())) {
// save the first row only on first iteration
if (firstRow == -1) {
firstRow = x;
}
// always update last row
lastRow == x+1;
}
}
// at this point, already having identified first and last rows, you can request for a merge
CellRangeAddress cellRangeAddress = new CellRangeAddress(firstRow, lastRow, 0, 0);
sheet.addMergedRegion(cellRangeAddress);
我现在无法测试,但它应该可以工作