Core Cell Operations

Cómo trabajar con operaciones básicas de celdas en Java

Visión general

La clase Cell es la unidad central para almacenar y leer datos de hojas de cálculo. Esta guía cubre el almacenamiento de valores tipados, la inspección de tipos, el almacenamiento de fórmulas y el acceso a valores de cadena.

Almacenando valores tipados

Utilice cell.putValue() para todos los tipos primitivos:

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

try (Workbook workbook = new Workbook()) {
    Worksheet sheet = workbook.getWorksheets().get(0);
    Cell a1 = sheet.getCells().get("A1");
    a1.putValue("Label");        // String
    Cell b1 = sheet.getCells().get("B1");
    b1.putValue(42);             // int
    Cell c1 = sheet.getCells().get("C1");
    c1.putValue(3.14);           // double
    Cell d1 = sheet.getCells().get("D1");
    d1.putValue(true);           // boolean
    workbook.save("typed.xlsx");
}

Inspección del tipo de valor

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(123);
    CellValueType type = b1.getType();
    System.out.println(type);           // NUMBER
    System.out.println(b1.getStringValue());  // "123"
    workbook.save("inspect.xlsx");
}

Almacenando fórmulas

import com.aspose.cells_foss.Cell;
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");
    workbook.save("formulas.xlsx");
}
 Español