Core Cell Operations

Cómo trabajar con las operaciones de células nucleares en Java

Visión general

El Cell clase es la unidad central para almacenar y leer los datos de la hoja. esta guía cubre el almacenamiento de valor tipado, la inspección de tipo, el almacén de fórmula y el acceso a valor de rango.

Almacenamiento de valores tipizados

Cell.putValue() Se aceptan seis tipos de Java: String, int, double, boolean, LocalDateTime,y, Object.Cada llamada almacena el valor y determina la CellValueType devuelto por cell.getType().Utilice la sobrecarga tipificada que corresponde a los datos — llamando putValue(int) El equipo se almacena con un ente que ronda sin ganar un punto decimal:

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 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 de tipo valor

llamadas cell.getType() Para obtener el CellValueType valor enum para cualquier valor almacenado. uso cell.getStringValue() para obtener una representación de la línea local-independente, independientemente del tipo subyacente:

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

Almacenamiento de fórmulas

llamadas cell.setFormula() con una línea de fórmula estándar Excel. el tipo de célula cambia a CellValueType.FORMULA y la fórmula se conserva en el archivo XLSX; Excel recalcula el resultado cuando se abre el fichero:

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(100.0);
    Cell c1 = sheet.getCells().get("C1");
    c1.setFormula("=B1*1.2");
    workbook.save("formulas.xlsx");
}

Ver también

 Español