java - 如何使用 apache POI 从 Excel 文件中跳过或删除行

标签 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;
}

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

最佳答案

我假设值为 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;
}

关于java - 如何使用 apache POI 从 Excel 文件中跳过或删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48693651/

相关文章:

Java/JSON.simple - 在斜杠之前打印反斜杠

python - 分析数据集的增减

VBA根据关键字将行从sheet1复制到sheet2

java - 使用 Apache POI 读取 .xlsx 文件会给出 InvocationTargetException

java - 如何设置 ImageView 以显示图像 url 中的图像?

java - 使用 hibernate @Version 控制进行 DTO 实体映射

java - 如何将变量调用到另一个方法中?

excel - 如何在Excel中将范围内的空白换为零?

java - 将 Web 元素导入 Excel 时出现空指针异常

java - Apache POI 样式应用于所有单元格