在Excel表中使用JAVA永久删除空行Apache POI。

时间:2021-07-19 20:25:05

I'd like to permanently delete those rows that are empty have no data of any type! I am doing this like:

我想永久地删除那些空的没有任何类型数据的行!我是这样做的:

 private void shift(File f){
    File F=f;
    HSSFWorkbook wb = null;
    HSSFSheet sheet=null;
    try{
    FileInputStream is=new FileInputStream(F);

    wb= new HSSFWorkbook(is);
    sheet = wb.getSheetAt(0);
    int rowIndex = 0;
    int lastRowNum = sheet.getLastRowNum();

   if (rowIndex >= 0 && rowIndex < lastRowNum) {
   sheet.shiftRows(rowIndex, lastRowNum, 500);
   }

        FileOutputStream fileOut = new FileOutputStream("C:/juni.xls");
         wb.write(fileOut);
         fileOut.close();
    }
    catch(Exception e){
        System.out.print("SERRO "+e);
    }

}

after shiftRows() i write my new file. But my code has now effect. I just need to remove/delete the empty rows that come inside my data (any ever). so is it possible doing so? if yes, am i doing right? if no can anybody please help me doing so? Thanks in advance!

shift()之后,我写新的文件。但是我的代码现在生效了。我只需要删除/删除数据内的空行(任何行)。那么这样做有可能吗?如果是,我做得对吗?如果没有人能帮我这么做吗?提前谢谢!

2 个解决方案

#1


11  

If memory recalls shiftRows(int startRow, int endRow, int n) is what you need. I don't quite remember how to check if a row is empty but if you DO know that a row is empty (normally through use of removeRow(Row row)) and you know the rowIndex, then it isn't so bad. By calling:

如果内存回忆移位(int startRow, int endRow, int n)是您需要的。我不太记得如何检查一个行是否为空,但是如果您确实知道一个行是空的(通常通过使用removeRow(row))),并且您知道rowIndex,那么它并不是那么糟糕。通过调用:

int rowIndex; //Assume already known and this is the row you want to get rid of
int lastIndex = sheet.getLastRowNum();
sheet.shiftRows(rowIndex + 1, lastIndex, -1);

you shift every row in [rowIndex + 1, lastIndex] inclusive up by 1 (which should delete an empty row effectively). If you have multiple rows and had a way to determine if the row was empty then I suggest something like:

将[rowIndex + 1, lastIndex]中的每一行移动1(这将有效地删除空行)。如果有多个行,并且有办法确定行是否为空,那么我建议如下:

for(int i = 0; i < sheet.getLastRowNum(); i++){
    if(isEmpty(sheet.getRow(i)){
        sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1);
        i--;//Adjusts the sweep in accordance to a row removal
    }
}

boolean isEmpty(Row row){

//Code to determine if a row is empty

}

As a small note if n is negative, the rows shift up. If n is positive the rows shift down. So I'm not quite sure if you meant to shift that chunk of rows down by 500 or not in your provided code. I hope this helps.

注意,如果n是负数,行会向上移动。如果n是正的行会向下平移。因此,我不太确定您是否打算在提供的代码中将这段代码的行减少500行。我希望这可以帮助。

#2


3  

Instead of shiftRows method. Try the removeRow method.

而不是shiftRows方法。尝试removeRow方法。

For more information have a look here.

有关更多信息,请参阅这里。

#1


11  

If memory recalls shiftRows(int startRow, int endRow, int n) is what you need. I don't quite remember how to check if a row is empty but if you DO know that a row is empty (normally through use of removeRow(Row row)) and you know the rowIndex, then it isn't so bad. By calling:

如果内存回忆移位(int startRow, int endRow, int n)是您需要的。我不太记得如何检查一个行是否为空,但是如果您确实知道一个行是空的(通常通过使用removeRow(row))),并且您知道rowIndex,那么它并不是那么糟糕。通过调用:

int rowIndex; //Assume already known and this is the row you want to get rid of
int lastIndex = sheet.getLastRowNum();
sheet.shiftRows(rowIndex + 1, lastIndex, -1);

you shift every row in [rowIndex + 1, lastIndex] inclusive up by 1 (which should delete an empty row effectively). If you have multiple rows and had a way to determine if the row was empty then I suggest something like:

将[rowIndex + 1, lastIndex]中的每一行移动1(这将有效地删除空行)。如果有多个行,并且有办法确定行是否为空,那么我建议如下:

for(int i = 0; i < sheet.getLastRowNum(); i++){
    if(isEmpty(sheet.getRow(i)){
        sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1);
        i--;//Adjusts the sweep in accordance to a row removal
    }
}

boolean isEmpty(Row row){

//Code to determine if a row is empty

}

As a small note if n is negative, the rows shift up. If n is positive the rows shift down. So I'm not quite sure if you meant to shift that chunk of rows down by 500 or not in your provided code. I hope this helps.

注意,如果n是负数,行会向上移动。如果n是正的行会向下平移。因此,我不太确定您是否打算在提供的代码中将这段代码的行减少500行。我希望这可以帮助。

#2


3  

Instead of shiftRows method. Try the removeRow method.

而不是shiftRows方法。尝试removeRow方法。

For more information have a look here.

有关更多信息,请参阅这里。