You can easily create an Excel file in the spring.
Note: This library supports Spring Boot version 3 and above because the javax
package has been changed to jakarta
.
- The "Spring Excel Extension" is an enhancement designed to facilitate easier extraction of Excel files for users of the Spring framework.
implementation("io.github.Yaklede:spring-excel-extension:0.0.4")
- First, prepare a DTO (Data Transfer Object) class to receive Excel data.
- Secondly, specify the names and order of the Excel columns you want to capture using the @ExcelColumn annotation.
data class ExcelResult(
@ExcelColumn(name = "name", index = 0)
val name: String,
@ExcelColumn(name = "email", index = 1)
val email: String,
@ExcelColumn(name = "phone-number", index = 2)
val phoneNumber: String,
)
- Next, set up a controller in Spring that can handle incoming requests.
- You should receive HttpServletResponse as a parameter in the method signature to handle parameters.
@RestController
@RequestMapping("/excel")
class ExcelController {
@GetMapping
fun getExcel(
response: HttpServletResponse
): Unit {
}
}
- Prepare the data to be generated as an Excel file in the form of a List.
val excelList = mutableListOf<ExcelResult>()
val excelResult1 = ExcelResult(
name = "first",
email = "first email",
phoneNumber = "first phone number"
)
val excelResult2 = ExcelResult(
name = "second",
email = "second email",
phoneNumber = "second phone number"
)
val excelResult3 = ExcelResult(
name = "third",
email = "third email",
phoneNumber = "third phone number"
)
excelList.add(excelResult1)
excelList.add(excelResult2)
excelList.add(excelResult3)
- Call the extension function toExcelFile on the prepared List type. Receive the Excel name and HttpServletResponse as parameters.
excelList.toExcelFile(fileName = "excel-info", response = response)
- Now you should have received the Excel file.
import jakarta.servlet.http.HttpServletResponse
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("/excel")
class ExcelController {
data class ExcelResult(
@ExcelColumn(name = "name", index = 0)
val name: String,
@ExcelColumn(name = "email", index = 1)
val email: String,
@ExcelColumn(name = "phone-number", index = 2)
val phoneNumber: String,
)
@GetMapping
fun getExcel(
response: HttpServletResponse
): Unit {
val excelList = mutableListOf<ExcelResult>()
val excelResult1 = ExcelResult(
name = "first",
email = "first email",
phoneNumber = "first phone number"
)
val excelResult2 = ExcelResult(
name = "second",
email = "second email",
phoneNumber = "second phone number"
)
val excelResult3 = ExcelResult(
name = "third",
email = "third email",
phoneNumber = "third phone number"
)
excelList.add(excelResult1)
excelList.add(excelResult2)
excelList.add(excelResult3)
excelList.toExcelFile(fileName = "excel-info", response = response)
}
}
- click the Send and Download
- enter the URL
No | Name | Phone Number | |
---|---|---|---|
1 | first | first email | first phone number |
2 | second | second email | second phone number |
3 | third | third email | third phone number |