如何复制Excel行?

时间:2018-12-27 08:24:14

标签: java excel

这是我的Excel数据:

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

我希望复制Excel文件中的所有行(不包括“标题”行)并处理数据。我的新Excel文件应如下所示:

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

如何使用Java?我正在使用org.apache.poi库。

1 个答案:

答案 0 :(得分:0)

我正在使用此方法复制

   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;
    }
}
相关问题