Workbooks & Sheets¶
The Excel object is the whole workbook. A workbook contains one
or more Sheet objects, keyed by name.
Creating and loading a workbook¶
import com.gyanoba.kexcel.Excel
val blank = Excel.createExcel() // one sheet, "Sheet1"
val fromBytes = Excel.decodeBytes(bytes) // from a ByteArray
val fromStream = Excel.decodeStream(input) // from an InputStream
Only .xlsx is supported
decodeBytes / decodeStream throw UnsupportedOperationException for
anything that isn't an Office Open XML spreadsheet (for example, legacy
.xls files).
Accessing sheets¶
Index the workbook by name with the [] operator. If the sheet doesn't exist,
it is created on demand — this is the idiomatic way to add a new sheet:
Get every sheet as a map:
Read a sheet's dimensions (both are 0-based counts of the used range):
Sheet operations¶
All of the following are methods on the Excel workbook object.
Copy¶
Copies the contents of one sheet into another. The destination is created if it doesn't exist, and receives an independent copy:
Rename¶
Renames a sheet. The old name must exist and the new name must not:
Delete¶
Deletes a sheet. A workbook always keeps at least one sheet, so a delete that would empty the workbook is silently ignored:
The default sheet¶
The default sheet is the one shown first when the file is opened:
excel.setDefaultSheet("Sheet1") // returns true on success
val name: String? = excel.getDefaultSheet()
Linking sheets¶
Two names can point at the same underlying Sheet object. After linking,
edits through either name affect both:
excel.link("Alias", excel["Sheet1"]) // "Alias" and "Sheet1" now share data
excel.unLink("Alias") // break the link; "Alias" keeps a copy
Compare this with copy, which always produces independent data.
link vs set
Assigning with excel["Name"] = otherSheet (the set operator) clones
otherSheet into a new, unlinked sheet. Use link when you specifically want
the two names to stay in sync.
The tables property¶
For compatibility with the original Dart API, excel.tables returns the same
Map<String, Sheet> as getSheets(). Prefer getSheets() in new code.
Right-to-left sheets¶
Each sheet exposes an isRTL flag for right-to-left layouts:
Method reference¶
| Operation | Call |
|---|---|
| Get / create a sheet | excel["Name"] |
| All sheets | excel.getSheets() |
| Copy a sheet | excel.copy(from, to) |
| Rename a sheet | excel.rename(old, new) |
| Delete a sheet | excel.delete(name) |
| Set default sheet | excel.setDefaultSheet(name) |
| Get default sheet | excel.getDefaultSheet() |
| Link two names | excel.link(name, sheet) |
| Break a link | excel.unLink(name) |