final TreeMap<Integer, TreeMap<Integer, XSSFCell>> rowCells = new TreeMap<Integer, TreeMap<Integer, XSSFCell>>();
int expectRownum = tRow; //handle sparse rows which might override destination row
int maxColNum = -1;
if (startRow <= endRow) {
for (Iterator<XSSFRow> it = getRows().subMap(startRow, endRow+1).values().iterator(); it.hasNext() ; ) {
XSSFRow row = it.next();
int rownum = row.getRowNum();
final int newRownum = rownum + nRow;
if (newRownum > maxrow) { //nothing to do
break;
}
if (rownum > expectRownum) { //sparse row between expectRownum(inclusive) and current row(exclusive), to be removed
addRemovePair(removePairs, expectRownum + nRow, newRownum);
}
expectRownum = rownum + 1;
SortedMap<Integer, XSSFCell> oldCells = row.getCells().subMap(Integer.valueOf(lCol), Integer.valueOf(rCol+1));
if (!oldCells.isEmpty()) {
TreeMap<Integer, XSSFCell> cells = new TreeMap<Integer, XSSFCell>(oldCells);
rowCells.put(newRownum, cells);
for(Cell cell : cells.values()) { //remove reference from row to the cell
row.removeCell(cell);
}
}
}
}
//spare row between expectedRownum(inclusive) to endRow+1(exclusive), to be remove
addRemovePair(removePairs, expectRownum + nRow, endRow + 1 + nRow);
//really remove rows of the target
final int tgtlCol = Math.max(0, lCol + nCol);
final int tgtrCol = Math.min(maxcol, rCol + nCol);
for(int[] pair : removePairs) {
final int start = Math.max(0, pair[0]);
final int end = pair[1];
for(int j=start; j < end; ++j) {
Row row = getRow(j);
if (row != null) {
removeCells(row, tgtlCol, tgtrCol);
}
}
}
//really update the row's cells
for (Entry<Integer, TreeMap<Integer, XSSFCell>> entry : rowCells.entrySet()) {
final int rownum = entry.getKey().intValue();
final TreeMap<Integer, XSSFCell> cells = entry.getValue();
XSSFRow row = getRow(rownum);
if (row == null) {
row = createRow(rownum);
} else {
removeCells(row, tgtlCol, tgtrCol);
}
for(Entry<Integer, XSSFCell> cellentry : cells.entrySet()) {
final int colnum = cellentry.getKey().intValue() + nCol;
if (colnum < 0) { //out of bound
continue;
}
if (colnum > maxcol) {
break;
}
final XSSFCell srcCell = cellentry.getValue();
BookHelper.assignCell(srcCell, row.createCell(colnum));
}
}
// Move comments from the source column to the
// destination column. Note that comments can