Note: This article refers to https://www.cnblogs.com/lin02/p/11648899.html
1. Table design
2. Add dependency to pom.xml file
<!-- poi实现excel导入导出-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
3. Write the Controller layer
@RestController
@RequestMapping("/export")
public class UserExportController {
@Autowired
private UserExportService userExportService;
@RequestMapping(value = "/user",method = RequestMethod.GET)
public void userExcel(HttpServletResponse response) throws IOException {
XSSFWorkbook wb = userExportService.show();
String fileName = "User报表.xlsx";
OutputStream outputStream = null;
fileName = URLEncoder.encode(fileName,"UTF-8");
//设置ContentType请求信息格式
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
outputStream = response.getOutputStream();
wb.write(outputStream);
outputStream.flush();
outputStream.close();
}
}
4. Write the Service layer
@Service
public class UserExportService {
@Autowired
private UserExportMapper userExportMapper;
public XSSFWorkbook show() {
List<User> list = userExportMapper.selectByExample(null);//查出数据库数据
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Goods");//创建一张表
Row titleRow = sheet.createRow(0);//创建第一行,起始为0
titleRow.createCell(0).setCellValue("uid");//第一列
titleRow.createCell(1).setCellValue("用户名");
titleRow.createCell(2).setCellValue("密码");
titleRow.createCell(3).setCellValue("姓名");
titleRow.createCell(4).setCellValue("年龄");
int cell = 1;
for (User users : list) {
Row row = sheet.createRow(cell);//从第二行开始保存数据
row.createCell(0).setCellValue(cell);
row.createCell(1).setCellValue(users.getUsername());//将数据库的数据遍历出来
row.createCell(2).setCellValue(users.getPawd());
row.createCell(3).setCellValue(users.getUname());
row.createCell(4).setCellValue(users.getAge());
cell++;
}
return wb;
}
}
5. Write the Mapper layer
public interface UserExportMapper {
List<User> selectByExample(Object o);
}
6. Copy Mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo1.userMapper.UserExportMapper">
<select id="selectByExample" resultType="com.example.demo1.userDomain.User">
select * from user
</select>
</mapper>
7. Perform the test.
Browser URL bar: http://localhost:8081/export/user
Note: Some people have port 8080 by default. If there are novices like me and an error occurs during access, please change 8081 to 8080
8. Export screenshot
9. The core idea
Find out the data in the database, and then use the io stream to output to the excel table
10. This article refers to https://www.cnblogs.com/lin02/p/11648899.html