Como Trabalhar com Operações de Células Principais em Java
Visão geral
A classe Cell é a unidade central para armazenar e ler dados de planilhas. Este guia cobre o armazenamento de valores tipados, inspeção de tipos, armazenamento de fórmulas e acesso a valores de string.
Armazenando Valores Tipados
Use cell.putValue() para todos os 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");
}Inspecionando o 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");
}Armazenando 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");
}