无法使用Apache POI

时间:2019-03-05 06:34:47

标签: java apache-poi

我有此电子表格

enter image description here

我想遍历包含“ USED”关键字和空字符串(空格)的所有行,将与那些条件匹配的所有行号保存到ArrayList中,然后循环我的电子表格并删除ArrayList中列出的所有行号。

我尝试并修改了Stackoverflow的一些解决方案(其中一个来自here)。我的代码如下:

 // Remove "USED" and empty string from spreadsheet
    private static void cleanUpSheet(String sheetName, String pathToExcel) throws IOException, InvalidFormatException {

        FileInputStream fileInput = new FileInputStream(pathToExcel);
        Workbook workbook = WorkbookFactory.create(fileInput);

        List<Integer> toRemove = new ArrayList<>();

        int index = workbook.getSheetIndex(sheetName);
        if (index == -1)
            System.out.println("No sheet Found");
        else {
            System.out.println("Index " + index);
            Sheet sheet = workbook.getSheetAt(index);
            Iterator<Row> rowItr = sheet.iterator();
            DataFormatter formatter = new DataFormatter();
            System.out.println("Total ROW " + sheet.getLastRowNum());
            while (rowItr.hasNext()) {
                Row row = rowItr.next();
                if (containsValue(row, row.getFirstCellNum(), row.getLastCellNum())) {
                    //Row contains value
                    System.out.println("Value Cell 0 " + formatter.formatCellValue(row.getCell(0)));
                    if (formatter.formatCellValue(row.getCell(0)).equals("USED")) {
                        System.out.println("Value cell 0 should contain USED " + formatter.formatCellValue(row.getCell(0)));
                        toRemove.add(row.getRowNum());
                        System.out.println("What I am removing "+ row.getCell(4).getRichStringCellValue());
                    }
                    continue;
                } else {
                    //Row does not contain value
                    System.out.println("Empty row " + formatter.formatCellValue(row.getCell(0)));
                    toRemove.add(row.getRowNum());
                    System.out.println("What I am removing "+ row.getCell(4).getRichStringCellValue());
                }
            }
            for (int i = 0 ; i<toRemove.size() ; i++) {
                System.out.println("To Remove " + toRemove.get(i));
                removeRow(pathToExcel,toRemove.get(i),false,sheet);
            }

            System.out.println("New Total Row " + sheet.getLastRowNum());
        }

        fileInput.close();

        FileOutputStream outFile = new FileOutputStream(new File(pathToExcel));
        workbook.write(outFile);
        outFile.close();
    }

    private static void removeRow(String pathToExcel, int rowToBeRemoved, boolean removeIndexOneOnly, Sheet sheet) throws IOException, InvalidFormatException {

        if (removeIndexOneOnly == true) rowToBeRemoved = 1;

        int lastRowNum = sheet.getLastRowNum();
        Row row = sheet.getRow(rowToBeRemoved);
        if (row != null && rowToBeRemoved != lastRowNum) {
            //System.out.println(row.getCell(3).getRichStringCellValue()); //For Testing
            //System.out.println(row.getCell(4).getRichStringCellValue()); //For Testing
            System.out.println("row to be removed " + rowToBeRemoved);
            System.out.println("last row " + lastRowNum);

            sheet.removeRow(row);
            sheet.shiftRows(rowToBeRemoved + 1, lastRowNum, -1);
        }

        if (rowToBeRemoved == lastRowNum) {
            System.out.println("Very Last Row");
            Row removingRow = sheet.getRow(rowToBeRemoved);
            if (removingRow != null) {
                sheet.removeRow(removingRow);
            }
        }


    }

结果看起来像这样

enter image description here

在调试模式下运行时,我可以看到List<Integer> toRemove确实列出了要删除的正确行号(即包含“ USED”和空白)。到目前为止,一切都很好。

但是当我执行这部分代码

    for (int i = 0 ; i<toRemove.size() ; i++) {
                System.out.println("To Remove " + toRemove.get(i));
                removeRow(pathToExcel,toRemove.get(i),false,sheet);
            }

它只是不会删除List<Integer> toRemove中列出的所有行。

我正在使用POI 3.17。

我想知道我在哪里可能做错事。

谢谢。

0 个答案:

没有答案