Skip to content

Commit 67f47b7

Browse files
committed
Xls post-processing demo
1 parent 0430206 commit 67f47b7

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

docs/StardustDocs/topics/write.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,60 @@ val jsonStr = df.toJson(prettyPrint = true)
5353

5454
You can write your dataframe in XLS, XLSX format to a file or `OutputStream`
5555

56+
<!---FUN writeXls-->
57+
58+
```kotlin
59+
df.writeExcel(file)
60+
```
61+
62+
<!---END-->
63+
5664
Values of ColumnGroup, FrameColumn, i.e. AnyRow, AnyFrame will be serialized as JSON objects.
5765

66+
If you work directly with Apache POI, you can use created Workbook and Sheets in your code:
67+
68+
<!---FUN writeXlsAppendAndPostProcessing-->
69+
70+
```kotlin
71+
/**
72+
* Do something with generated sheets. Here we set bold style for headers and italic style for first data column
73+
*/
74+
fun setStyles(sheet: Sheet) {
75+
val headerFont = sheet.workbook.createFont()
76+
headerFont.bold = true
77+
val headerStyle = sheet.workbook.createCellStyle()
78+
headerStyle.setFont(headerFont)
79+
80+
val indexFont = sheet.workbook.createFont()
81+
indexFont.italic = true
82+
val indexStyle = sheet.workbook.createCellStyle()
83+
indexStyle.setFont(indexFont)
84+
85+
sheet.forEachIndexed { index, row ->
86+
if (index == 0) {
87+
for (cell in row) {
88+
cell.cellStyle = headerStyle
89+
}
90+
} else {
91+
row.first().cellStyle = indexStyle
92+
}
93+
}
94+
}
95+
96+
// Create a workbook (or use existing)
97+
val wb = WorkbookFactory.create(true)
98+
99+
// Create different sheets from different data frames in the workbook
100+
val allPersonsSheet = df.writeExcel(wb, sheetName = "allPersons")
101+
val happyPersonsSheet = df.filter { person -> person.isHappy }.remove("isHappy").writeExcel(wb, sheetName = "happyPersons")
102+
val unhappyPersonsSheet = df.filter { person -> !person.isHappy }.remove("isHappy").writeExcel(wb, sheetName = "unhappyPersons")
103+
104+
// Do anything you want by POI
105+
listOf(happyPersonsSheet, unhappyPersonsSheet).forEach { setStyles(it) }
106+
107+
// Save the result
108+
wb.write(file.outputStream())
109+
wb.close()
110+
```
111+
112+
<!---END-->

tests/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies {
2020
exclude("org.jetbrains.kotlin", "kotlin-stdlib-jdk8")
2121
}
2222
testImplementation(libs.kotlin.datetimeJvm)
23+
testImplementation(libs.poi)
2324
}
2425

2526
kotlin.sourceSets {

tests/src/test/kotlin/org/jetbrains/kotlinx/dataframe/samples/api/Write.kt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package org.jetbrains.kotlinx.dataframe.samples.api
22

33
import io.kotest.matchers.string.shouldStartWith
44
import org.apache.commons.csv.CSVFormat
5+
import org.apache.poi.ss.usermodel.Sheet
6+
import org.apache.poi.ss.usermodel.WorkbookFactory
7+
import org.jetbrains.kotlinx.dataframe.api.filter
8+
import org.jetbrains.kotlinx.dataframe.api.remove
59
import org.jetbrains.kotlinx.dataframe.io.toCsv
610
import org.jetbrains.kotlinx.dataframe.io.toJson
711
import org.jetbrains.kotlinx.dataframe.io.writeCSV
@@ -64,7 +68,56 @@ class Write : TestBase() {
6468
@Test
6569
fun writeXls() {
6670
useTempFile { file ->
71+
// SampleStart
6772
df.writeExcel(file)
73+
// SampleEnd
74+
}
75+
}
76+
77+
@Test
78+
fun writeXlsAppendAndPostProcessing() {
79+
useTempFile { file ->
80+
// SampleStart
81+
/**
82+
* Do something with generated sheets. Here we set bold style for headers and italic style for first data column
83+
*/
84+
fun setStyles(sheet: Sheet) {
85+
val headerFont = sheet.workbook.createFont()
86+
headerFont.bold = true
87+
val headerStyle = sheet.workbook.createCellStyle()
88+
headerStyle.setFont(headerFont)
89+
90+
val indexFont = sheet.workbook.createFont()
91+
indexFont.italic = true
92+
val indexStyle = sheet.workbook.createCellStyle()
93+
indexStyle.setFont(indexFont)
94+
95+
sheet.forEachIndexed { index, row ->
96+
if (index == 0) {
97+
for (cell in row) {
98+
cell.cellStyle = headerStyle
99+
}
100+
} else {
101+
row.first().cellStyle = indexStyle
102+
}
103+
}
104+
}
105+
106+
// Create a workbook (or use existing)
107+
val wb = WorkbookFactory.create(true)
108+
109+
// Create different sheets from different data frames in the workbook
110+
val allPersonsSheet = df.writeExcel(wb, sheetName = "allPersons")
111+
val happyPersonsSheet = df.filter { person -> person.isHappy }.remove("isHappy").writeExcel(wb, sheetName = "happyPersons")
112+
val unhappyPersonsSheet = df.filter { person -> !person.isHappy }.remove("isHappy").writeExcel(wb, sheetName = "unhappyPersons")
113+
114+
// Do anything you want by POI
115+
listOf(happyPersonsSheet, unhappyPersonsSheet).forEach { setStyles(it) }
116+
117+
// Save the result
118+
wb.write(file.outputStream())
119+
wb.close()
120+
// SampleEnd
68121
}
69122
}
70123

0 commit comments

Comments
 (0)