Introduction
การ Cell class เป็นหน่วยพื้นฐานใน Aspose.เซลล์ FOSS สําหรับ Java. มันเก็บข้อมูล ค่าที่เขียนออกมา แสดงแบบ runtime ของมันผ่านการใช้งานของ CellValueType อึม, ยังคงอยู่ สายสูตรสําหรับ Excel เพื่อประเมิน และคืนตัวแทนที่มีรูปแบบสตริง ผ่าน getStringValue().บทความนี้ครอบคลุมการเก็บค่าที่เขียนแบบ, การตรวจสอบประเภท runtime, ความยั่งยืนของสูตร และการเรียงลําดับ XLSX ไป-กลับพร้อมตัวอย่างรหัสที่ถูกตรวจ.
เริ่มต้นอย่างรวดเร็ว
เพิ่มการพึ่งพาของ Maven ต่อไปกับของคุณ pom.xml เพื่อเริ่มใช้ Aspose.เซลล์ FOSS:
<dependency>
<groupId>org.aspose</groupId>
<artifactId>aspose-cells-foss</artifactId>
<version>26.5.0</version>
</dependency>Core Cell API
การเก็บค่าที่เขียนด้วย putValue (()))
Cell.putValue() รับ 6 แบบ Java: String, int, double, boolean, LocalDateTime,และ Object. การเรียกแต่ละครั้งจะตั้งค่าภายในเซลล์ และกําหนด การ CellValueType กลับโดย cell.getType().
import com.aspose.cells_foss.Cell;
import com.aspose.cells_foss.CellValueType;
import com.aspose.cells_foss.Workbook;
import com.aspose.cells_foss.Worksheet;
import java.time.LocalDateTime;
try (Workbook workbook = new Workbook()) {
WorksheetCollection sheets = workbook.getWorksheets();
Worksheet sheet = sheets.get(0);
Cell a1 = sheet.getCells().get("A1");
a1.putValue("Revenue");
assert a1.getType() == CellValueType.STRING;
Cell b1 = sheet.getCells().get("B1");
b1.putValue(42500.75);
assert b1.getType() == CellValueType.NUMBER;
Cell c1 = sheet.getCells().get("C1");
c1.putValue(true);
assert c1.getType() == CellValueType.BOOLEAN;
Cell d1 = sheet.getCells().get("D1");
d1.putValue(LocalDateTime.of(2026, 4, 27, 9, 0, 0));
assert d1.getType() == CellValueType.DATE_TIME;
workbook.save("typed.xlsx");
}
เซลล์ว่างกลับ CellValueType.BLANK จาก getType().ค่าตัวสมบูรณ์ที่เก็บไว้กับ putValue(int) การเดินทางไป-กลับผ่าน XLSX รุ่นโดยไม่ได้รับจุดทศนิยม getStringValue() การคืนสินค้า "123" ไม่ "123.0".
การตรวจสอบค่าในเวลาทํางาน
cell.getValue() กลับค่าเก็บสกัดเป็น Object. cell.getStringValue() กลับตัวแทนเชือกที่ไม่ขึ้นอยู่กับสถานที่ เหมาะสําหรับการประมวลผลด้านล่าง หรือการแสดง. สาขาบน CellValueType เป็นรูปแบบที่แนะนําสําหรับการใช้งานประเภทปลอดภัย การสกัดค่านิยม:
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);
sheet.getCells().get("A1").putValue("Hello");
sheet.getCells().get("B1").putValue(123);
sheet.getCells().get("C1").putValue(true);
sheet.getCells().get("D1").putValue(12.5d);
sheet.getCells().get("G1").setFormula("=B1*2");
Cell b1 = sheet.getCells().get("B1");
if (b1.getType() == CellValueType.NUMBER) {
System.out.println("Number: " + b1.getStringValue());
}
workbook.save("inspect.xlsx");
}
การเก็บของสูตร
สายสูตรถูกเก็บไว้กับ cell.setFormula(). สายสูตรที่เก็บเริ่มต้น มี = และตามการออกแบบสูตร Excel มาตรฐาน. getType() วิธีการคืน CellValueType.FORMULA สําหรับเซลล์สูตร Aspose.เซิล FOSS สําหรับ Java store และ สูตรการเดินทางไป-กลับใน XLSX; สสูตรจะคํานวณใหม่โดย Excel ในแบบเปิด.
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(100.0);
Cell c1 = sheet.getCells().get("C1");
c1.setFormula("=B1*1.2");
assert c1.getType() == CellValueType.FORMULA;
workbook.save("formulas.xlsx");
}
XLSX ความ ซื่อ สัตย์ ใน การ ไป-กลับ
ค่าทับทั้งหมดถูกรักษาผ่านการเรียง XLSX และรีโลด. การโหลดตัวแวร์ที่ใช้ในระบบนี้คือ: อัตราการใช้งาน .xlsx การใช้ไฟล์ new Workbook(path) หรือ new Workbook(path, options) มี LoadOptions เพื่อเปิดโหมดการซ่อมแซม หลังจากชาร์ทใหม่, getType() และ getStringValue() กลับค่าเดิมๆกับเวลาเขียน.
import com.aspose.cells_foss.LoadIssue;
import com.aspose.cells_foss.LoadOptions;
import com.aspose.cells_foss.Workbook;
import com.aspose.cells_foss.Worksheet;
LoadOptions options = new LoadOptions();
options.setStrictMode(false);
options.setTryRepairPackage(true);
options.setTryRepairXml(true);
try (Workbook workbook = new Workbook("input.xlsx", options)) {
if (workbook.getLoadDiagnostics().hasRepairs()) {
for (LoadIssue issue : workbook.getLoadDiagnostics().getIssues()) {
System.out.println(issue.getMessage());
}
}
WorksheetCollection sheets = workbook.getWorksheets();
Worksheet sheet = sheets.get(0);
System.out.println(sheet.getCells().get("A1").getStringValue());
workbook.save("output.xlsx");
}
รูปแบบที่สนับสนุน
| Format | Extension | Read | Write |
|---|---|---|---|
| Excel เปิด XML | .xlsx | ✓ | ✓ |
หลักสูตรและใบอนุญาต
Aspose.Cells FOSS for Java is released under the MIT license. You are free to use, ปรับเปลี่ยน และกระจายมันในโครงการส่วนตัว แหล่งเปิดและพาณิชย์ โดยไม่ต้องใช้ระบบที่สามารถสร้างข้อมูลได้ การจํากัด.รหัสแหล่งมีที่ www.e-mail.com github.com/aspose-cells-foss/Aspose.Cells - FOSS สําหรับ Java.