无法删除使用Apache POI的空行的Excel文件 [英] Unable to delete the Empty Rows Excel File using Apache POI

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

问题描述

我一直想我的code删除我的excel文件里的空行!我的code是:

I have been trying my code to delete the empty rows inside my excel file! my code is:

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);
         for(int i = 0; i < sheet.getLastRowNum(); i++){
if(sheet.getRow(i)==null){
    sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1);
    i--;
}
}

FileOutputStream fileOut = new FileOutputStream("C:/juni1.xls");
wb.write(fileOut);
fileOut.close();
        //Here I want to write the new update file without empty rows! 
    }
    catch(Exception e){
        System.out.print("SERRO "+e);
    }

}

在code没有任何作用的。任何机构可以告诉我是什么问题,请帮助我,我一直在努力做的事情,因为过去的10个小时。在此先感谢!

The code has no effect at all. Can any body tell me what is the problem please help me i have been trying to do the thing since last 10 hours. thanks in advance!

推荐答案

将有两种情况下,任何行是空白的。

There will be two cases when any row is blank.


  1. 首先,行是其他行之间,但从来没有被初始化或创建。在这种情况下的 Sheet.getRow(I)为null。

  2. 第二,行创建,它的电池可能会或可能不会习惯,但现在所有的细胞是空白的。在这种情况下的 Sheet.getRow(我)不会以空。 (您可以通过检查 Sheet.getRow(I).getLastCellNum()它总是会告诉你计数像其他行。)

  1. First, the Row is in between the other rows, but never be initialized or created. In this case Sheet.getRow(i) will be null.
  2. And Second, the Row was created, its cell may or may not get used but now all of its cells are blank. In this case Sheet.getRow(i) will not be null. (you can check it by using Sheet.getRow(i).getLastCellNum() it will always show you the count same as other rows.)

在一般情况下会发生的第二个条件。也许在你的情况,应该是这个原因。为此,您需要添加附加条件来检查所有的细胞是否是空的或没有。

In general case the second condition occurs. Perhaps in your case, it should be the reason. For this you need to add additional condition to check whether all the cells are blank or not.

    for(int i = 0; i < sheet.getLastRowNum(); i++){
        if(sheet.getRow(i)==null){
            sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1);
            i--;
        continue;
        }
        for(int j =0; j<sheet.getRow(i).getLastCellNum();j++){
            if(sheet.getRow(i).getCell(j).toString().trim().equals("")){
                isRowEmpty=true;
            }else {
                isRowEmpty=false;
                break;
            }
        }
        if(isRowEmpty==true){
            sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1);
            i--;
        }
    }

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

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