Cara Bekerja dengan Operasi Sel Inti di Java
Gambaran Umum
Kelas Cell adalah unit inti untuk menyimpan dan membaca data spreadsheet. Panduan ini mencakup penyimpanan nilai bertipe, inspeksi tipe, penyimpanan formula, dan akses nilai string.
Menyimpan Nilai Bertipe
Gunakan cell.putValue() untuk semua tipe primitif:
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");
}Memeriksa Tipe Nilai
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");
}Menyimpan Rumus
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");
}