Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.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
Java 使用Apache_poi删除包含特定文本的Excel行_Java_Excel_Swing_Apache_Apache Poi - Fatal编程技术网

Java 使用Apache_poi删除包含特定文本的Excel行

Java 使用Apache_poi删除包含特定文本的Excel行,java,excel,swing,apache,apache-poi,Java,Excel,Swing,Apache,Apache Poi,我正在尝试删除一个包含特定数据的Excel行。例如:删除一个学生卷号为1的行。以下是我到目前为止所做的操作,但它不起作用 String personal=personal1.getSelectedItem().toString(); String roll=classper.getText(); String ps="personal"; InputStream myxls = new FileInputStream("D:\\" + personal + ps + ".

我正在尝试删除一个包含特定数据的Excel行。例如:删除一个学生卷号为1的行。以下是我到目前为止所做的操作,但它不起作用

   String personal=personal1.getSelectedItem().toString();
   String roll=classper.getText();
   String ps="personal";
   InputStream myxls = new FileInputStream("D:\\" + personal + ps + ".xls");
   Workbook book = new HSSFWorkbook(myxls);
   Sheet sheet = book.getSheetAt(0);
   Row Row = null;
   Cell Cell = null;
   int LastRowNum = sheet.getLastRowNum();
   for(int RowNum= 0;RowNum<LastRowNum-1;RowNum++){
        Row=sheet.getRow(RowNum);
        for(int CellNum = 0; CellNum<Row.getLastCellNum();CellNum++){
           Cell = Row.getCell(CellNum);
           String TextInCell=Cell.toString();
           if(TextInCell.contains(roll)){
              sheet.shiftRows(RowNum+1, LastRowNum, -1);
              LastRowNum--;
              RowNum--;
              break;
           }
        }
    }
   }
String personal=personal 1.getSelectedItem().toString();
String roll=classper.getText();
字符串ps=“个人”;
InputStream myxls=新文件InputStream(“D:\\”+个人+ps+“.xls”);
工作簿=新的HSSF工作簿(myxls);
Sheet Sheet=book.getSheetAt(0);
行=空;
Cell=null;
int LastRowNum=sheet.getLastRowNum();

对于(int RowNum=0;RowNum),请不要忘记在应用更改后需要保存工作簿。添加以下内容:

FileOutputStream out = new FileOutputStream ("file name");
book.write(out);
还要注意,如果所需的行是最后一行,则应调用
sheet.removeow(rowIndex);
而不是
shiftRows

另一个观察结果是,在运行时跳过
for
循环中的最后一行,直到
LastRowNum-1
。它应该是:

for (int rowIndex = 0; rowIndex < sheet.getLastRowNum(); rowIndex++) {
}
for(int-rowIndex=0;rowIndex
removeRow()
实现的一个示例。