Aspose.Cells FOSS for Java ist eine reine Java 17‑Bibliothek, die Entwicklern ermöglicht, Excel‑.xlsx‑Arbeitsmappen zu erstellen, zu laden, zu ändern und zu speichern, ohne dass eine kommerzielle Aspose‑Laufzeitabhängigkeit besteht. Sie stellt eine saubere öffentliche API im Paket com.aspose.cells_foss bereit und wird unter der MIT‑Lizenz veröffentlicht.
Die Bibliothek deckt das Kern‑Tabellenkalkulations‑Objektmodell ab: Arbeitsmappen, Arbeitsblätter, Zellen, Formate und Sammlungen. Unterstützte Funktionen umfassen Zellwerte (Zeichenkette, Zahl, Boolesch, Datum/Uhrzeit und Formel), Zellformatierung (Schriften, Rahmen, Füllungen, Ausrichtung und Zahlenformate), AutoFilter, Datenvalidierung, bedingte Formatierung, Hyperlinks, zusammengeführte Zellen, definierte Namen, Seiteneinrichtung und Blattschutz.
Aspose.Cells FOSS for Java wird mit Maven 3.9+ gebaut und zielt auf Java 17+ ab. Fügen Sie es Ihrem Projekt über eine einzige Maven‑Abhängigkeit hinzu. Das Speichern ist derzeit auf das .xlsx‑Format beschränkt.
Create a workbook, set cell values and styles, adjust row and column dimensions, and save to an .xlsx file using Workbook.save().
import com.aspose.cells_foss.Cell;
import com.aspose.cells_foss.Style;
import com.aspose.cells_foss.Workbook;
import com.aspose.cells_foss.Worksheet;
public class Main {
public static void main(String[] args) {
try (Workbook workbook = new Workbook()) {
Worksheet sheet = workbook.getWorksheets().get(0);
sheet.setName("Report");
sheet.getCells().get("A1").putValue("Revenue");
sheet.getCells().get("B1").putValue(12500.75);
Cell total = sheet.getCells().get("B1");
Style style = total.getStyle();
style.getFont().setBold(true);
style.setCustom("#,##0.00");
total.setStyle(style);
sheet.getCells().getRows().get(0).setHeight(22.0);
sheet.getCells().getColumns().get(1).setWidth(14.5);
workbook.save("report.xlsx");
}
}
}
Load an existing .xlsx file using LoadOptions to enable repair mode, then inspect load diagnostics before saving the modified workbook.
import com.aspose.cells_foss.LoadIssue;
import com.aspose.cells_foss.LoadOptions;
import com.aspose.cells_foss.Workbook;
public class LoadWorkbook {
public static void main(String[] args) {
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());
}
}
workbook.getDocumentProperties().setAuthor("cells-foss");
workbook.save("output.xlsx");
}
}
}
Add a whole-number validation rule and highlight qualifying cells with bold conditional formatting in a single workbook.
import com.aspose.cells_foss.CellArea;
import com.aspose.cells_foss.FormatCondition;
import com.aspose.cells_foss.FormatConditionCollection;
import com.aspose.cells_foss.FormatConditionType;
import com.aspose.cells_foss.OperatorType;
import com.aspose.cells_foss.Style;
import com.aspose.cells_foss.Validation;
import com.aspose.cells_foss.ValidationType;
import com.aspose.cells_foss.Workbook;
import com.aspose.cells_foss.Worksheet;
public class RulesExample {
public static void main(String[] args) {
try (Workbook workbook = new Workbook()) {
Worksheet sheet = workbook.getWorksheets().get(0);
int vi = sheet.getValidations().add(new CellArea(1, 0, 10, 1));
Validation validation = sheet.getValidations().get(vi);
validation.setType(ValidationType.WHOLE_NUMBER);
validation.setOperator(OperatorType.BETWEEN);
validation.setFormula1("1");
validation.setFormula2("100");
int cfIndex = sheet.getConditionalFormattings().add();
FormatConditionCollection conditions = sheet.getConditionalFormattings().get(cfIndex);
conditions.addArea(CellArea.createCellArea("B2", "B11"));
int condIndex = conditions.addCondition(
FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "1", "100");
FormatCondition condition = conditions.get(condIndex);
Style style = condition.getStyle();
style.getFont().setBold(true);
condition.setStyle(style);
workbook.save("rules.xlsx");
}
}
}