Skip to content

Latest commit

 

History

History
165 lines (121 loc) · 3.94 KB

README.md

File metadata and controls

165 lines (121 loc) · 3.94 KB

spring-excel-extension

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.

What is Spring excel extension

  • The "Spring Excel Extension" is an enhancement designed to facilitate easier extraction of Excel files for users of the Spring framework.

Gradle

implementation("io.github.Yaklede:spring-excel-extension:0.0.4")

How to Use

  • 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.

Full Example Code

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)
    }
}

You can test it using both Chrome and Postman!

Postman

img.png

  • click the Send and Download

Chrome

img.png

  • enter the URL

Response Excel File

No Name Email Phone Number
1 first first email first phone number
2 second second email second phone number
3 third third email third phone number