Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Excel工作表中使用JAVA永久删除空行Apache POI_Java_Excel_Row_Apache Poi - Fatal编程技术网

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

在Excel工作表中使用JAVA永久删除空行Apache POI,java,excel,row,apache-poi,Java,Excel,Row,Apache Poi,我想永久删除那些没有任何类型数据的空行!我是这样做的: 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

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

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

}
private void shift(文件f){
文件F=F;
HSSF工作簿wb=null;
HSSF工作表=空;
试一试{
FileInputStream is=新的FileInputStream(F);
wb=新的HSSF工作手册(is);
sheet=wb.getSheetAt(0);
int rowIndex=0;
int lastRowNum=sheet.getLastRowNum();
如果(rowIndex>=0&&rowIndex

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

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


有关更多信息,请查看。

如果内存调用
shiftRows(int startRow、int endRow、int n)
就是您需要的。我不太记得如何检查一行是否为空,但如果您确实知道一行为空(通常通过使用
removeRow(row-row)
),并且您知道行索引,那么就没那么糟糕了。致电:

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(这将有效地删除一个空行)。如果您有多行,并且有办法确定该行是否为空,那么我建议如下:

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

}
for(int i=0;i

作为一个小提示,如果
n
为负数,则行向上移动。如果
n
为正数,则行向下移动。因此,我不太确定您是否打算在提供的代码中将该行向下移动500。我希望这会有所帮助。

请检查[如何使用poi删除工作表之间的空白行?][1]也许这会有帮助:没问题。祝您编码愉快!:d请注意,如果您的电子表格中有一个隐藏条目,在行中出现较大间隙后,这种方法可能会非常缓慢,因为它会迭代数十万个空白行,重复将其余行移动一个位置。但这并不是说这会在数据中造成一个漏洞—一个空白(null)一行