Skip to content

Commit

Permalink
add excelsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
rudeh2926 committed Jan 19, 2024
1 parent 936ab11 commit 2bb50e5
Showing 1 changed file with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.xquare.v1userservice.user.spi

import org.apache.poi.ss.usermodel.BorderStyle
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.xssf.usermodel.XSSFCellStyle
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.springframework.stereotype.Component
import org.springframework.web.server.ServerWebExchange
import reactor.core.publisher.Mono

@Component
class CreateExcelSheetSpiImpl {
fun createExcelSheet(serverWebExchange: ServerWebExchange) {
val workbook = createExcelWorkbook()

serverWebExchange.response.headers.add("Content-Disposition", "attachment; filename=spring_excel_download.xlsx")

val response = serverWebExchange.response

val dataMono = Mono.fromCallable {
val outputStream = response.bufferFactory().allocateBuffer()
workbook.write(outputStream.asOutputStream())
outputStream
}

response.writeWith(dataMono)
}

private fun createExcelWorkbook(): Workbook {
val workbook = XSSFWorkbook()
val sheet = workbook.createSheet("Sheet1")
sheet.defaultColumnWidth = 28

val headerCellStyle = createHeaderCellStyle(workbook)

val headerNames = arrayOf("name", "birthDay", "")
var rowCount = 0

// Header
val headerRow: Row = sheet.createRow(rowCount++)
headerNames.forEachIndexed { i, header ->
val headerCell = headerRow.createCell(i)
headerCell.setCellValue(header)
headerCell.cellStyle = headerCellStyle
}

return workbook
}


fun createHeaderCellStyle(workbook: Workbook): XSSFCellStyle {
val font = workbook.createFont().apply {
//color = XSSFColor(255, 255)
}

return (workbook.createCellStyle() as XSSFCellStyle).apply {
setFont(font)
borderLeft = BorderStyle.THIN
borderRight = BorderStyle.THIN
borderTop = BorderStyle.THIN
borderBottom = BorderStyle.THIN
}
}
}

0 comments on commit 2bb50e5

Please sign in to comment.