java - 删除特定行 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
尝试在 aIterator
迭代该工作表的行时更改行。这是不可能的。
Iterator
因此,您需要以另一种方式获取行,而不是使用。例如,使用for
循环使用int r
andsheet.getRow(r)
来获取行。
但Sheet.removeRow
有一个误导性的名字。它并没有真正删除一行,而只是从工作表的存储中删除了该行,而不将下面的行向上移动。所以实际上它只是完全清空了行。如果您真的想删除该行,则需要使用Sheet.shiftRows
. Sheet.shiftRows
在以前的版本中有很多错误apache poi
。所以它只有使用 current 才能正常工作apache poi 5.0.0
。
但是使用Sheet.shiftRows
每次移位都会影响行号。因此,for
使用int r
andsheet.getRow(r)
来获取行的循环需要从下到上向后循环,否则它将尝试获取已经删除的行号。
以下代码应使用 currentapache 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);
}
}
}
推荐阅读
- c# - 使用 where 子句 linq C# 查询中的对象数组从主列表中过滤
- sql - SQL中如何将多行合并为一行
- prometheus - jmx_exporter 未发回 jmv_ 指标
- syntax - ? 如何获得 dart 2.13 编译语句:typedef IntList = List
; - javascript - 如何动态地将 3 个 html 元素附加到 .col-3 div?
- typescript - 为什么 typedoc 会忽略我的接口声明?
- laravel-backpack - 背包 laravel 在 setupListOperation 中显示具体数据
- vbscript - 使用 VbScript 在列中查找字符串并返回行值
- node.js - 阻止 Google 自动更改结果语言
- r - R - 根据其他 3 列中的 NA 有条件地创建新列