Last active
November 15, 2023 07:29
-
-
Save stykalin/78550cdfa0906c78ca738db583c45032 to your computer and use it in GitHub Desktop.
Get Apache Poi Palette
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.poi.ss.usermodel.FillPatternType | |
import org.apache.poi.ss.usermodel.IndexedColors | |
import org.apache.poi.xssf.usermodel.XSSFWorkbook | |
import org.springframework.http.HttpHeaders | |
import org.springframework.http.MediaType | |
import org.springframework.http.ResponseEntity | |
import org.springframework.web.bind.annotation.GetMapping | |
import org.springframework.web.bind.annotation.RestController | |
import java.io.ByteArrayOutputStream | |
@RestController | |
class PoiController { | |
@GetMapping("/palette") | |
fun getPoiPalette(): ResponseEntity<ByteArray> { | |
val workbook = XSSFWorkbook() | |
val sheet = workbook.createSheet("PALETTE") | |
IndexedColors.entries.forEachIndexed { index, color -> | |
sheet.createRow(index).apply { | |
createCell(0).apply { | |
setCellValue(color.name) | |
cellStyle = workbook.createCellStyle().apply { | |
fillForegroundColor = color.index | |
fillPattern = FillPatternType.SOLID_FOREGROUND | |
} | |
} | |
createCell(1).apply { setCellValue(" ") } | |
createCell(2).apply { | |
setCellValue(color.name) | |
cellStyle = workbook.createCellStyle().apply { | |
setFont(sheet.workbook.createFont().apply { bold = true; this.color = color.index }) | |
} | |
} | |
} | |
} | |
val out = ByteArrayOutputStream() | |
workbook.use { it.write(out) } | |
val fileName = "poi_palette.xlsx" | |
val result = out.toByteArray() | |
return ResponseEntity.ok() | |
.contentType(MediaType.APPLICATION_OCTET_STREAM) | |
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"$fileName\"") | |
.body(result) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment