Arbeiten mit Excel in Kotlin: Bibliotheken Apache POI und Kotlin-Wrapper

Online Python-Trainer für Anfänger

Lernen Sie Python einfach ohne theoretische Überlastung. Lösen Sie praktische Aufgaben mit automatischer Überprüfung und schreiben Sie Code direkt im Browser.

Kurs starten

Arbeiten mit Excel in Kotlin: Apache POI und Wrapper-Bibliotheken



Kotlin als moderne Programmiersprache für die JVM bietet hervorragende Möglichkeiten für die Arbeit mit Tabellendaten. Obwohl das Kotlin-Ökosystem keine native Unterstützung für das Format .xlsx bietet, können Sie bewährte Java-Bibliotheken verwenden, die an die Kotlin-Syntax angepasst sind. In diesem Artikel betrachten wir das Hauptwerkzeug — Apache POI — und zwei beliebte Kotlin-Wrapper: poi-kotlin und kotlinpoi.



1. Apache POI — die Grundlage für die Arbeit mit Excel



Apache POI ist eine ausgereifte Java-Bibliothek zum Lesen und Schreiben von Dateien im Microsoft-Office-Format (OLE2 und OOXML). Für die Arbeit mit modernen .xlsx-Dateien wird das Untermodul poi-ooxml verwendet. Fügen Sie die Abhängigkeit in build.gradle.kts hinzu:



dependencies {    implementation("org.apache.poi:poi:5.3.0")    implementation("org.apache.poi:poi-ooxml:5.3.0")}


Excel-Datei in Kotlin lesen

Beispiel zum Lesen von Daten aus dem ersten Blatt:

import org.apache.poi.ss.usermodel.*import org.apache.poi.xssf.usermodel.XSSFWorkbookimport java.io.FileInputStream

fun readExcel(filePath: String) { val file = FileInputStream(filePath) val workbook: Workbook = XSSFWorkbook(file) val sheet: Sheet = workbook.getSheetAt(0)

for (row in sheet) { for (cell in row) { when (cell.cellType) { CellType.STRING -> print("${cell.stringCellValue} ") CellType.NUMERIC -> print("${cell.numericCellValue} ") else -> print(" ") } } println() } workbook.close()}


Excel-Datei erstellen

Erstellen wir eine einfache Tabelle mit Überschriften:

import org.apache.poi.xssf.usermodel.XSSFWorkbookimport java.io.FileOutputStream

fun createExcel() { val workbook = XSSFWorkbook() val sheet = workbook.createSheet("Daten")

// Überschriften val headerRow = sheet.createRow(0) headerRow.createCell(0).setCellValue("Name") headerRow.createCell(1).setCellValue("Alter") headerRow.createCell(2).setCellValue("Stadt")

// Daten val data = listOf( listOf("Anna", 25, "Moskau"), listOf("Ivan", 30, "SPb"), listOf("Maria", 22, "Kasan") )

data.forEachIndexed { index, rowData -> val row = sheet.createRow(index + 1) rowData.forEachIndexed { cellIndex, value -> row.createCell(cellIndex).setCellValue(value.toString()) } }

workbook.write(FileOutputStream("output.xlsx")) workbook.close()}


2. Bibliothek poi-kotlin — Kotlin-first Wrapper



poi-kotlin (Version 0.3.0) ist ein leichtgewichtiger Wrapper über Apache POI, der für Kotlin typische Erweiterungsfunktionen und DSL-Syntax hinzufügt. Installation:



repositories {    maven("https://jitpack.io")}dependencies {    implementation("com.github.nicklausw.poi-kotlin:poi-kotlin:0.3.0")}


Beispiel mit poi-kotlin

import com.github.nicklausw.poi.kotlin.*

fun dslExample() { val workbook = workbook { sheet("Bericht") { row { cell("Produkt") cell("Preis") cell("Menge") } row { cell("Laptop") cell(1200.0) cell(5) } row { cell("Maus") cell(25.5) cell(10) } } } workbook.writeToFile("report.xlsx")}

Der Code ist lesbarer geworden, und die verschachtelten Blöcke sheet { row { ... } } erinnern an den Aufbau von HTML-Markup.



3. Bibliothek kotlinpoi — funktionaler Ansatz



kotlinpoi (Vers

Empfehlungen