com.atlan.pkg.serde.xls.ExcelReader.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of package-toolkit-runtime Show documentation
Show all versions of package-toolkit-runtime Show documentation
Atlan custom package runtime toolkit
/* SPDX-License-Identifier: Apache-2.0
Copyright 2023 Atlan Pte. Ltd. */
package com.atlan.pkg.serde.xls
import org.apache.poi.ss.usermodel.CellType
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import java.io.Closeable
import java.io.FileInputStream
import java.io.IOException
import java.math.BigDecimal
/**
* Utility class for parsing and reading the contents of Excel files, using Apache POI.
*
* @param fileLocation location of the Excel file
*/
class ExcelReader(
private val fileLocation: String,
) : Closeable {
private val workbook: Workbook
init {
val file = FileInputStream(fileLocation)
workbook = XSSFWorkbook(file)
}
/**
* Indicates whether the file has the specified sheet within it (true) or not (false).
*
* @return boolean indicating whether the sheet is present (true) or not (false)
*/
fun hasSheet(name: String): Boolean {
return workbook.getSheet(name) != null
}
/**
* Retrieve all rows from the specified sheet of the Excel workbook, by default using the
* very first row (0) as the header row.
*
* @param index the index (0-based) of the worksheet within the workbook
* @param headerRow index of the row containing headers (0-based)
* @return a list of rows, each being a mapping from column name to its value
*/
fun getRowsFromSheet(
index: Int,
headerRow: Int = 0,
): List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy