Working with Core Cell Operations

Working with Core Cell Operations

Overview

This guide shows how to store typed cell values, assign formulas, inspect value types via CellValueType, and a uniform string representation through getStringValue().

Storing Typed Values

The Cell.putValue() method accepts String, int, double, boolean, and LocalDateTime arguments, storing the value with the corresponding CellValueType. Use Cell.getType() to inspect the stored type at any time:

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()) {
    WorksheetCollection sheets = workbook.getWorksheets();
    Worksheet sheet = sheets.get(0);

    Cell a1 = sheet.getCells().get("A1");
    a1.putValue("Revenue");

    Cell b1 = sheet.getCells().get("B1");
    b1.putValue(42500.75);

    Cell c1 = sheet.getCells().get("C1");
    c1.putValue(true);

    Cell d1 = sheet.getCells().get("D1");
    d1.setFormula("=B1*1.2");

    // Type inspection
    System.out.println(a1.getType());  // STRING
    System.out.println(b1.getType());  // NUMBER
    System.out.println(c1.getType());  // BOOLEAN
    System.out.println(d1.getType());  // FORMULA

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

CellValueType Enum Values

Enum ValueDescription
STRINGText value stored by putValue(String)
NUMBERNumeric value stored by putValue(int) or putValue(double)
BOOLEANBoolean value stored by putValue(boolean)
DATE_TIMEDate/time value stored by putValue(LocalDateTime)
FORMULAFormula string stored by setFormula(String)
BLANKEmpty cell — no value has been set

Getting String Values

cell.getStringValue() returns a locale-independent string suitable for display or downstream processing regardless of the underlying value type:

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

try (Workbook workbook = new Workbook()) {
    WorksheetCollection sheets = workbook.getWorksheets();
    Worksheet sheet = sheets.get(0);
    Cell b1 = sheet.getCells().get("B1");
    b1.putValue(123);
    System.out.println(b1.getStringValue());  // "123" — no decimal point
    workbook.save("string-values.xlsx");
}

Formula Storage

Formulas are stored as strings via Cell.setFormula(). The cell type changes to CellValueType.FORMULA and the formula text round-trips through save and reload. Excel recalculates the result when the file is opened:

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()) {
    WorksheetCollection sheets = workbook.getWorksheets();
    Worksheet sheet = sheets.get(0);
    Cell b1 = sheet.getCells().get("B1");
    b1.putValue(100.0);
    Cell c1 = sheet.getCells().get("C1");
    c1.setFormula("=B1*1.2");
    System.out.println(c1.getType() == CellValueType.FORMULA);  // true
    workbook.save("formulas.xlsx");
}

See Also

 English