java - 如何删除 apache POI 单元格中字符串的整行基础

标签 java excel apache-poi

我正在处理一个 xlsx 文件,该文件最后包含最后两行,我必须根据它包含的单元格值排除这些行。即我的 Excel 包含印度总计 (10%) 和总计 (10%) )在最后两行,所以我必须根据 Total 关键字排除这两行。我正在使用以下方法,但它不起作用

@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.isEmpty()) {
                row.append(cellValue);
            }

            // trying to remove the rows containing total --------------------------------------->
            int rowNums = sheet.getPhysicalNumberOfRows();
            for(int k =0;k<rowNums;k++) {
                currentRow = sheet.getRow(k);
                if(currentRow !=null) {
                    for(int j=0;j<currentRow.getLastCellNum();j++) {
                        currentCell = currentRow.getCell(j);
if(currentCell !=null && currentCell.getCellTypeEnum().equals(CellType.STRING) && currentCell.getStringCellValue().toLowerCase().contains("total")){

                            sheet.removeRow(currentRow);
                            rowNums++;
                        }
                    }
                }
            }

            //---------------------------------------------------------->

            //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;
}
            }

有人可以帮忙吗?

最佳答案

这是我的小测试程序。它会删除包含单词“total”的每一行。您必须删除不需要的代码(可能是前两行和最后一行)

XSSFWorkbook wb = new XSSFWorkbook(new File("test.xlsx"));
XSSFSheet sheet = wb.getSheetAt(0);
int rowNums = sheet.getPhysicalNumberOfRows();
for (int i = 0; i < rowNums; i++) {
    XSSFRow r = sheet.getRow(i);
    if (r != null) {
        for (int j = 0; j < r.getLastCellNum(); j++) {
            XSSFCell c = r.getCell(j);
            if (c != null && c.getCellTypeEnum().equals(CellType.STRING)
                    && c.getStringCellValue().toLowerCase().contains("total")) {
                sheet.removeRow(r);
                rowNums++;
            }
        }
    }
}
wb.write(Files.newOutputStream(Paths.get("test2.xlsx"), StandardOpenOption.CREATE_NEW));

更新了您的代码:

List<String> rows = new ArrayList<String>();
int rowNums = sheet.getPhysicalNumberOfRows();
for (int k = 0; k < rowNums; k++) {
    Row currentRow = sheet.getRow(k);
    if (currentRow != null) {
        StringBuilder row = new StringBuilder();
        for (int i = 0; i < currentRow.getLastCellNum(); i++) {
            if (currentRow.getRowNum() <= 11 && currentRow.getRowNum() != 8) {
                continue;
            } else {
                Cell currentCell = currentRow.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                String cellValue = excelManager.evalCell(currentCell);
                if (!cellValue.isEmpty()) {
                    row.append(cellValue);
                }
                if (currentCell != null && currentCell.getCellTypeEnum().equals(CellType.STRING)
                        && currentCell.getStringCellValue().toLowerCase().contains("total")) {
                    sheet.removeRow(currentRow);
                    rowNums++;
                }
                if (currentCell == null || currentCell.getCellType() == Cell.CELL_TYPE_BLANK) {
                    row.append("");
                } else {
                    row.append(SEPARATOR);
                }
            }
        }
        if (!row.toString().isEmpty()) {
            rows.add(row.toString());
        }
    }
}

关于java - 如何删除 apache POI 单元格中字符串的整行基础,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48687781/

相关文章:

excel - 计算数据集中具有高值的对

vba - 应用程序定义或对象定义的错误 Excel

java - 解析密码重置

excel - 重命名工作表或显示错误的 Vba 代码

java - MindSphere URL 访问

android - 在 Android 中读取 Excel

java - Apache POI 从文件读取时抛出编码错误,但从 Stream 读取时不会抛出编码错误

java - 这个excel方法在做什么?

java - 为父类(super class)的方法重写分配较弱的访问权限

java - 如何知道 Spring/CXF 何时准备好接受请求?