Showing posts with label read XLSX files in Java. Show all posts
Showing posts with label read XLSX files in Java. Show all posts

Monday, November 30, 2026

Axyra Sheets CellValue: Read Excel Cell Types

A cell iterator needs more than the contents returned by value(). It must distinguish numbers, text, Boolean values, errors, truly blank cells, and arrays. This matters when an operator imports an xlsx workbook and needs reliable totals, inventory records, dates, or empty-field checks.

The problem

This issue came up in the Stack Overflow community while testing an Axyra Sheets workflow that reads each cell in an xlsx workbook. The method sheet.cell(row, column).value() returns a CellValue, but checking only the displayed result does not tell you whether the source cell contains an empty string or no content at all. The original discussion is available in Using Axyra-sheet API to tell data type when reading a cell value.

A second complication affects dates. Excel stores dates as numbers, so a value such as 45291.0 may represent a date or an ordinary amount. The number alone does not provide enough business meaning. The cell's number format determines whether Excel displays that number as a date.

The API also does not use null for an empty cell. A blank cell and a text cell containing "" can both look empty when displayed, but they represent different cell content. That difference affects validation, imports, and decisions such as whether to create a customer record or leave a field untouched.

The solution

Use the CellValue type itself as the source of truth. Axyra Sheets defines six records through its sealed CellValue interface:

  • CellValue.Number, for numeric content
  • CellValue.Text, for text content
  • CellValue.Bool, for Boolean values
  • CellValue.Error, for spreadsheet errors
  • CellValue.Blank, for a cell with no content
  • CellValue.Array, for an array result

A sealed interface is a Java type whose permitted implementations are known. That makes it suitable for a switch statement that handles every supported cell type.

String description = switch (cell.value()) {
    case CellValue.Number n -> "number " + n.value();
    case CellValue.Text t -> "text " + t.value();
    case CellValue.Bool b -> "bool " + b.value();
    case CellValue.Error e -> "error " + e.display();
    case CellValue.Blank b -> "blank";
    case CellValue.Array a -> "array";
};

This example uses pattern matching from Java 21. For older Java versions, use a sequence of instanceof checks and handle each permitted type separately.

The distinction between empty text and a blank cell is especially important:

CellValue value = cell.value();

if (value instanceof CellValue.Blank) {
    // The cell contains no content.
}

if (value instanceof CellValue.Text text && text.value().isEmpty()) {
    // The cell contains text whose value is "".
}

Both cases may return true when checked with isEmpty(). Do not use that check when the business process needs to distinguish an intentionally empty text result from a cell that was never populated.

Use cell.formattedText() only when you need the value as a person sees it. It returns processed, displayed text rather than the raw cell value. For example, a formatted currency amount may be useful in a report, but it is usually the wrong value for calculations or data validation.

For dates, first identify the cell type. If the value is a CellValue.Number, use cell.dateValue() or cell.dateTimeValue() when the cell's number format represents a date or time. These methods return a Java date object when the cell qualifies, and null when it does not. A text value that looks like a date remains text and needs separate parsing rules.

How to apply it

  1. Get the cell object and read its raw value.
Cell cell = sheet.cell(row, column);
CellValue value = cell.value();
  1. Handle the value according to its CellValue record.
if (value instanceof CellValue.Number number) {
    double amount = number.value();
    // Process a numeric value.
} else if (value instanceof CellValue.Text text) {
    String content = text.value();
    // Process text, including an intentional empty string.
} else if (value instanceof CellValue.Bool bool) {
    boolean answer = bool.value();
    // Process TRUE or FALSE.
} else if (value instanceof CellValue.Error error) {
    String message = error.display();
    // Record or reject the spreadsheet error.
} else if (value instanceof CellValue.Blank) {
    // Treat the cell as having no content.
} else if (value instanceof CellValue.Array array) {
    // Handle an array result according to the workbook design.
}
  1. Separate blank cells from empty text before applying required-field rules. For example, a blank product code may mean the row is unused, while an empty text value may come from a formula or an intentional user entry.

  2. Check numeric cells before converting them to dates.

if (value instanceof CellValue.Number) {
    LocalDate date = cell.dateValue();

    if (date != null) {
        // Use the date as a Java LocalDate.
    } else {
        // Treat the number as an amount or another numeric value.
    }
}
  1. Parse text dates separately. A text value such as 15/01/2026 is not automatically the same as an Excel date number. Apply the date format used by the business, and reject ambiguous or invalid text instead of silently converting it.

  2. For a complete worksheet, avoid making one API call for every cell when possible. Use sheet.usedRange() to identify the populated area, then call range.values() to retrieve a CellValue[][] block. This reduces repeated cell queries and gives the iterator a consistent structure to process.

  3. Keep raw values for calculations and validation. Use formattedText() only when generating a display report, exporting a human-readable document, or showing the same presentation seen in Excel.

  4. Add explicit handling for errors and unexpected arrays. A sales import should not quietly convert an error cell into an empty product or amount. Store the row and column location so someone can correct the workbook.

Implementation angle

This type-aware reader can become a dependable import layer for a small business workbook. An automation partner could map Number, Text, Bool, Blank, and Error into structured records in Zoho Creator, while preserving the difference between an empty string and an unfilled field. The same process could use Make or n8n to route invalid rows for review, and use Claude to generate and check Java validation rules before deployment.

Book an AI implementation engagement with Creator Scripts to turn your Axyra workbook iterator into a reliable operational workflow.

Frequently asked questions

How do I check a cell’s data type when reading an Excel file in Java?

Read the cell with cell.value() and check which CellValue type it returns. Handle numbers, text, Boolean values, errors, blanks, and arrays separately.

How can I tell a blank Excel cell from an empty string?

Check whether the value is CellValue.Blank or CellValue.Text with an empty value. Both can look empty, but they represent different cell content.

How do I know whether a number in Excel is a date?

Excel stores dates as numbers, so the number alone is not enough to identify one. Check the cell’s number format and use cell.dateValue() or cell.dateTimeValue(); these methods return null when the cell does not qualify as a date or time.