Core Cell Operations

Làm thế nào để làm việc với Core Cell Operations trong Java

Tổng quan

Các Cell lớp là đơn vị cốt lõi để lưu trữ và đọc dữ liệu bảng điều khiển. hướng dẫn này bao gồm lưu trữ giá trị được nhập, kiểm tra kiểu, lưu lượng công thức và truy cập giá cả dòng.

Lưu trữ các giá trị

Cell.putValue() Có 6 loại Java: String, int, double, boolean, LocalDateTime, và Object.Mỗi cuộc gọi lưu trữ giá trị và xác định giá CellValueType Trở lại bởi cell.getType().Sử dụng quá tải được ghi là phù hợp với dữ liệu - gọi putValue(int) Lưu trữ một toàn bộ mà vòng quanh không đạt được điểm mười:

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

Kiểm tra giá trị loại

Call cell.getType() Để có được The CellValueType giá trị enum cho bất kỳ giá cả được lưu trữ. sử dụng cell.getStringValue() để có được một đại diện dây tự do địa phương bất kể loại dưới đây:

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

Lưu trữ công thức

Call cell.setFormula() với một dòng công thức Excel tiêu chuẩn. loại tế bào thay đổi để CellValueType.FORMULA và công thức được lưu trữ trong tệp XLSX; Excel tái tính toán kết quả khi tách:

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

Xem thêm

 Tiếng Việt