Working with Core Cell Operations

Lavorare con le operazioni di base delle celle

Panoramica

L’API core delle celle supporta valori tipizzati, memorizzazione di formule, ispezione del tipo di valore tramite CellValueType e una rappresentazione stringa uniforme tramite getStringValue().

Memorizzazione di valori tipizzati

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

Valori Enum CellValueType

Enum ValueDescription
STRINGValore di testo memorizzato da putValue(String)
NUMBERValore numerico memorizzato da putValue(int) o putValue(double)
BOOLEANValore booleano memorizzato da putValue(boolean)
DATE_TIMEValore data/ora memorizzato da putValue(LocalDateTime)
FORMULAStringa di formula memorizzata da setFormula(String)
BLANKCella vuota — nessun valore è stato impostato

Ottenere valori stringa

cell.getStringValue() restituisce una stringa indipendente dalla locale adatta per la visualizzazione o l’elaborazione a valle, indipendentemente dal tipo di valore sottostante:

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(123);
    System.out.println(b1.getStringValue());  // "123" — no decimal point
    workbook.save("string-values.xlsx");
}

Archiviazione della formula

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");
    System.out.println(c1.getType() == CellValueType.FORMULA);  // true
    workbook.save("formulas.xlsx");
}
 Italiano