Lesen und Schreiben in Excel mit Kotlin
Aspose.Cells for Java ist eine leistungsstarke Bibliothek, die Entwicklern ermöglicht, Excel-Dateien programmatisch zu manipulieren. Obwohl sie für Java entwickelt wurde, integriert sie sich nahtlos mit Kotlin, dank der vollständigen Interoperabilität von Kotlin mit Java. Dieses Dokument bietet eine Schritt-für-Schritt-Anleitung zum Lesen und Schreiben von Excel-Dateien mit Kotlin und Aspose.Cells for Java.
Voraussetzungen
- Kotlin und Java Development Kit (JDK) installiert.
- Ein Build-Tool (Maven oder Gradle) für Dependency-Management konfiguriert.
Einrichtung von Aspose.Cells in einem Kotlin-Projekt
Fügen Sie die Aspose.Cells-Abhängigkeit zu Ihrem Projekt hinzu:
Für Maven (pom.xml
):
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
</repositories>
<dependencies>
<!-- Aspose.Cells for Java -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>25.2</version>
</dependency>
<!-- Mandatory Bouncy Castle Libraries -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.68</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.68</version>
</dependency>
</dependencies>
Für Gradle (build.gradle.kts
):
repositories {
maven { url = uri("https://releases.aspose.com/java/repo/") }
}
dependencies {
// Aspose.Cells for Java
implementation("com.aspose:aspose-cells:25.2")
// Mandatory Bouncy Castle Libraries
implementation("org.bouncycastle:bcprov-jdk15on:1.68")
implementation("org.bouncycastle:bcpkix-jdk15on:1.68")
}
In Excel schreiben
Dieses Beispiel zeigt, wie man eine neue Excel-Arbeitsmappe erstellt, Zellen mit Daten füllt und die Datei auf Festplatte speichert.
val workbook = Workbook() // Create a new workbook | |
val worksheet: Worksheet = workbook.worksheets[0] | |
// Write headers | |
worksheet.cells.get("A1").putValue("Product") | |
worksheet.cells.get("B1").putValue("Price") | |
// Write data rows | |
worksheet.cells.get("A2").putValue("Laptop") | |
worksheet.cells.get("B2").putValue(999.99) | |
worksheet.cells.get("A3").putValue("Phone") | |
worksheet.cells.get("B3").putValue(699.99) | |
// Save to file | |
workbook.save("products.xlsx") | |
println("Excel file saved successfully!") |
Aus Excel lesen
Dieses Beispiel zeigt, wie eine bestehende Excel-Datei geladen, Zellenwerte gelesen und die Ergebnisse ausgegeben werden.
val workbook = Workbook("products.xlsx") // Load the file | |
val worksheet = workbook.worksheets[0] | |
// Read data from cells | |
val product = worksheet.cells.get("A2").stringValue | |
val price = worksheet.cells.get("B2").doubleValue | |
println("Product: $product, Price: $price") // Output: Product: Laptop, Price: 999.99 |
Erweiterte Operationen
Formeln behandeln
Dieses Beispiel fügt einer Zelle eine Formel (SUM
) hinzu, berechnet die Arbeitsmappe neu und gibt das Ergebnis aus.
val workbook = Workbook() | |
val worksheet = workbook.worksheets[0] | |
// Add values and a formula | |
worksheet.cells.get("A1").putValue(50) | |
worksheet.cells.get("A2").putValue(30) | |
worksheet.cells.get("A3").formula = "=SUM(A1:A2)" | |
// Calculate formulas | |
workbook.calculateFormula() | |
println("Formula result: ${worksheet.cells.get("A3").intValue}") // Output: 80 | |
workbook.save("formulas.xlsx") |
Zellen formatieren
Dieses Beispiel wendet Stil (fetter Text, rote Farbe und zentrierte Ausrichtung) auf eine Zelle an.
val workbook = Workbook() | |
val worksheet = workbook.worksheets[0] | |
// Write data and apply formatting | |
val cell = worksheet.cells.get("A1") | |
cell.putValue("Important Note") | |
val style = cell.style | |
style.font.isBold = true | |
style.font.color = Color.fromArgb(255, 0, 0) // Red text | |
style.horizontalAlignment = TextAlignmentType.CENTER | |
cell.style = style | |
workbook.save("formatted.xlsx") |