All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.peterbecker.xls.Sheet.kt Maven / Gradle / Ivy

Go to download

This is a number of extensions and additional classes to make handling XLS files easier in Kotlin.

There is a newer version: 0.7
Show newest version
package de.peterbecker.xls

import org.apache.poi.ss.SpreadsheetVersion
import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.util.AreaReference
import org.apache.poi.ss.util.CellReference
import java.awt.Dimension

fun Sheet.getValueAt(row: Int, col: Int) = this.getRow(row)?.getCell(col)?.getValue()

fun Sheet.setValueAt(row: Int, col: Int, value: Any?) {
    when (value) {
        null -> {
            val r = this.getRow(row)
            if(r != null) {
                val cell = r.getCell(col)
                if(cell != null) {
                    r.removeCell(cell)
                }
            }
        }
        else -> this.getOrCreateRow(row).getOrCreateCell(col).setValue(value)
    }
}

fun Sheet.getOrCreateRow(row: Int) = this.getRow(row) ?: this.createRow(row)!!

/**
 * Writes data into the provided area and beyond, returning the area used when writing.
 */
fun Sheet.writeToArea(ref: AreaReference, rows: Iterator>): AreaReference {
    val width = ref.lastCell.col - ref.firstCell.col + 1
    var r = 0
    for (row in rows) {
        if (row.size > width) {
            throw RowTooLongException(r, width, row.size)
        }
        for ((c, value) in row.withIndex()) {
            setValueAt(ref.firstCell.row + r, ref.firstCell.col + c, value)
        }
        r++
    }
    return AreaReference(
            ref.firstCell, ref.lastCell.replace(row = ref.firstCell.row + r - 1), SpreadsheetVersion.EXCEL2007
    )
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy