SpringBoot实现Excel导出
作者:互联网
1.maven依赖
点击查看代码
<!--表格生成-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
2.通过调用mapper方法查询到数据库的数据并生成表格
点击查看代码
import com.example.blog.mapper.BlogMapper;
import com.example.blog.service.ITableService;
import com.example.blog.vo.ListBlogVO;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Author QingHao
* @Date: 2022/06/09/ 13:19
* @Describe
*/
@Service
public class TableServiceImpl{
@Autowired
private BlogMapper mapper;
public XSSFWorkbook formGeneration() {
//查询数据库中的数据
List<ListBlogVO> listBlogVOS = mapper.listSelect();
XSSFWorkbook xssfSheets = new XSSFWorkbook();
XSSFSheet userList = xssfSheets.createSheet("userList");
Row titleRow = userList.createRow(0);//创建第一行,起始为0
titleRow.createCell(0).setCellValue("id");//第一列
titleRow.createCell(1).setCellValue("用户id");
titleRow.createCell(2).setCellValue("标题");
titleRow.createCell(3).setCellValue("摘要");
titleRow.createCell(4).setCellValue("创建时间");
titleRow.createCell(5).setCellValue("status");
for (int i = 0; i < listBlogVOS.size(); i++) {
Row row = userList.createRow(i + 1);//设置对哪一行操作
row.createCell(0).setCellValue(listBlogVOS.get(i).getId());
row.createCell(1).setCellValue(listBlogVOS.get(i).getUserId());
row.createCell(2).setCellValue(listBlogVOS.get(i).getTitle());
row.createCell(3).setCellValue(listBlogVOS.get(i).getDescription());
row.createCell(4).setCellValue(listBlogVOS.get(i).getCreated()+"");
row.createCell(5).setCellValue(listBlogVOS.get(i).getStatus()+"");
}
return xssfSheets;
}
}
3.将生成的表格数据返回给前端页面
点击查看代码
import com.example.blog.service.ITableService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
/**
* @Author QingHao
* @Date: 2022/06/09/ 13:28
* @Describe
*/
@Slf4j
@Controller
public class TableDowloadController {
@Autowired
private ITableService tableService;
/**
* 表格生成器
*
* @param response
*/
@GetMapping("/tableDowload")
public void tableDowload(HttpServletResponse response) {
log.debug("开始生成表格数据");
XSSFWorkbook xssfSheets = tableService.formGeneration();
String fileName = "Goods报表.xlsx";
OutputStream outputStream = null;
try {
fileName = URLEncoder.encode(fileName, "UTF-8");
//设置ContentType请求信息格式
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
outputStream = response.getOutputStream();
xssfSheets.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
标签:listBlogVOS,SpringBoot,Excel,poi,导出,createCell,import,org,setCellValue 来源: https://www.cnblogs.com/Seasky-null/p/16483149.html