1.创建springboot工程,勾选web
项目结构
2.pom.xml依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
3.编写下载工具类
public class DownloadUtils {
public void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName) throws IOException {
response.setContentType("application/octet-stream");
returnName = response.encodeURL(new String(returnName.getBytes(),"iso8859-1"));
response.addHeader("Content-Disposition","attachment;filename="+returnName);
response.setContentLength(byteArrayOutputStream.size());
response.addHeader("Content-Length", "" + byteArrayOutputStream.size());
ServletOutputStream outputstream = response.getOutputStream();
byteArrayOutputStream.writeTo(outputstream);
byteArrayOutputStream.close();
outputstream.flush();
}
}
4.创建实体类
public class User {
private Integer id;
private String name;
private String phone;
private Integer pwd;
private String bornDate;
public User() {
}
public User(Integer id, String name, String phone, Integer pwd, String bornDate) {
this.id = id;
this.name = name;
this.phone = phone;
this.pwd = pwd;
this.bornDate = bornDate;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Integer getPwd() {
return pwd;
}
public void setPwd(Integer pwd) {
this.pwd = pwd;
}
public String getBornDate() {
return bornDate;
}
public void setBornDate(String bornDate) {
this.bornDate = bornDate;
}
}
5.controller
@RestController
public class IndexController {
@RequestMapping("/index")
public void index(HttpServletResponse response) throws IOException {
List<User> list = new ArrayList<User>();
User u1 = new User(1, "zs", "13232", 444, "20110102");
User u2 = new User(2, "r3", "5454", 32, "20110102");
User u3 = new User(3, "ad", "23232", 547, "20110102");
User u4 = new User(4, "fd", "7565", 776, "20110102");
list.add(u1);
list.add(u2);
list.add(u3);
list.add(u4);
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
String[] titles = "编号,姓名,手机号,密码,出生日期".split(",");
Row row = sheet.createRow(0);
int titleIndex = 0;
for(String title:titles){
Cell cell = row.createCell(titleIndex++);
cell.setCellValue(title);
}
int rowIndex = 1;
for(User u:list){
row = sheet.createRow(rowIndex++);
Cell cell = row.createCell(0);
cell.setCellValue(u.getId());
cell = row.createCell(1);
cell.setCellValue(u.getName());
cell = row.createCell(2);
cell.setCellValue(u.getPhone());
cell = row.createCell(3);
cell.setCellValue(u.getPwd());
cell = row.createCell(4);
cell.setCellValue(u.getBornDate());
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
wb.write(os);
new DownloadUtils().download(os,response,"人事报表.xlsx");
}
}
6.index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="/index">导出</a>
</body>
</html>
7.启动测试