Read and Write to Excel With Kotlin

Aspose.Cells for Java is a powerful library that enables developers to manipulate Excel files programmatically. While it is designed for Java, it integrates seamlessly with Kotlin, thanks to Kotlin’s full interoperability with Java. This document provides a step-by-step guide to reading from and writing to Excel files using Kotlin and Aspose.Cells for Java.

Prerequisites

  • Kotlin and Java Development Kit (JDK) installed.
  • A build tool (Maven or Gradle) configured for dependency management.

Setting Up Aspose.Cells in a Kotlin Project

Add the Aspose.Cells dependency to your project:

For 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>

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

Write to Excel

This example demonstrates how to create a new Excel workbook, populate cells with data, and save the file to disk.

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!")

Read from Excel

This example shows how to load an existing Excel file, read cell values, and print the results.

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

Advanced Operations

Handle Formulas

This example adds a formula (SUM) to a cell, recalculates the workbook, and prints the result.

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

Format Cells

This example applies styling (bold text, red color, and center alignment) to a cell.

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