[英]Remove Specific row Apache Poi
我需要刪除包含用戶輸入的單詞的行。 這是我的代碼:
try {
System.out.println("Write material that need to be removed: ");
Scanner in = new Scanner(System.in);
String toFind = in.nextLine();
FileInputStream file = new FileInputStream(new File("solty.xls"));
//Create Workbook instance holding reference to .xlsx file
HSSFWorkbook workbook = new HSSFWorkbook(file);
DataFormatter formatter = new DataFormatter();
HSSFSheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row3 = rowIterator.next();
CellReference cellRef = new CellReference(row3.getRowNum(), cell.getColumnIndex());
String text = formatter.formatCellValue(cell);
// is it an exact match?
if (toFind.equals(text)) {
System.out.println("Text finded at " + cellRef.formatAsString());
System.out.println(cellRef.getRow());
int rowIndex = cellRef.getRow();
System.out.println(row.getCell(rowIndex));
HSSFRow row1 = sheet.getRow(rowIndex);
sheet.removeRow(row1);
file.close();
FileOutputStream outFile = new FileOutputStream(new File("solty.xls"));
workbook.write(outFile);
outFile.flush();
outFile.close();
}
// is it a partial match?
else if (text.contains(toFind)) {
System.out.println("Index is " + cellRef.formatAsString());
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
但問題是它始終刪除行,而不是包含用戶單詞的行。 即使它有效,它也有異常:ConcurrentModificationException
拋出ConcurrentModificationException
是因為Sheet.removeRow
嘗試更改行,而Iterator
迭代該工作表的行。 這是不可能的。
因此,您需要以另一種方式獲取行,而不是使用Iterator
。 例如,使用for
循環使用int r
和sheet.getRow(r)
來獲取行。
但是Sheet.removeRow
有一個誤導性的名稱。 它並沒有真正刪除一行,而只是從工作表的存儲中刪除了該行,而不將下面的行向上移動。 所以實際上它只是完全清空了行。 如果您真的想刪除該行,則需要使用Sheet.shiftRows
。 Sheet.shiftRows
在apache poi
的舊版本中存在許多錯誤。 所以它只能使用當前的apache poi 5.0.0
才能正常工作。
但是使用Sheet.shiftRows
每次移動都會影響行號。 因此,使用int r
和sheet.getRow(r)
獲取行的for
循環需要從下到上向后循環,否則它將嘗試獲取已經刪除的行號。
以下代碼應使用當前的apache poi 5.0.0
並刪除所有包含以String toFind
作為內容的單元格的行。
import java.io.FileOutputStream;
import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.*;
class RemoveSpecificRows {
public static void main(String[] args) throws Exception {
System.out.println("Write material that need to be removed: ");
java.util.Scanner in = new java.util.Scanner(System.in);
String toFind = in.nextLine();
try (Workbook workbook = WorkbookFactory.create(new FileInputStream("ExcelSource.xls"));
FileOutputStream fileout = new FileOutputStream("ExcelResult.xls") ) {
DataFormatter formatter = new DataFormatter();
Sheet sheet = workbook.getSheetAt(0);
for (int r = sheet.getLastRowNum(); r >=0; r--) {
Row row = sheet.getRow(r);
if (row != null) {
for (int c = 0; c < row.getLastCellNum(); c++) {
Cell cell = row.getCell(c);
String value = formatter.formatCellValue(cell);
if (toFind.equals(value)) {
System.out.println("Text " + toFind + " found at " + cell.getAddress());
//sheet.removeRow(row); //this only empties the row
sheet.shiftRows(r + 1, sheet.getLastRowNum() + 2, -1); //shift row below up to last row once up
break;
}
}
}
}
workbook.write(fileout);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.