永久删除空行的Apache POI使用JAVA在Excel工作表 [英] Permanently Delete Empty Rows Apache POI using JAVA in Excel Sheet

查看:1037
本文介绍了永久删除空行的Apache POI使用JAVA在Excel工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

}

shiftRows()之后,我写我的新文件。但我的code现已生效。我只需要删除/删除(任何话)来我里面的数据的空行。所以是有可能这样做?如果是的话,我是在做正确的?如果没有任何人可以帮我这样做?提前致谢!

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!

推荐答案

如果记忆回忆 shiftRows(INT STARTROW,诠释endRow,INT N)是你所需要的。我不太记得如何检查是否行是空的,但是如果你不知道,一排是空的(通常是通过使用 removeRow的(鳞次栉比))和你知道的rowIndex,那么它是没有那么糟糕。通过调用:

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 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);

您在每一个行移位[的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:

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

}

作为一个小纸条,如果 N 为负,行上移。如果 N 为正的行向下移动。所以我不太清楚,如果你的意思是行的块下移500或不在您提供code。我希望这有助于。

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.

这篇关于永久删除空行的Apache POI使用JAVA在Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆