-1

This is my Excel data:

|   | Animal | Step | IsGood |
|:-:|:------:|:----:|:------:|
| 1 |   Dog  |   1  | true   |
| 2 |   Cat  |   2  | true   |

I wish to duplicate all the rows in my Excel file (not including the Header row) and manipulate the data. My New Excel File should look like this:

|   | Animal | Step | IsGood |
|:-:|:------:|:----:|:------:|
| 1 |   Dog  |   1  |  true  |
| 2 |   Dog  |   2  |  false |
| 3 |   Cat  |   3  |  true  |
| 4 |   Cat  |   4  |  false |

How do I this using Java? I'm using org.apache.poi library.

Dang Nguyen
  • 1,209
  • 1
  • 17
  • 29
Tal Angel
  • 1,301
  • 3
  • 29
  • 63

1 Answers1

0

I am using this method to duplicate

   private void duplicateInPlace() {
    XSSFSheet firstSheetObj = this.firstWB.getSheet(firstSheetName);
    XSSFSheet targetSheetObj = this.targetWB.createSheet(this.targetSheetName);
    int firstSheetLen = firstSheetObj.getLastRowNum();
    replaceMinusOnes(firstSheetObj,firstSheetObj);
    int targetCursorPosition=0;


    //
    for (int i = 0; i <  rangeStart ; i++) {
        XSSFRow firstRow = firstSheetObj.getRow(i);
        XSSFRow targetRow = targetSheetObj.createRow(targetCursorPosition);
        this.copyRow(firstRow,targetRow);
        targetCursorPosition+=1;

    }//endfor

    for (int i = rangeStart; i <  rangeEnd ; i++) {
        XSSFRow firstRow = firstSheetObj.getRow(i);
        XSSFRow targetRow = targetSheetObj.createRow(targetCursorPosition);
        this.copyRow(firstRow,targetRow);

        XSSFRow targetRow2 = targetSheetObj.createRow(targetCursorPosition+1);
        this.copyRow(firstRow,targetRow2); //this part duplicates
        targetCursorPosition+=2;

    }

    for (int i = rangeEnd; i <  firstSheetLen ; i++) {
        XSSFRow firstRow = firstSheetObj.getRow(i);
        XSSFRow targetRow = targetSheetObj.createRow(targetCursorPosition);
        this.copyRow(firstRow,targetRow);
        targetCursorPosition+=1;
    }
}