Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

Reading an uploaded Excel file with POI, cell.getStringCellValue() throws IllegalStateException: Cannot get a STRING value from a NUMERIC cell for an id column. Why, and how do you read cells robustly?

Excel stores numbers and text as different cell types, and when a user types an id like 10045 Excel saves it as a NUMERIC cell, so getStringCellValue() refuses it. To read cells robustly we use DataFormatter.formatCellValue(cell), which returns the text Excel would display whatever the type, together with a FormulaEvaluator for formula cells and DateUtil.isCellDateFormatted for dates. Around that we validate every row and report clear errors with row numbers instead of failing the whole upload.

Why this question is asked

Uploads of Excel files are a classic source of production errors, because the people who fill in the files do not think about cell types at all. An id column looks like text to them, a date is just a date, and some cells are formulas copied from another sheet. Interviewers ask this question because the exception is very common and the fix shows whether we understand how Excel actually stores data. In this answer, we will see how cell types work, reproduce the exception, fix it with DataFormatter, handle formulas and dates, and then turn the reader into one that gives users useful errors.

1A Quick Recap of Cell Types

Every cell in an Excel file has a type, which POI exposes as the CellType enum: NUMERIC, STRING, BOOLEAN, FORMULA, BLANK and ERROR. The type is decided by what the user typed, not by what the column means. If someone types 10045, Excel stores the number 10045 as a double and the cell is NUMERIC. If they type ABC-7, the cell is STRING. If they type 00123, Excel normally stores the number 123 and drops the zeros, unless the cell was formatted as text first.

A cell also has a data format that controls how its value is displayed, such as 0, #,##0.00 or dd/mm/yyyy. The format does not change the stored value. That distinction between the stored value and the displayed text is the key to this problem.

2Reproducing the Exception

Our reader assumes the id column is text and asks for a string. That works for ids like ABC-7 and fails the moment a row contains a plain number.

Java
Row row = sheet.getRow(1);
Cell idCell = row.getCell(0); // the user typed 10045
idCell.getCellType(); // NUMERIC
idCell.getStringCellValue();
// IllegalStateException: Cannot get a STRING value from a NUMERIC cell
idCell.getNumericCellValue(); // 10045.0
String.valueOf(idCell.getNumericCellValue()); // "10045.0": not the id either

Notice that the obvious workaround, reading the number and converting it, is also wrong: we get 10045.0, and for an id with 16 or more digits the double has already lost precision. POI is strict on purpose. Each typed getter only works for its own type, so that we never silently get a value we did not ask for.

3Reading the Displayed Text With DataFormatter

What we usually want from an id column is exactly what the user sees in Excel. DataFormatter gives us that: it looks at the cell's type and data format and returns the formatted text.

Java
DataFormatter fmt = new DataFormatter(Locale.ROOT); // one per import, fixed locale
fmt.formatCellValue(numericCell); // "10045"
fmt.formatCellValue(textCell); // "ABC-7"
fmt.formatCellValue(zeroPadded); // "00123" when the cell has format 00000
fmt.formatCellValue(blankCell); // ""
fmt.formatCellValue(null); // "": a missing cell is safe too

So the same call works for numbers, text, booleans and blanks. We pass a fixed locale so that the output does not depend on the server's default locale, which would otherwise change decimal and thousands separators. For values that are meant to be numbers, such as an amount, we still check the type and read getNumericCellValue(), because there we want the number itself, not its display.

4Handling Formulas and Dates

A formula cell has type FORMULA, and DataFormatter on its own returns the formula text, such as A2&"-"&B2, which is rarely what we want. There are two ways to get the value. The file usually stores the last calculated result, and we can read it after checking getCachedFormulaResultType(). Or we create a FormulaEvaluator and pass it to DataFormatter, which calculates the formula now. With untrusted uploads, the cached result is often the safer choice, because evaluating formulas from strangers costs CPU and some formulas refer to other workbooks.

Dates are the other surprise. Excel has no date type: a date is a NUMERIC cell holding the number of days since 1900, displayed with a date format. So 2026-03-01 is stored as 46082. DateUtil.isCellDateFormatted(cell) tells us whether a numeric cell is meant as a date, and then we read it as a date.

Java
FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
String readText(Cell c) {
return fmt.formatCellValue(c, eval); // evaluates formulas, formats the result
}
LocalDate readDate(Cell c) {
if (c.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(c)) {
return c.getLocalDateTimeCellValue().toLocalDate(); // 2026-03-01
}
throw new CellError("expected a date");
}
Step throughReading one cell robustly
  1. Is the cell missing or BLANK?
    YesTreat as empty, check if requiredNoLook at the type
  2. Is it a FORMULA?
    YesUse the cached result type, or evaluateNoUse the cell's own type
  3. Do we expect a date?
    YesCheck DateUtil.isCellDateFormattedNoContinue
  4. Do we expect a number?
    YesRequire NUMERIC, read getNumericCellValue()NoRead text with DataFormatter
  5. Validate the valuelength, pattern, range

5 steps in order. Play them, or step through them one at a time.

5Validating the Upload Row by Row

A robust reader does not stop at the first bad cell with a stack trace. It reads every row, collects problems with the row number (shown the way Excel shows it, starting at 1) and the column name, and returns them all at once. The user can then fix the file in one go.

Java
record RowError(int row, String column, String message) {}
List<RowError> errors = new ArrayList<>();
for (Row row : sheet) {
if (row.getRowNum() == 0) continue; // header
String id = fmt.formatCellValue(row.getCell(0), eval).trim();
if (!id.matches("[A-Z0-9-]{1,20}"))
errors.add(new RowError(row.getRowNum() + 1, "Id", "must be 1 to 20 letters, digits or dashes"));
Cell amount = row.getCell(2);
if (amount == null || amount.getCellType() != CellType.NUMERIC)
errors.add(new RowError(row.getRowNum() + 1, "Amount", "must be a number"));
}
// errors: [RowError[row=7, column=Amount, message=must be a number]]

We also check the header row against the expected column names, so a file with columns in a different order is rejected clearly instead of being read wrongly. And we treat the upload as untrusted input: limit its size and row count, as for any file a user sends us.

6Follow-up questions

How can users keep leading zeros in ids?

By formatting the column as text in Excel before typing, or by starting the value with an apostrophe, which makes Excel store it as text. If the cell already has a number format such as 00000, DataFormatter shows the zeros back. A template file with the id column already formatted as text prevents most of these problems.

Why does iterating with for (Cell c : row) skip some cells?

The row iterator returns only cells that physically exist in the file, so empty cells in the middle of a row are skipped and the positions shift. For column-based reading we use row.getCell(index), optionally with a MissingCellPolicy such as RETURN_BLANK_AS_NULL, so each value is read from the column we mean.

Should we evaluate formulas in uploaded files?

Only if we need the current value and the cached one cannot be trusted. Evaluation costs CPU on large files and may meet functions POI does not support or references to external workbooks. For most uploads, reading the cached result is enough and safer.

7Conclusion

In this answer, we saw that the exception comes from Excel's cell types: an id typed as a number is a NUMERIC cell, and POI's typed getters only work for their own type. DataFormatter.formatCellValue returns the displayed text for any type, a FormulaEvaluator or the cached result handles formula cells, and DateUtil.isCellDateFormatted recognises dates, which are numbers with a date format. Wrapping this in a reader that validates every row and reports errors with row numbers turns a crash into feedback the user can act on.

What to say in the interviewAbout 70 seconds aloud

Open with

The cause is that Excel decides the cell type from what was typed, not from what the column means. An id like 10045 is stored as a NUMERIC cell holding a double, and POI's getStringCellValue only works on STRING cells, so it throws IllegalStateException.

Then, if they want more

Reading the number and converting it is not right either, because I get 10045.0, lose leading zeros, and long ids lose precision. For text-like columns I use DataFormatter, created once per import with a fixed locale, and call formatCellValue, which gives me exactly what the user sees in Excel for numbers, text, booleans and blanks.

For formula cells I either read the cached result after checking getCachedFormulaResultType, or pass a FormulaEvaluator to the formatter; for untrusted uploads I prefer the cached value. Dates are numeric cells with a date format, so I check DateUtil.isCellDateFormatted and read getLocalDateTimeCellValue. For real numbers like amounts I require a NUMERIC cell.

Finally I validate every row, check the header, and return a list of errors with Excel row numbers and column names, so the user can fix the whole file at once.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Reading an uploaded Excel file with POI, cell.getStringCellValue() throws...": https://scrivio-interview-library.netlify.app/java/document-libraries/f0d8cc818b/