2
2
.
.
4
4
.
.
1
1
1
1
D
D
o
o
w
w
n
n
l
l
o
o
a
a
d
d
-
-
E
E
x
x
c
c
e
e
l
l
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
This tutorial shows how to use ResponseEntity to download Excel from the Controller.
You can either call
http://localhost:8080/DownloadExcel to directly download Excel (Page Content is not changed)
http://localhost:8080/GetExcel to display download link
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller, @RequestMapping and Tomcat HTTP Server.
http://localhost:8080/GetExcel
Tomcat
getExcel()
MyController
Downloads Test.xlsx
downloadExcel()
http://localhost:8080/DownloadExcel
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_endpoint_return_excel (add Spring Boot Starters from the table)
Edit File: pom.xml (add poi Maven dependencies)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside controllers package)
pom.xml
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
MyController.java
package com.ivoronline.springboot_endpoint_return_excel.controllers;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
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.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
@Controller
public class MyController {
//=======================================================================
// GET EXCEL
//=======================================================================
@ResponseBody
@GetMapping("/GetExcel")
public String getExcel() {
return "<a href='DownloadExcel'> Download Excel </a>";
}
//=======================================================================
// DOWNLOAD EXCEL
//=======================================================================
@ResponseBody
@GetMapping("/DownloadExcel")
public ResponseEntity<StreamingResponseBody> DownloadExcel() {
//CREATE EXCEL FILE
Workbook workBook = new XSSFWorkbook();
//CREATE TABLE
Sheet sheet = workBook.createSheet("My Sheet");
sheet.setColumnWidth(0, 10 * 256); //10 characters wide
//CREATE ROW
Row row = sheet.createRow(0);
//CREATE CELL
Cell cell = row.createCell(0);
cell.setCellValue("Hello World");
//DOWNLOAD EXCEL
return ResponseEntity
.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.header(HttpHeaders.CONTENT_DISPOSITION, "inline;filename=\"Test.xlsx\"")
.body(workBook::write);
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/GetExcel
Test.xslx
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
</dependencies>