Core Cell Operations

Comment travailler avec les opérations de base sur les cellules en Java

Vue d’ensemble

La classe Cell est l’unité centrale pour le stockage et la lecture des données de feuille de calcul. Ce guide couvre le stockage des valeurs typées, l’inspection des types, le stockage des formules et l’accès aux valeurs de chaîne.

Stockage des valeurs typées

Utilisez cell.putValue() pour tous les types primitifs :

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");
}

Inspection du type de valeur

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");
}

Stockage des formules

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");
}
 Français