Estou tentando gerar um relatório em excel no java porém recebo a seguinte mensagem de erro
java.lang.IllegalStateException: Cannot add merged region A18:B20 to sheet because it overlaps with an existing merged region (A14:B18).
.
Segue o trecho do código abaixo, o mesmo ao estar comentado funciona, porém não traz todos os registros do objeto.
mergeLines += actSize > 0 ? actSize-1 : 0;
if(mergeGoal != mergeLines) {
includeMergeCell(sheet, mergeGoal, mergeLines, 2, 2, styles.get("no-back-left") );
includeMergeCell(sheet, mergeGoal, mergeLines, 3, 3, styles.get("no-back-left") );
}
}
// if(mergeSheet < mergeLines){
// sheet.addMergedRegion(new CellRangeAddress(mergeSheet , mergeLines, 0, 1));
// }
A14:B18 já está mesclado e você está tentando mesclar A18:B20, porém A18 faz parte de um conjunto de células mescladas. Não aceita sobrepor A14:B18 que já está mesclada.
A menssagem de erro está dizendo você não pode mesclar a região A18:B20 pelo motivo que eu disse acima.
Tenta mesclar A14:B20.
Antonio, realmente isso está acontecendo, porém a nível de código não consigo identificar o momento em que ocorre esse merge incorreto. Apenas identifiquei que ocorre neste trecho do código.
Posta o código do método includeMergeCell.
O códgio do includeMergeCell:
public static void includeMergeCell(HSSFSheet sheet, int firstRow, int lastRow, int firstCol, int lastCol, CellStyle style) {
for (int i = firstRow; i <= lastRow; i++) {
Row row = getRow(sheet, i);
for (int j = firstCol; j <= lastCol; j++) {
Cell cell = getCell(row, j);
cell.setCellStyle(style);
}
}
sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol));
}
public int addMergedRegion(CellRangeAddress region) {
return this.addMergedRegion(region, true);
}
Essa linha mescla as células desejadas. Aqui você precisa informar o intervalo correto.
new CellRangeAddress(daLinha, ateLinha, daColuna, ateColuna)
Os índices começam em 0, portando se você quer a linha 1 da planilha passa o valor 0 como parâmetro.
O intervalo A14:B20 seria new CellRangeAddress(13, 19, 0, 1)