在Excel中写入时发生Java ConcurrentModificationException错误
我有一个java类,用于根据输入在特定单元格中编写特定字符串。 我使用ApachePOI 我们班有两种写作方法。一个用于单行书写,一个用于多次书写。 多重写作的方法似乎是我的麻烦,我认为:( 这是我的写作课:在Excel中写入时发生Java ConcurrentModificationException错误,java,excel,arraylist,apache-poi,runtime-error,Java,Excel,Arraylist,Apache Poi,Runtime Error,我有一个java类,用于根据输入在特定单元格中编写特定字符串。 我使用ApachePOI 我们班有两种写作方法。一个用于单行书写,一个用于多次书写。 多重写作的方法似乎是我的麻烦,我认为:( 这是我的写作课: import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TrueExcelWriter {
public TrueExcelWriter(){}
private static int findRow(XSSFSheet sheet, String cellContent) {
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getCellType() == CellType.STRING) {
if (cell.getRichStringCellValue().getString().trim().equals(cellContent)) {
return row.getRowNum();
}
}
if (cell.getCellType().equals(CellType.NUMERIC)){
// if (DateUtil.isCellDateFormatted(cell)) {
// System.out.println(cell.getDateCellValue());
//
// } else {
long lookup = (long) cell.getNumericCellValue();
String catcher = Long.toString(lookup);
if(catcher.equals(cellContent)){
return row.getRowNum();
}
// }
}
}
}
return 0;
}
public void WriteToAll(String suchobjekt, String ID, String SPID) throws FileNotFoundException, IOException{
final String fileName = "C:/Temp/TTT.xlsx";
InputStream input = new FileInputStream(fileName);
XSSFWorkbook wb = new XSSFWorkbook(input);
XSSFSheet sheet = wb.getSheetAt(0);
int colnr = 0;
switch (suchobjekt) {
case "option1":
colnr = 15;
break;
case "option2":
colnr = 16;
break;
case "option3":
colnr = 999;
break;
}
Iterator<Row> iterator = sheet.iterator();
while (iterator.hasNext()) {
Row nextRow = iterator.next();
if(nextRow.getRowNum()==0){
continue; //just skip the rows if row number is X or Y
}
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell nextCell = cellIterator.next();
String y = nextCell + "";
if(y.equals(ID)){
Row r = sheet.getRow(nextRow.getRowNum());
if (r == null) {
// First cell in the row, create
r = sheet.createRow(nextRow.getRowNum());
}
Cell c = r.getCell(colnr);
if (c == null) {
// New cell
c = r.createCell(colnr, CellType.STRING);
}
c.setCellValue(SPID);
}
}
}
input.close();
OutputStream output = new FileOutputStream(fileName);
wb.write(output);
output.close();
wb.close();
}
public void WriteTo(String suchobjekt, String ID, String SPID) throws FileNotFoundException, IOException{
final String fileName = "C:/Temp/TTT.xlsx";
InputStream input = new FileInputStream(fileName);
XSSFWorkbook wb = new XSSFWorkbook(input);
XSSFSheet sheet = wb.getSheetAt(0);
int colnr = 0;
int rownr;
switch (suchobjekt) {
case "option1":
colnr = 15;
break;
case "option2":
colnr = 16;
break;
case "option3":
colnr = 999;
break;
}
rownr = findRow(sheet, ID);
Row r = sheet.getRow(rownr);
if (r == null) {
// First cell in the row, create
r = sheet.createRow(rownr);
}
Cell c = r.getCell(colnr);
if (c == null) {
// New cell
c = r.createCell(colnr, CellType.STRING);
}
c.setCellValue(SPID);
input.close();
OutputStream output = new FileOutputStream(fileName);
wb.write(output);
output.close();
wb.close();
}
}
我的excel文件当前如下所示:
...14 15 16 ...
... | ID | ...
... | abc | done
... | abc | done
... | abc | done
... | def |
... | ghi |
... | jkl |
... | jkl |
... | jkl |
... | mno |
... | mno |
... | mno |
如果我使用上面的方法并将其用于所有ID,则除了ID“def”之外,其他所有操作都有效
我不知道为什么这对这一个不起作用(
如果我尝试此ID的单行写入功能,它会工作
但是我知道为什么多重书写功能不适用于这个
我收到以下错误消息:
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.base/java.util.TreeMap$PrivateEntryIterator.nextEntry(TreeMap.java:1208)
at java.base/java.util.TreeMap$ValueIterator.next(TreeMap.java:1253)
at easypackaging.TrueExcelWriter.WriteToAll(TrueExcelWriter.java:88)
at easypackaging.TEST_TrueVerladen.actionPerformed(TEST_TrueVerladen.java:172)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6401)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
TrueExcelWriter
是我运行ter.WriteToAll
的类
我用谷歌搜索了错误,但我无法用我发现的解决问题(
如果需要,我可以为您上传我完整的(经过审查的)excel表格
你们能帮帮我吗
诚挚的,
沙托斯
更新:
我为我的多行书写编写了一个新的、更简单的算法,它对我很有效。:)谢谢你的帮助
新功能:
public void WriteToAll_TV(String ID, String SPID) throws FileNotFoundException, IOException{
final String fileName = "C:/Temp/TTT.xlsx";
InputStream input = new FileInputStream(fileName);
XSSFWorkbook wb = new XSSFWorkbook(input);
CreationHelper createHelper = wb.getCreationHelper();
XSSFSheet sheet = wb.getSheetAt(0);
XSSFCell cell;
XSSFRow row;
int colnr = 15;
int rownr = 0;
rownr = findRow(sheet, ID);
for(int i = rownr; i < sheet.getLastRowNum(); i++){
row = sheet.getRow(i);
String y = row.getCell(colnr)+"";
if(y.equals(ID)){
row.createCell(16).setCellValue(createHelper.createRichTextString(SPID));
}
}
input.close();
OutputStream output = new FileOutputStream(fileName);
wb.write(output);
output.close();
wb.close();
}
public void WriteToAll\u TV(字符串ID,字符串SPID)抛出FileNotFoundException,IOException{
最终字符串fileName=“C:/Temp/TTT.xlsx”;
InputStream输入=新文件InputStream(文件名);
XSSF工作簿wb=新XSSF工作簿(输入);
CreationHelper createHelper=wb.getCreationHelper();
XSSFSheet-sheet=wb.getSheetAt(0);
XSSFCell细胞;
XSSFRow行;
int colnr=15;
int-rownr=0;
rownr=findRow(图纸,ID);
对于(int i=rownr;i
您在对行进行迭代的同时添加行来修改工作表。这就是ConcurrentModificationException的来源。非常感谢您的回复!我想我明白您的意思了。我编写了一个新的、更简单的算法,适用于我。我用我的多重写入函数的更新版本更新了我的主题!谢谢你的hep!:)
public void WriteToAll_TV(String ID, String SPID) throws FileNotFoundException, IOException{
final String fileName = "C:/Temp/TTT.xlsx";
InputStream input = new FileInputStream(fileName);
XSSFWorkbook wb = new XSSFWorkbook(input);
CreationHelper createHelper = wb.getCreationHelper();
XSSFSheet sheet = wb.getSheetAt(0);
XSSFCell cell;
XSSFRow row;
int colnr = 15;
int rownr = 0;
rownr = findRow(sheet, ID);
for(int i = rownr; i < sheet.getLastRowNum(); i++){
row = sheet.getRow(i);
String y = row.getCell(colnr)+"";
if(y.equals(ID)){
row.createCell(16).setCellValue(createHelper.createRichTextString(SPID));
}
}
input.close();
OutputStream output = new FileOutputStream(fileName);
wb.write(output);
output.close();
wb.close();
}