I am trying to parse two tables, one in the .xls file, the second in .xlsx using Apache POI.

There are no problems with the table in .xls . But when working with the second table I get:

ConcurrentModificationException

Here is an example of the code for both methods:

  public static void Xls(String name) { InputStream in = null; HSSFWorkbook wb = null; try { in = new FileInputStream(name); wb = new HSSFWorkbook(in); } catch (IOException e) { e.printStackTrace(); } //Π’Π°Π±Π»ΠΈΡ†Π° для чтСния Sheet sheet = wb.getSheetAt(0); Iterator<Row> it = sheet.iterator(); Processing proc = new Processing(); //Π‘Π΄Π²ΠΈΠ³Π°Π΅ΠΌ строки Π²Π²Π΅Ρ€Ρ… sheet.shiftRows(11, sheet.getLastRowNum(), -11); int rowID = 0; while (it.hasNext()) { int cellID = 0; Row row = it.next(); //Π’Π°Π±Π»ΠΈΡ†Π° для записи Row newRow = sheet1.createRow(rowID); sheet1.setColumnWidth(0, 15000); //ΡˆΠΈΡ€ΠΈΠ½Π° столбца Iterator<Cell> cells = row.iterator(); while (cells.hasNext()) { Cell cell = cells.next(); Cell newCell = newRow.createCell(cellID); //Π’Ρ‹Π±ΠΈΡ€Π°Π΅ΠΌ ΠΊΠ°ΠΊΠΈΠ΅ ΠΈΠΌΠ΅Π½ΠΎ столбцы ΠΏΠ°Ρ€ΡΠΈΡ‚ΡŒ if (cell.getColumnIndex() >= 2 && cell.getColumnIndex() <= 4) { switch (cell.getCellTypeEnum()) { case STRING: newCell.setCellValue(cell.getStringCellValue()); cellID++; break; case NUMERIC: double tmp = cell.getNumericCellValue(); newCell.setCellValue(tmp); cellID++; newCell = newRow.createCell(cellID); newCell.setCellValue(proc.processing_opt(tmp)); cellID++; newCell = newRow.createCell(cellID); newCell.setCellValue(proc.processing_rozn(tmp)); cellID++; break; case FORMULA: break; default: break; } } } rowID++; } } //ΠœΠ΅Ρ‚ΠΎΠ΄ для создания прайса ΠΎΡ‚ AllSpare public static void Xlsx(String name, double currency) { InputStream inx = null; XSSFWorkbook wbx = null; try { inx = new FileInputStream(name); wbx = new XSSFWorkbook(inx); } catch (IOException e) { e.printStackTrace(); } //Π’Π°Π±Π»ΠΈΡ†Π° для чтСния Sheet sheet = wbx.getSheetAt(0); Iterator<Row> it = sheet.iterator(); Processing proc = new Processing(); //Π‘Π΄Π²ΠΈΠ³Π°Π΅ΠΌ строки Π²Π²Π΅Ρ€Ρ… sheet.shiftRows(11, sheet.getLastRowNum(), -11); int rowID = 0; while (it.hasNext()) { int cellID = 0; Row row = it.next(); **//Π—Π΄Π΅ΡΡŒ ConcurrentModificationException** //Π’Π°Π±Π»ΠΈΡ†Π° для записи Row newRow = sheet2.createRow(rowID); sheet2.setColumnWidth(0, 15000); //ΡˆΠΈΡ€ΠΈΠ½Π° столбца Iterator<Cell> cells = row.iterator(); while (cells.hasNext()) { Cell cell = cells.next(); Cell newCell = newRow.createCell(cellID); //Π’Ρ‹Π±ΠΈΡ€Π°Π΅ΠΌ ΠΊΠ°ΠΊΠΈΠ΅ ΠΈΠΌΠ΅Π½ΠΎ столбцы ΠΏΠ°Ρ€ΡΠΈΡ‚ΡŒ if (cell.getColumnIndex() == 2 | cell.getColumnIndex() == 4) { switch (cell.getCellTypeEnum()) { case STRING: newCell.setCellValue(cell.getStringCellValue()); cellID++; break; case NUMERIC: double tmp = cell.getNumericCellValue() / currency; newCell.setCellValue(tmp); cellID++; newCell = newRow.createCell(cellID); newCell.setCellValue(proc.processing_opt(tmp)); cellID++; newCell = newRow.createCell(cellID); newCell.setCellValue(proc.processing_rozn(tmp)); cellID++; break; case FORMULA: break; default: break; } } } rowID++; } } 
  • And you did not try the Iterator line <Row> it = sheet.iterator (); do after sheet.shiftRows (11, sheet.getLastRowNum (), -11) ;? - ezhov_da
  • I tried. Does not help. I also thought at first that the shift of the cells does not allow the iterator to work. But no. Even if I put a shift after a cycle, everything throws out the same action. - kirasirrr
  • Everything. figured out. Yes, really put the line Iterator <Row> it = sheet.iterator (); after the shift and it worked. I just initially did not carefully read your advice. It is strange that in the first method it works, but in the second it does not - kirasirrr

0