Index: src/examples/src/org/apache/poi/xssf/usermodel/examples/ShiftRows.java =================================================================== --- src/examples/src/org/apache/poi/xssf/usermodel/examples/ShiftRows.java (revision 0) +++ src/examples/src/org/apache/poi/xssf/usermodel/examples/ShiftRows.java (revision 0) @@ -0,0 +1,58 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.xssf.usermodel.examples; + +import java.io.FileOutputStream; + +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +public class ShiftRows { + + public static void main(String[]args) throws Exception{ + Workbook wb = new XSSFWorkbook(); + Sheet sheet = wb.createSheet("Sheet1"); + + Row row1=sheet.createRow(1); + row1.createCell(0).setCellValue(1); + + Row row2=sheet.createRow(4); + row2.createCell(1).setCellValue(2); + + Row row3=sheet.createRow(5); + row3.createCell(2).setCellValue(3); + + Row row4=sheet.createRow(6); + row4.createCell(3).setCellValue(4); + + Row row5=sheet.createRow(9); + row5.createCell(4).setCellValue(5); + + // Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5) + sheet.shiftRows(5, 10, -4); + + FileOutputStream fileOut = new FileOutputStream("shiftRows.xlsx"); + wb.write(fileOut); + fileOut.close(); + + } + + +} Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java (revision 708894) +++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java (working copy) @@ -267,14 +267,14 @@ * @param height the height in "twips" or 1/20th of a point. -1 resets to the default height */ public void setHeight(short height) { - if(height == -1){ - this.row.unsetHt(); - this.row.unsetCustomHeight(); - } else { - this.row.setHt((double)height/20); - this.row.setCustomHeight(true); + if(height == -1){ + if(row.isSetHt())this.row.unsetHt(); + if(row.isSetCustomHeight())this.row.unsetCustomHeight(); + } else { + this.row.setHt((double)height/20); + this.row.setCustomHeight(true); - } + } } /** * Set the row's height in points. Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java (revision 708894) +++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java (working copy) @@ -806,4 +806,17 @@ public CTCell getCTCell(){ return cell; } + +/** + * update cell reference in case of shift row + * @param row + */ + public void modifyCellReference(XSSFRow row){ + this.cell.setR(new CellReference(row.getRowNum(), cellNum).formatAsString()); + + CTCell[] ctCells=row.getCTRow().getCArray(); + for(CTCell ctCell: ctCells){ + ctCell.setR(new CellReference(row.getRowNum(), cellNum).formatAsString()); + } + } } Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (revision 708894) +++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (working copy) @@ -1424,9 +1424,11 @@ public void shiftRows(int startRow, int endRow, int n, boolean copyRowHeight, boolean resetOriginalRowHeight) { for (Iterator it = rowIterator() ; it.hasNext() ; ) { Row row = it.next(); + if (!copyRowHeight) { row.setHeight((short)-1); } + if (resetOriginalRowHeight && getDefaultRowHeight() >= 0) { row.setHeight(getDefaultRowHeight()); } @@ -1435,6 +1437,9 @@ } else if (row.getRowNum() >= startRow && row.getRowNum() <= endRow) { row.setRowNum(row.getRowNum() + n); + if(row.getFirstCellNum()>-1){ + modifyCellReference((XSSFRow)row); + } } } //rebuild the rows map @@ -1443,6 +1448,16 @@ rows = map; } + + private void modifyCellReference(XSSFRow row){ + for(int i=row.getFirstCellNum();i<=row.getLastCellNum();i++){ + XSSFCell c=(XSSFCell)row.getCell(i); + if(c!=null){ + c.modifyCellReference(row); + } + } + } + /** * Location of the top left visible cell Location of the top left visible cell in the bottom right * pane (when in Left-to-Right mode).