Java 使用Apache_poi删除包含特定文本的Excel行
我正在尝试删除一个包含特定数据的Excel行。例如:删除一个学生卷号为1的行。以下是我到目前为止所做的操作,但它不起作用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 + ".
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()
实现的一个示例。