In all three implementations of Cell, in setCellValue(String) and setCellValue(RichTextString) cell is convertyed to STRING before all checks have passed (or failed). That is, if the passed string exceeds the maximum length defined by the spreadsheet version, the method wil throw but the cell contents will have changed. The fix is simple: perform all checks before any invasive operation. testcase: @Test public void setStringCellValue_ifThrows_shallNotChangeCell() { Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0); final double value = 2.78; cell.setCellValue(value); assertEquals(CellType.NUMERIC, cell.getCellType()); int badLength = cell.getSheet().getWorkbook().getSpreadsheetVersion().getMaxTextLength() + 1; String badStringValue = new String(new byte[badLength]); try { cell.setCellValue(badStringValue); } catch (IllegalArgumentException e) { // no-op, expected to throw but we need to assert something more } assertEquals(CellType.NUMERIC, cell.getCellType()); // <- fails assertEquals(value, cell.getNumericCellValue(), 0); // <- fails, obviously }
My bad, this only applies to SXSSFCell. Also this in setCellValue(String): if(_value.getType()==CellType.FORMULA) if(_value instanceof NumericFormulaValue) { ((NumericFormulaValue) _value).setPreEvaluatedValue(Double.parseDouble(value)); } ... } never happens because it is preceded by resetting the internal value to a [Formula]StringValue, so I removed it.
Fixed in r1850342