如何使用apache POI跳过或删除excel文件中的行

时间:2018-02-08 19:49:24

标签: java excel apache-poi

我正在使用excel文件,我必须将其转换为文本文件,我必须从中选择特定的行。我已经完成了但是我正在使用文件的结尾因为我必须删除或跳过那里有3行,它们的位置可能会动态变化。所以我正在共享excel文件的屏幕 enter image description here

我必须跳过或删除三个高亮显示的行,它们的位置可能会动态更改。我提供了用于从文件中获取特定行的代码

@Override
public List < String > processSheet(Sheet sheet) {
    ActivityLauncher.logConsoleMsg("Processing sheet " + sheet.getSheetName() + " started");
    List < String > rows = new ArrayList < String > ();
    Iterator < Row > rowIterator = sheet.iterator();
    while (rowIterator.hasNext()) {
        Row currentRow = rowIterator.next();
        StringBuilder row = new StringBuilder();
        for (int i = 0; i < currentRow.getLastCellNum(); i++) {
            if (currentRow.getRowNum() == 0 || currentRow.getRowNum() == 1 || 
                    currentRow.getRowNum() == 2 || currentRow.getRowNum() == 3 || 
                    currentRow.getRowNum() == 4 || currentRow.getRowNum() == 5 || 
                    currentRow.getRowNum() == 6 || currentRow.getRowNum() == 7 || 
                    currentRow.getRowNum() == 9 || currentRow.getRowNum() == 10 || 
                    currentRow.getRowNum() == 11) {
                continue;
            } else {
                Cell currentCell = currentRow.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                String cellValue = excelManager.evalCell(currentCell);

                if (cellValue.contains("Total")) { //i have tried here
                    continue;
                }
                if (!cellValue.isEmpty()) {
                    row.append(cellValue);
                }

                //adjusting the cell values with the | or BLANK 
                if (currentCell == null || currentCell.getCellType() == Cell.CELL_TYPE_BLANK) {
                    row.append("");
                } else {
                    row.append(SEPARATOR);
                }
            }
        }
        if (!row.toString().isEmpty()) {
            rows.add(row.toString());
        }
    }
    ActivityLauncher.logConsoleMsg("Processing sheet " + sheet.getSheetName() + " completed");
    return rows;
}

我无法跳过位置,因为位置可能会随时间而改变。

1 个答案:

答案 0 :(得分:1)

我假设值为ARC...的列是第一列。如果没有,请将startCell变量的值更改为正确值。

@Override
public List < String > processSheet(Sheet sheet) {
    ActivityLauncher.logConsoleMsg("Processing sheet " + sheet.getSheetName() + " started");
    List <String> rows = new ArrayList<String>();
    Iterator<Row> rowIterator = sheet.iterator();
    final int startCell = 0;
    while (rowIterator.hasNext()) {
        Row currentRow = rowIterator.next();
        StringBuilder row = new StringBuilder();
        // not sure if you really need this if statement in your Prod code
        if (currentRow.getRowNum() == 0 || currentRow.getRowNum() == 1 || 
                currentRow.getRowNum() == 2 || currentRow.getRowNum() == 3 || 
                currentRow.getRowNum() == 4 || currentRow.getRowNum() == 5 || 
                currentRow.getRowNum() == 6 || currentRow.getRowNum() == 7 || 
                currentRow.getRowNum() == 9 || currentRow.getRowNum() == 10 || 
                currentRow.getRowNum() == 11) {
            continue;
        } 
        if (currentRow.getLastCellNum() < 0 || currentRow.getCell(startCell) == null || "".equals(currentRow.getCell(startCell).getStringValue()) || (currentRow.getCell(startCell).getStringValue() != null && currentRow.getCell(startCell).getStringValue().contains("Total"))) {
            continue;
       }
       for (int i = 0; i < currentRow.getLastCellNum(); i++) {
         else {
            Cell currentCell = currentRow.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
            String cellValue = excelManager.evalCell(currentCell);
            if (!cellValue.isEmpty()) {
                row.append(cellValue);
            }

            //adjusting the cell values with the | or BLANK 
            if (currentCell == null || currentCell.getCellType() == Cell.CELL_TYPE_BLANK) {
                row.append("");
            } else {
                row.append(SEPARATOR);
            }
        }
    }
    if (!row.toString().isEmpty()) {
        rows.add(row.toString());
    }
  }
  ActivityLauncher.logConsoleMsg("Processing sheet " + sheet.getSheetName() + " completed");
  return rows;
}