ExcelUtil

Properties

Link copied to clipboard
const val APP_NAME: String
Link copied to clipboard
const val APP_VERSION: String
Link copied to clipboard
const val DEFAULT_MAX_CHAR_IN_CELL: Int = 512
Link copied to clipboard
val logger: KLogger
Link copied to clipboard
const val MAX_SHEET_NAME_LENGTH: Int = 31

Excel's hard limit on worksheet-name length.

Functions

Link copied to clipboard
fun columnSize(sheet: Sheet, columnIndex: Int): Int

fastexcel has no random row access, so we materialize the sheet to walk up from the bottom. Acceptable since this is used only for sizing.

Link copied to clipboard

Replacement for POI's WorkbookUtil.createSafeSheetName. Replaces invalid characters with spaces and truncates to 31 chars.

Link copied to clipboard
fun createSheet(workbook: Workbook, sheetName: String): Worksheet

Creates a new worksheet with a safe name. fastexcel does not expose an "already exists" check, so callers needing collision-free names should track the names they have used and pass a unique value.

Link copied to clipboard
fun exportAsWorkSheet(resultSet: ResultSet, ws: Worksheet, writeHeader: Boolean = true)

Streams a JDBC ResultSet to a worksheet. The ResultSet is closed when done.

Link copied to clipboard
fun exportTablesToExcel(db: DatabaseIfc, path: Path, tableNames: List<String>, schemaName: String?)

Writes the supplied tables of a database to one workbook, one sheet per table.

Link copied to clipboard
fun importSheetToTable(db: DatabaseIfc, sheet: Sheet, tableName: String, tblMetaData: List<ColumnMetaData>, schemaName: String?, numRowsToSkip: Int, rowBatchSize: Int = 100, unCompatibleRows: PrintWriter): Boolean
Link copied to clipboard
fun importWorkbookToSchema(db: DatabaseIfc, pathToWorkbook: Path, tableNames: List<String>, schemaName: String?, skipFirstRow: Boolean)

Imports each named sheet of the workbook into the table of the same name. Sheets are processed in the supplied order to satisfy referential constraints.

Link copied to clipboard
fun isCellEmpty(cell: Cell?): Boolean
Link copied to clipboard
fun numberColumns(row: Row): Int
Link copied to clipboard
fun numberColumnsForCSVHeader(sheet: Sheet): Int

Opens a one-row stream just to size the header.

Link copied to clipboard
fun openReadableWorkbook(pathToWorkbook: Path): ReadableWorkbook?

Opens an .xlsx workbook for reading. Returns null on missing file or read error. Caller is responsible for closing the returned ReadableWorkbook.

Link copied to clipboard
fun readCellAsObject(cell: Cell): Any?

Translates a fastexcel cell into a "best fit" Java object.

Link copied to clipboard
fun readCellAsString(cell: Cell): String

Translates a fastexcel cell into a String.

Link copied to clipboard
fun readCellForJdbcType(cell: Cell?, jdbcType: Int): Any?

Translates a fastexcel cell into a value compatible with PreparedStatement.setObject(idx, value) for a column whose JDBC type is jdbcType (a constant from java.sql.Types).

Link copied to clipboard
fun readRowAsObjectList(row: Row, numColumns: Int = row.cellCount): List<Any?>

Reads a row into a fixed-length list, padding missing cells with null. fastexcel's Row.getCell throws on out-of-bounds indices (POI returned null), so we bounds-check before reading.

Link copied to clipboard
fun readRowAsStringArray(row: Row, numCol: Int, maxChar: Int = DEFAULT_MAX_CHAR_IN_CELL): Array<String?>
Link copied to clipboard
fun readRowAsStringList(row: Row, numCol: Int, maxChar: Int = DEFAULT_MAX_CHAR_IN_CELL): List<String?>
Link copied to clipboard
fun readRowForJdbcTypes(row: Row, jdbcTypes: List<Int>): List<Any?>

Reads a row coerced to the supplied per-column JDBC types. Cells beyond the row's Row.cellCount are read as null.

Link copied to clipboard
fun readSheetAsObjects(sheet: Sheet, numColumns: Int = numberColumnsForCSVHeader(sheet), skipFirstRow: Boolean = false): List<List<Any?>>

Reads an entire sheet as a list of object lists. fastexcel streams rows; the stream is closed when this returns.

Link copied to clipboard
fun readToMap(sheetName: String, pathToWorkbook: Path = KSL.excelDir.resolve("${sheetName}.xlsx"), skipFirstRow: Boolean = false): Map<String, Double>

Inverse of writeToExcel(Map).

Link copied to clipboard
fun writeCell(ws: Worksheet, row: Int, col: Int, value: Any?)

Writes a value to (row, col) of the worksheet. Date/time types receive an Excel format style. fastexcel deduplicates styles internally, so the per-cell style call is cheap (unlike POI).

Link copied to clipboard
fun writeSheetToCSV(sheet: Sheet, numCol: Int = numberColumnsForCSVHeader(sheet), skipFirstRow: Boolean = false, pathToCSV: Path = KSL.outDir.resolve("${sheet.name}.csv"), maxChar: Int = DEFAULT_MAX_CHAR_IN_CELL)
Link copied to clipboard
fun writeToExcel(map: Map<String, Double>, sheetName: String, wbName: String = sheetName, wbDirectory: Path = KSL.excelDir, header: Boolean = false)

Writes a Map to a single-sheet workbook. NaN / +Infinity / -Infinity are written as the corresponding strings.