I am trying to print a string in a specific column of a xlsx file using apache poi in java. I have parsed the column x using regex and printing the required string in column y. However the getRowNum() of the code flags ConcurrentModificationExceptio. Below is the code snippet. Please help
|Column X|Column Y|
|Barath |Bar | //Regex Matches Bar and prints it in Column y (Required output).
while (rows.hasNext())
{
row=(XSSFRow) rows.next();
Iterator cells = row.cellIterator();
while (cells.hasNext())
{
cell=(XSSFCell) cells.next();
if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING)
{
Matcher m = p.matcher(cell.getRichStringCellValue().getString());
//rownr = row.getRowNum();
if(m.find()){
sheet.createRow(row.getRowNum()).createCell(colnr+6).setCellValue(row.getCell(colnr+4).toString().substring(m.start(),m.end()-1).replace(":", "")); //row.getRowNum() function causes Concurrent modification exception colnr+6 is destination column and Colnr+4 is column to be parsed.
System.out.print(row.getCell(colnr+4).toString().substring(m.start(),m.end()-1).replace(":", "")+"\n"); //Prints the parsed string in the console correctly
}
}
}
}
答案 0 :(得分:0)
似乎在创建行时,原始的rows
集合会被修改,而您正在迭代它。因此ConcurrentModificationException
。
有几种方法可以解决这个问题,但在这种情况下,我认为你根本不需要创建行,因为该行已经存在。所以不是......
sheet.createRow(...).createCell(colnr+6)
......你会:
row.createCell(colnr+6)