1 回答

TA貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個(gè)贊
如果一行在 List 中被雙重包含,則會(huì)發(fā)生錯(cuò)誤toRemove。AList允許重復(fù)條目。所以同一行可能會(huì)被雙重添加到List. 如果然后Iterator得到該行的第一次出現(xiàn),這將從工作表中正確刪除。但是,如果稍后再次出現(xiàn)同一行,則row.getRowNum()失敗,因?yàn)樵撔胁辉俅嬖谟诠ぷ鞅碇小?/p>
這是重現(xiàn)該行為的完整代碼:
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;
public class ExcelRemoveRows {
public static void main(String[] args) throws Exception {
String filePath = "Excel.xlsx"; // must contain at least 5 filled rows
Workbook workbook = WorkbookFactory.create(new FileInputStream(filePath));
Sheet sheet = workbook.getSheetAt(0);
List<Row> toRemoveList = new ArrayList<Row>();
toRemoveList.add(sheet.getRow(0));
toRemoveList.add(sheet.getRow(2));
toRemoveList.add(sheet.getRow(4));
toRemoveList.add(sheet.getRow(2)); // this produces the error
System.out.println(toRemoveList); // contains row hawing index 2 (r="3") two times
for (Row row : toRemoveList) {
System.out.println(row.getRowNum()); // XmlValueDisconnectedException on second occurance of row index 2
sheet.removeRow(row);
}
FileOutputStream out = new FileOutputStream("Changed"+filePath);
workbook.write(out);
out.close();
workbook.close();
}
}
解決方案是避免List多次包含同一行。
我不會(huì)收集要在 a 中刪除的行List<XSSFRow>,而是收集要在 a 中刪除的行號(hào)Set<Integer>。這將避免重復(fù),因?yàn)?aSet不允許重復(fù)的元素。然后要?jiǎng)h除的行可以簡(jiǎn)單地通過(guò)sheet.getRow(rowNum).
代碼:
...
Set<Integer> toRemoveSet = new HashSet<Integer>();
toRemoveSet.add(sheet.getRow(0).getRowNum());
toRemoveSet.add(sheet.getRow(2).getRowNum());
toRemoveSet.add(sheet.getRow(4).getRowNum());
toRemoveSet.add(sheet.getRow(2).getRowNum());
System.out.println(toRemoveSet); // does not contain the row index 2 two times
for (Integer rowNum : toRemoveSet) {
Row row = sheet.getRow(rowNum);
System.out.println(row.getRowNum());
sheet.removeRow(row);
}
...
添加回答
舉報(bào)