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++; } }