Java 写入 Excel 时出现 ConcurrentModificationException 错误

Java ConcurrentModificationException Error while writing in Excel

我有一个 java class,我用它在特定的单元格中写入特定的字符串,具体取决于输入。 我使用 Apache POI。

我的Class有两种写法。一种用于单行书写,一种用于多次书写。 多写的方法好像是我的麻烦,我觉得:(

这是我写的class:

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 文件中写入内容:

try {
     ter.WriteToAll("option2", "abc", "done");
    } catch (IOException ex) {
               System.out.println("Error!  "+ex);
    }

我的 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.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue.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.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue.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 是 class 我 运行 是 ter.WriteToAll.

我用谷歌搜索了这个错误,但我无法用我找到的东西解决我的问题。 :(

如果需要,我可以为您上传完整的(已审查)excel sheet。

你们能帮帮我吗?

您真诚的, 沙托斯

更新:

我为我的多行写作写了一个新的、更简单的算法,它对我有用。 :) 感谢您的帮助!

新功能:

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();
        
    }

您正在通过在遍历行时添加行来修改 sheet。这就是 ConcurrentModificationException 的来源。