I'm using the Apache POi HSSF library to import and export an Excel table to my application (tableview). I want to delete from Java a Row in Excel with a specific ID. It works. My Problem is, after i delete a row there are a empty row and it delete more than the selected row. Could everybody help?
FileInputStream inp = new FileInputStream(
"...............";
HSSFWorkbook wb = (HSSFWorkbook) WorkbookFactory.create(inp);
HSSFSheet sheet = wb.getSheetAt(0);
String selectedid = auftragTabelle.getSelectionModel().getSelectedItem().getId();
int rowIndex = 0;
int lastRowNum = sheet.getLastRowNum();
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
if (cell.getRichStringCellValue().getString().trim().equals(selectedid)) {
rowIndex = cell.getRowIndex();
}
}
}
}
if (rowIndex >= 0 && rowIndex < lastRowNum) {
sheet.shiftRows(rowIndex, lastRowNum, -1);
}
if (rowIndex <= lastRowNum) {
HSSFRow removingRow = sheet.getRow(rowIndex);
if (removingRow != null) {
sheet.removeRow(removingRow);
}
}
OutputStream out = new FileOutputStream(
"............";
wb.write(out);
out.close();
}