简体   繁体   English

使用带有递增循环的Apache POI HSSF在XLS中删除几行空白

[英]Removing several blank lines in XLS using Apache POI HSSF with an incrementing loop

I need to remove several lines of an excel xls sheet. 我需要删除Excel XLS工作表的几行。 These lines always contain the same first cell thats why i check the first cell of all rows to find these rows 这些行始终包含相同的第一个单元格,这就是为什么我检查所有行的第一个单元格以查找这些行的原因

SSFCell myCell = myRow.getCell(0);
            myCell.setCellType(Cell.CELL_TYPE_STRING);
            String foundString = myCell.getStringCellValue();
            if(foundString.equals(searchString)){
                foundRows.add(rowCount);
            }
            rowCount++;

I then go on and "remove" those rows using removeRow which nulls all values 然后,我继续使用removeRow“删除”这些行,这将使所有值都为空

public static void removeRows() {

    List<Integer> foundRowsToDelete = new ArrayList<Integer>();
    //Copy values to another list
    for(int i=0; i<foundRows.size(); i++){
        foundRowsToDelete.add(foundRows.get(i));
    }
    //Delete values from rows, leaving empty rows
    while(foundRowsToDelete.size()!=0){
        int rowIndex = foundRowsToDelete.get(0);
        Row removingRow = mySheet.getRow(rowIndex);
        if (removingRow != null) {
            mySheet.removeRow(removingRow);
            foundRowsToDelete.remove(0);
    }
}
    //Move empty rows to bottom of the sheet
    for(int i = 0; i < mySheet.getLastRowNum(); i++){
        if(isRowEmpty(i)){
            mySheet.shiftRows(i+1, mySheet.getLastRowNum(), -1);
            i--;
        }
    }
}

I check if they are empty through using the duplicated rowcounter 我通过使用重复的行计数器检查它们是否为空

//Comparision of previously detected empty rows and given row count
public static boolean isRowEmpty(int suspectedRowNumber) {
    for(int i=0;i<foundRows.size();i++){
        if (suspectedRowNumber == foundRows.get(i)){
            foundRows.remove(i);
            return true;
        }
    }
    return false;
}

However only the first of these rows gets deleted. 但是,仅删除这些行中的第一行。 The rest will stay empty. 其余的将为空。 I therefore assume that there is something wrong with some incrementing done by me, but i just can't figure out exactly why. 因此,我认为我进行的某些增量操作有问题,但是我无法确切知道原因。

Thanks for your help in advance. 感谢您的帮助。

It's not immediately clear why your code isn't working, but I look at a couple things to debug 目前尚不清楚为什么您的代码无法正常工作,但我要调试一些事情

  1. Your foundRowsToDelete ArrayList is being populated with values contained in the foundRows Array. 您的foundRowsToDelete ArrayList正在使用foundRows Array中包含的值填充。 Are you sure what you expect to find in foundRows is actually there. 您确定您期望在foundRows中找到的内容确实存在吗?

  2. Is there a reason you don't remove the row when initally iterating through the rows in your sheet? 初次遍历工作表中的行时,有没有理由不删除该行? Maybe something like this: 也许是这样的:

 Sheet sheet = workbook.getSheetAt(0); For (Row row : sheet) { SSFCell myCell = row.getCell(0); if(myCell.getCellType() == Cell.CELL_TYPE_STRING){ String foundString = myCell.getStringCellValue(); if(foundString.equalsIgnoreCase(searchString){ // why not just remove here? sheet.removeRow(row); } } } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM