if (crefs != null) {
addRangeBounds(band, crefs);
Bounds thisBounds = templateBounds.get(band.getName());
Bounds parentBounds = templateBounds.get(band.getParentBand().getName());
Range parentRange = bandsToResultRanges.get(band.getParentBand());
int localRowNum = parentBounds != null && parentRange != null ?
parentRange.getFirstRow() - 1 + thisBounds.row0 - parentBounds.row0 :
rownum;
colnum = colnum == 0 ? getCellFromReference(crefs[0], templateSheet).getColumnIndex() : colnum;
copyMergeRegions(resultSheet, rangeName, localRowNum, colnum);
int firstRow = crefs[0].getRow();
int firstColumn = crefs[0].getCol();
for (CellReference cref : crefs) {//create necessary rows
int currentRow = cref.getRow();
final int rowOffset = currentRow - firstRow;
if (!rowExists(resultSheet, localRowNum + rowOffset)) {
HSSFRow resultRow = resultSheet.createRow(localRowNum + rowOffset);
copyPageBreaks(templateSheet, resultSheet, cref.getRow(), resultRow.getRowNum());
}
addedRowNumbers.add(cref.getRow());
}
CellReference topLeft = null;
CellReference bottomRight = null;
for (CellReference cref : crefs) {
int currentRow = cref.getRow();
int currentColumn = cref.getCol();
final int rowOffset = currentRow - firstRow;
final int columnOffset = currentColumn - firstColumn;
HSSFCell templateCell = getCellFromReference(cref, templateSheet);
resultSheet.setColumnWidth(colnum + columnOffset, templateSheet.getColumnWidth(templateCell.getColumnIndex()));
HSSFCell resultCell = copyCellFromTemplate(templateCell, resultSheet.getRow(localRowNum + rowOffset), colnum + columnOffset, band);
if (topLeft == null) {
topLeft = new CellReference(resultCell);
}
bottomRight = new CellReference(resultCell);
}
colnum += crefs[crefs.length - 1].getCol() - firstColumn + 1;
AreaReference templateRange = getAreaForRange(templateWorkbook, rangeName);
AreaReference resultRange = new AreaReference(topLeft, bottomRight);
areaDependencyManager.addDependency(new Area(band.getName(), Area.AreaAlign.VERTICAL, templateRange),
new Area(band.getName(), Area.AreaAlign.VERTICAL, resultRange));
bandsToResultRanges.put(band, new Range(resultSheet.getSheetName(),
resultRange.getFirstCell().getCol() + 1, resultRange.getFirstCell().getRow() + 1,
resultRange.getLastCell().getCol() + 1, resultRange.getLastCell().getRow() + 1
));
}