shiftRows() apache POI 3.9. Sorting sheet

2019-07-27 03:53发布

问题:

I came here from this topic: Apache-POI sorting rows in excel

Here is my code:

public class WorkWithData {
    private int rowNum;
    private Sheet sheet;
    private Workbook workbook;

    protected WorkWithData(File userFile) throws Exception {
        this.workbook = getWorkbook(userFile);
        sheet = workbook.getSheetAt(0);
        rowNum = sheet.getLastRowNum();
    }

    protected Workbook getWorkbook(File userFile) throws Exception {
        InputStream inputStream = new FileInputStream(userFile);
        return WorkbookFactory.create(inputStream);
    }

    public void saveWorkbook(File userFile) throws IOException {
        OutputStream outputStream = new FileOutputStream(userFile);
        workbook.write(outputStream);
    }

    public void sortSheet() throws IOException {
        boolean sorted = true;
        while (sorted) {
            sorted = false;
            for (Row row : sheet) {
                if (row.getRowNum() == rowNum) {
                    break;
                }
                if (row.getRowNum() == 0) {
                    continue;
                }
                Row row2 = sheet.getRow(row.getRowNum() + 1);
                if (row2 == null) {
                    continue;
                }
                String firstValue = (row.getCell(0) != null) ? row.getCell(0).getStringCellValue() : "";
                String secondValue = (row2.getCell(0) != null) ? row2.getCell(0).getStringCellValue() : "";
                if (secondValue.compareToIgnoreCase(firstValue) < 0) {
                    sheet.shiftRows(row2.getRowNum(), row2.getRowNum(), -1);
                    sheet.shiftRows(row.getRowNum(), row.getRowNum(), 1);
                    sorted = true;
                    lookAtFile();
                }
            }
        }
        saveWorkbook(new File("newnwe.xlsx"));
    }

    private void lookAtFile() {
        System.out.println("NEW OUTPUT:");
        Sheet sheet1 = workbook.cloneSheet(0);
        for (int i = 0; i <= rowNum; i++) {
            System.out.println(sheet1.getRow(i).getCell(0).getStringCellValue() + " : " + sheet1.getRow(i).getCell(1).getStringCellValue());
        }
    }
}

I have next excel file:

After we "change" the file I have output the result on colsole.

Output:

NEW OUTPUT:
Char name : Env name
Andrew : PC
Andrew : PC
Other name : Notebook
Andrew : PC
Other name : Notebook
Other name : Notebook
Other name : Notebook
Other name : Notebook
Other name : Notebook
Andrew : PC
Other name : Notebook
NEW OUTPUT:
Char name : Env name
Andrew : PC
Andrew : PC
Other name : Notebook
Andrew : PC
Other name : Notebook
Other name : Notebook
Other name : Notebook
Other name : Notebook
Other name : Notebook
Andrew : PC
Other name : Notebook

As you can see there is no changes. I expected that the first column will firstly have Andrew 4 times and then Other name.

Am I expecting wrong result ? How then it should work ?

Why is shiftRows() doesn't change anything ?