@@ -82,7 +82,7 @@ public fun DataFrame.Companion.readExcel(
82
82
return wb.use { readExcel(it, sheetName, columns, rowsCount) }
83
83
}
84
84
85
- internal fun DataFrame.Companion.readExcel (
85
+ public fun DataFrame.Companion.readExcel (
86
86
wb : Workbook ,
87
87
sheetName : String? = null,
88
88
columns : String? = null,
@@ -91,7 +91,14 @@ internal fun DataFrame.Companion.readExcel(
91
91
val sheet: Sheet = sheetName
92
92
?.let { wb.getSheet(it) ? : error(" Sheet with name $sheetName not found" ) }
93
93
? : wb.getSheetAt(0 )
94
+ return readExcel(sheet, columns, rowsCount)
95
+ }
94
96
97
+ public fun DataFrame.Companion.readExcel (
98
+ sheet : Sheet ,
99
+ columns : String? = null,
100
+ rowsCount : Int? = null
101
+ ): AnyFrame {
95
102
val columnIndexes = if (columns != null ) {
96
103
columns.split(" ," ).flatMap {
97
104
if (it.contains(" :" )) {
@@ -108,7 +115,12 @@ internal fun DataFrame.Companion.readExcel(
108
115
val headerRow = sheet.getRow(0 )
109
116
val valueRows = sheet.drop(1 ).let { if (rowsCount != null ) it.take(rowsCount) else it }
110
117
val columns = columnIndexes.map { index ->
111
- val name = headerRow.getCell(index)?.stringCellValue ? : CellReference .convertNumToColString(index)
118
+ val headerCell = headerRow.getCell(index)
119
+ val name = if (headerCell?.cellType == CellType .NUMERIC ) {
120
+ headerCell.numericCellValue.toString() // Support numeric-named columns
121
+ } else {
122
+ headerCell?.stringCellValue ? : CellReference .convertNumToColString(index) // Use Excel column names if no data
123
+ }
112
124
val values = valueRows.map {
113
125
val cell: Cell ? = it.getCell(index)
114
126
when (cell?.cellType) {
@@ -171,6 +183,17 @@ public fun <T> DataFrame<T>.writeExcel(
171
183
factory : () -> Workbook
172
184
) {
173
185
val wb: Workbook = factory()
186
+ writeExcel(wb, columnsSelector, sheetName, writeHeader)
187
+ wb.write(outputStream)
188
+ wb.close()
189
+ }
190
+
191
+ public fun <T > DataFrame<T>.writeExcel (
192
+ wb : Workbook ,
193
+ columnsSelector : ColumnsSelector <T , * > = { all() },
194
+ sheetName : String? = null,
195
+ writeHeader : Boolean = true
196
+ ): Sheet {
174
197
val sheet = if (sheetName != null ) {
175
198
wb.createSheet(sheetName)
176
199
} else {
@@ -189,6 +212,12 @@ public fun <T> DataFrame<T>.writeExcel(
189
212
i++
190
213
}
191
214
215
+ val createHelper = wb.creationHelper
216
+ val cellStyleDate = wb.createCellStyle()
217
+ val cellStyleDateTime = wb.createCellStyle()
218
+ cellStyleDate.dataFormat = createHelper.createDataFormat().getFormat(" dd.mm.yyyy" )
219
+ cellStyleDateTime.dataFormat = createHelper.createDataFormat().getFormat(" dd.mm.yyyy hh:mm:ss" )
220
+
192
221
columns.forEachRow {
193
222
val row = sheet.createRow(i)
194
223
it.values().forEachIndexed { index, any ->
@@ -198,12 +227,21 @@ public fun <T> DataFrame<T>.writeExcel(
198
227
if (any != null ) {
199
228
val cell = row.createCell(index)
200
229
cell.setCellValueByGuessedType(any)
230
+
231
+ when (any) {
232
+ is LocalDate , is kotlinx.datetime.LocalDate -> {
233
+ cell.cellStyle = cellStyleDate
234
+ }
235
+ is LocalDateTime , is kotlinx.datetime.LocalDateTime , is Calendar , is Date -> {
236
+ cell.cellStyle = cellStyleDateTime
237
+ }
238
+ else -> {}
239
+ }
201
240
}
202
241
}
203
242
i++
204
243
}
205
- wb.write(outputStream)
206
- wb.close()
244
+ return sheet
207
245
}
208
246
209
247
private fun Cell.setCellValueByGuessedType (any : Any ) {
0 commit comments