Search code examples
javaapache-poiapache-poi-4

Why am I getting IllegalArgumentException while using copyRowFrom(...) in XSSFRow?


I'm trying to use copyRowFrom(...); to copy the first row of a spreadsheet to the first row of a new XSSFSheet but something is not working right. You can find the XSSFRow class and the method here: https://github.com/apache/poi/blob/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java#L581

I'm getting IllegalArgumentException("amountToMove must not be zero") from FormulaShifter.java: https://github.com/apache/poi/blob/trunk/src/java/org/apache/poi/ss/formula/FormulaShifter.java#L80

It seems that the problem is at line 623 of XSSFRow where FormulaShifter.createForRowCopy(...) is invoked with parameter rowDifference = 0 because source row is 0 and destination row is 0: https://github.com/apache/poi/blob/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java#L623

I don't know, maybe it's an error, but when reaching line 80 in FormulaShifter.java the parameter rowDifference corresponds to amountToMove which is 0 so it throws IllegalArgumentException.

Am I missing something or is this a bug of copyRowFrom(...); method in XSSFRow?


Solution

  • You are correct. This is a bug in XSSFRow since it calls FormulaShifter.createForRowCopy even if there is nothing to shift because destination row number is the same as source row number. You could file this as a bug to apache poi.

    But then there is a need for creating a test case which can be provided there. I have done that for you. The code also provides a workaround. This is first copying to a wrong destination row where row number differs from source row number. Then it copies the wrong first destination row to the really needed destination row.

    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xssf.usermodel.*;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    class ExcelCopyRowFrom {
    
     public static void main(String[] args) throws Exception {
    
      XSSFWorkbook workbook = (XSSFWorkbook)WorkbookFactory.create(new FileInputStream("SAMPLE.xlsx"));
      XSSFSheet srcSheet = workbook.getSheetAt(0);
      XSSFRow srcRow = srcSheet.getRow(0);
    
      XSSFSheet destSheet = workbook.createSheet();
      //XSSFRow destRow = destSheet.createRow(0); //this fails because destination row number is the same as source row number
    
      XSSFRow destRow = destSheet.createRow(1); //this works 
      destRow.copyRowFrom(srcRow, new CellCopyPolicy());
    
      //workaround copy wrong first destination row to really needed destination row
      XSSFRow destRowNeeded = destSheet.createRow(0);
      destRowNeeded.copyRowFrom(destRow, new CellCopyPolicy());
      //the remove wrong first destination row
      destSheet.removeRow(destRow);
    
      FileOutputStream outputStream = new FileOutputStream("SAMPLENEW.xlsx");
      workbook.write(outputStream);
      outputStream.close();
      workbook.close();
    
     }
    }