导出Java类
package com.shsnc.perform.controller;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.shsnc.perform.vo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/perform")
public class Controller {
/**
* 功能描述: <br>
* 导出excel
*/
@GetMapping("/export/excel")
public void executeSql(HttpServletResponse response) throws IOException {
List<User> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setId("000" + i);
user.setName("tome" + i);
user.setAge(i + "");
user.setHobby("篮球" + i);
user.setAddress("杭州西湖 文一西路 00" + i + "号");
list.add(user);
}
log.info("查询到数据为{}", list);
ExcelWriter writer = ExcelUtil.getWriter();
log.info("response为{}", response);
response.addHeader("Content-Disposition", "attachment; filename=\"" + System.currentTimeMillis() + ".xls\"");//
response.setContentType("application/octet-stream");
writer.addHeaderAlias("id", "编号");
writer.addHeaderAlias("name", "姓名");
writer.addHeaderAlias("age", "年龄");
writer.addHeaderAlias("hobby", "爱好");
writer.addHeaderAlias("address", "住址");
writer.write(list);
OutputStream outputStream = response.getOutputStream();
log.info("输出流为{}", outputStream);
writer.flush(outputStream, true);
log.info("渲染结束", response);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
Pom文件
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.21</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
百度网盘地址
链接:https://pan.baidu.com/s/1o0BB0kz9vf6jORnp2z38qA?pwd=1234
提取码:1234
打开项目 直接启动
访问地址
http://localhost:22770//perform/export/excel
导出结果