Introduction

The Cell class is the foundational unit in Aspose.Cells FOSS for Java. It stores typed values, exposes their runtime type through the CellValueType enum, persists formula strings for Excel to evaluate, and provides a uniform string representation via getStringValue(). This post examines the core cell API in detail with verified examples.

All code examples below use the Maven dependency:

<dependency>
  <groupId>com.aspose</groupId>
  <artifactId>cells-foss</artifactId>
  <version>1.0.0</version>
</dependency>

Core Cell API

Storing Typed Values with putValue()

Cell.putValue() accepts six Java types: String, int, double, boolean, LocalDateTime, and Object. Each call sets the cell’s internal value and determines the CellValueType returned by cell.getType().

import com.aspose.cells_foss.Cell;
import com.aspose.cells_foss.CellValueType;
import com.aspose.cells_foss.Workbook;
import com.aspose.cells_foss.Worksheet;
import java.time.LocalDateTime;

try (Workbook workbook = new Workbook()) {
    Worksheet sheet = workbook.getWorksheets().get(0);

    Cell a1 = sheet.getCells().get("A1");
    a1.putValue("Revenue");
    assert a1.getType() == CellValueType.STRING;

    Cell b1 = sheet.getCells().get("B1");
    b1.putValue(42500.75);
    assert b1.getType() == CellValueType.NUMBER;

    Cell c1 = sheet.getCells().get("C1");
    c1.putValue(true);
    assert c1.getType() == CellValueType.BOOLEAN;

    Cell d1 = sheet.getCells().get("D1");
    d1.putValue(LocalDateTime.of(2026, 4, 27, 9, 0, 0));
    assert d1.getType() == CellValueType.DATE_TIME;

    workbook.save("typed.xlsx");
}

Blank cells return CellValueType.BLANK from getType(). Integer values stored with putValue(int) round-trip through XLSX serialization without gaining a decimal point — getStringValue() returns "123" not "123.0".

Inspecting Values at Runtime

cell.getValue() returns the raw stored value as Object. cell.getStringValue() returns a locale-independent string representation suitable for downstream processing or display. Branching on CellValueType is the recommended pattern for type-safe value extraction:

import com.aspose.cells_foss.Cell;
import com.aspose.cells_foss.CellValueType;
import com.aspose.cells_foss.Workbook;
import com.aspose.cells_foss.Worksheet;

try (Workbook workbook = new Workbook()) {
    Worksheet sheet = workbook.getWorksheets().get(0);
    sheet.getCells().get("A1").putValue("Hello");
    sheet.getCells().get("B1").putValue(123);
    sheet.getCells().get("C1").putValue(true);
    sheet.getCells().get("D1").putValue(12.5d);
    sheet.getCells().get("G1").setFormula("=B1*2");

    Cell b1 = sheet.getCells().get("B1");
    if (b1.getType() == CellValueType.NUMBER) {
        System.out.println("Number: " + b1.getStringValue());
    }
    workbook.save("inspect.xlsx");
}

Formula Storage

Formula strings are stored with cell.setFormula(). The stored formula string begins with = and follows standard Excel formula syntax. The getType() method returns CellValueType.FORMULA for formula cells. Aspose.Cells FOSS for Java stores and round-trips formulas in XLSX; the formula is recalculated by Excel on open.

import com.aspose.cells_foss.Cell;
import com.aspose.cells_foss.CellValueType;
import com.aspose.cells_foss.Workbook;
import com.aspose.cells_foss.Worksheet;

try (Workbook workbook = new Workbook()) {
    Worksheet sheet = workbook.getWorksheets().get(0);
    Cell b1 = sheet.getCells().get("B1");
    b1.putValue(100.0);
    Cell c1 = sheet.getCells().get("C1");
    c1.setFormula("=B1*1.2");
    assert c1.getType() == CellValueType.FORMULA;
    workbook.save("formulas.xlsx");
}

XLSX Round-Trip Fidelity

All typed values are preserved across XLSX serialization and reload. Loading an existing .xlsx file uses new Workbook(path) or new Workbook(path, options) with LoadOptions to enable repair mode. After reload, getType() and getStringValue() return the same values as at write time.

import com.aspose.cells_foss.LoadIssue;
import com.aspose.cells_foss.LoadOptions;
import com.aspose.cells_foss.Workbook;
import com.aspose.cells_foss.Worksheet;

LoadOptions options = new LoadOptions();
options.setStrictMode(false);
options.setTryRepairPackage(true);
options.setTryRepairXml(true);

try (Workbook workbook = new Workbook("input.xlsx", options)) {
    if (workbook.getLoadDiagnostics().hasRepairs()) {
        for (LoadIssue issue : workbook.getLoadDiagnostics().getIssues()) {
            System.out.println(issue.getMessage());
        }
    }
    Worksheet sheet = workbook.getWorksheets().get(0);
    System.out.println(sheet.getCells().get("A1").getStringValue());
    workbook.save("output.xlsx");
}

Supported Formats

FormatExtensionReadWrite
Excel Open XML.xlsx

Open Source & Licensing

Aspose.Cells FOSS for Java is released under the MIT license. You are free to use, modify, and distribute it in personal, open-source, and commercial projects without restriction. The source code is available at github.com/aspose-cells-foss/Aspose.Cells-FOSS-for-Java.


Getting Started