728x90
@GetMapping(value = ["/excel"])
fun excel(res: HttpServletResponse,excelReq: ExcelReq/*, excelForm: ExcelForm*/) {
val savePath = "/Users/jwkim_macbook/Desktop/exceltest.xlsx"
try {
XSSFWorkbook().use { wb2 ->
ByteArrayOutputStream().use { fileOut ->
FileOutputStream(savePath).use { fos ->
val sheet1: Sheet = wb2.createSheet("Sheet1")
val style: CellStyle = wb2.createCellStyle()
style.setBorderBottom(BorderStyle.THIN)
val header: Row = sheet1.createRow(0);
var headerCell: Cell = header.createCell(0)
headerCell.cellStyle = style
headerCell.setCellValue("no")
headerCell = header.createCell(1)
headerCell.cellStyle = style
headerCell.setCellValue("no1")
headerCell = header.createCell(2)
headerCell.cellStyle = style
headerCell.setCellValue("no2")
headerCell = header.createCell(3)
headerCell.cellStyle = style
headerCell.setCellValue("no3")
//Write Excel File
val excelList = excelManager.abnormal()
excelList.forEachIndexed { index, excelInfo ->
val row: Row = sheet1.createRow(index+1)
var cell: Cell = row.createCell(0)
cell.setCellValue(excelInfo.num1)
cell = row.createCell(1)
cell.setCellValue(excelInfo.num2)
cell = row.createCell(2)
cell.setCellValue(excelInfo.num3)
cell = row.createCell(3)
cell.setCellValue(excelInfo.num4)
}
wb2.write(fileOut)
val filein: InputStream = ByteArrayInputStream(fileOut.toByteArray())
val opc: OPCPackage = OPCPackage.open(filein)
val fileSystem = POIFSFileSystem()
val encryptionInfo = EncryptionInfo(EncryptionMode.agile)
val encryptor: Encryptor = encryptionInfo.encryptor
encryptor.confirmPassword(excelReq.pw)
opc.save(encryptor.getDataStream(fileSystem))
fileSystem.writeFilesystem(fos)
}
}
}
} catch (e: IOException) {
logger.error(e.message, e)
} catch (e: InvalidFormatException) {
logger.error(e.message, e)
} catch (e: GeneralSecurityException) {
logger.error(e.message, e)
}
val attachFile = File(savePath)
val inputStream: InputStream = FileInputStream(attachFile)
try{
val encodedFileName =
"${URLEncoder.encode("exceltest", "UTF-8").replace("+", "%20")}.xlsx"
//val encodedFileName = "exceltest.xlsx"
//val ext = ".${attachFile.extension}"
res.addHeader(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"$encodedFileName\";
filename*=utf-8''$encodedFileName"
)
res.setContentLengthLong(attachFile.length())
res.addHeader(HttpHeaders.PRAGMA, "No-cache") //HTTP 1.0
res.addDateHeader(HttpHeaders.EXPIRES, 0)
res.addHeader(HttpHeaders.CACHE_CONTROL, "no-cache")
res.contentType = Files.probeContentType(attachFile.toPath())
IOUtils.copy(inputStream, res.outputStream)
}catch(e: IOException){
e.printStackTrace()
throw ErrorResp.getInternalServerError().apply {
addError("attachFile", "e", e.localizedMessage)
}
}finally {
IOUtils.closeQuietly(inputStream)
res.flushBuffer()
}
}
별다른 설명 필요없는 간단한 excel 쓰기 및 다운로드.
728x90
'프로그래밍 공부 > Spring Boot' 카테고리의 다른 글
[Slack] slack bot message (0) | 2022.10.19 |
---|---|
[java] spring boot oauth2 refresh token 고민 (0) | 2022.07.13 |
[Java] spring boot - firebase message server (11) | 2021.07.27 |
[Spring Boot/ldaps] AD 연동 (0) | 2020.07.24 |
[GraphQL] Spring Boot + 그래프QL 사용하기 (CRUD) (4) | 2019.07.08 |