spring boot 导出数据到excel
手把手教你springboot中导出数据到excel中
问题来源:
前一段时间公司的项目有个导出数据的需求,要求能够实现全部导出也可以多选批量导出(虽然不是我负责的,我自己研究了研究),我们的项目是xboot前后端分离系统,后端的核心为SpringBoot 2.2.6.RELEASE,因此今天我主要讲述后端的操作实现,为了简化需求,我将需要导出的十几个字段简化为5个字段,导出的样式模板如下:

实现步骤:
打开一个你平时练习使用的springboot的demo,开始按照以下步骤加入代码进行操作。
1.添加maven依赖
|
1
2
3
4
5
6
|
<!--Excel--><dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.11</version></dependency> |
poi-ooxml是一个excel表格的操作工具包,处理的单页数据量也是百万级别的,因此我们选择的是poi-ooxml.
2.编写excel工具类
|
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
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.xssf.streaming.SXSSFWorkbook;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.util.List;public class ExcelUtil { /** * 用户信息导出类 * @param response 响应 * @param fileName 文件名 * @param columnList 每列的标题名 * @param dataList 导出的数据 */ public static void uploadExcelAboutUser(HttpServletResponse response,String fileName,List<String> columnList,<br>List<List<String>> dataList){ //声明输出流 OutputStream os = null; //设置响应头 setResponseHeader(response,fileName); try { //获取输出流 os = response.getOutputStream(); //内存中保留1000条数据,以免内存溢出,其余写入硬盘 SXSSFWorkbook wb = new SXSSFWorkbook(1000); //获取该工作区的第一个sheet Sheet sheet1 = wb.createSheet("sheet1"); int excelRow = 0; //创建标题行 Row titleRow = sheet1.createRow(excelRow++); for(int i = 0;i<columnList.size();i++){ //创建该行下的每一列,并写入标题数据 Cell cell = titleRow.createCell(i); cell.setCellValue(columnList.get(i)); } //设置内容行 if(dataList!=null && dataList.size()>0){ //序号是从1开始的 int count = 1; //外层for循环创建行 for(int i = 0;i<dataList.size();i++){ Row dataRow = sheet1.createRow(excelRow++); //内层for循环创建每行对应的列,并赋值 for(int j = -1;j<dataList.get(0).size();j++){//由于多了一列序号列所以内层循环从-1开始 Cell cell = dataRow.createCell(j+1); if(j==-1){//第一列是序号列,不是在数据库中读取的数据,因此手动递增赋值 cell.setCellValue(count++); }else{//其余列是数据列,将数据库中读取到的数据依次赋值 cell.setCellValue(dataList.get(i).get(j)); } } } } //将整理好的excel数据写入流中 wb.write(os); } catch (IOException e) { e.printStackTrace(); } finally { try { // 关闭输出流 if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } } } /* 设置浏览器下载响应头 */ private static void setResponseHeader(HttpServletResponse response, String fileName) { try { try { fileName = new String(fileName.getBytes(),"ISO8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } response.setContentType("application/octet-stream;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment;filename="+ fileName); response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); } catch (Exception ex) { ex.printStackTrace(); } }} |
网上的excel的工具类有很多,但很多并不是你复制过来就能直接使用的,因此需要我们深究其原理,这样可以应对不同的场景写出属于我们自己的合适的代码,这里就不一一解释了,代码中注释加的很清楚,有不懂的可以留言评论。
3.编写controller,service,serviceImpl,dao,entity
3.1 entity
|
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
|
import io.swagger.annotations.ApiModelProperty;import lombok.Data;import org.hibernate.annotations.Where;import javax.persistence.*;import java.math.BigDecimal;@Data@Entity@Where(clause = "del_flag = 0")@Table(name = "t_scf_item_data")public class ItemData{ private static final long serialVersionUID = 1L; @Id @TableId @ApiModelProperty(value = "唯一标识") private String id = String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()); @ApiModelProperty(value = "创建者") @CreatedBy private String createBy; @CreatedDate @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @ApiModelProperty(value = "创建时间") private Date createTime; @ApiModelProperty(value = "项目编号") private String itemNo; @ApiModelProperty(value = "项目名称") private String itemName; @ApiModelProperty(value = "删除标志 默认0") private Integer delFlag = 0; } |
3.2 dao
|
1
2
3
4
5
6
7
8
9
10
11
12
|
import cn.exrick.xboot.modules.item.entity.ItemData;import org.springframework.data.jpa.repository.Query;import org.springframework.stereotype.Repository;import java.util.List;@Repositorypublic interface ItemDataDao{ @Query(value = "select a.item_no,a.item_name,concat(a.create_time),a.create_by from t_scf_item_data a where a.del_flag = 0 limit 5",nativeQuery = true) List<List<String>> findAllObject(); @Query(value = "select a.item_no,a.item_name,concat(a.create_time),a.create_by from t_scf_item_data a where a.del_flag = 0 and a.id in ?1 limit 5",nativeQuery = true) List<List<String>> findByIds(List<String> idList);} |
3.3 service
|
1
2
3
4
5
6
|
import javax.servlet.http.HttpServletResponse;import java.util.List;public interface TestService { void exportExcel(List<String> idList, HttpServletResponse response);} |
3.4 serviceImpl
|
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
|
import cn.exrick.xboot.common.utils.ExcelUtil;import cn.exrick.xboot.modules.item.dao.ItemDataDao;import cn.exrick.xboot.modules.item.service.TestService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import javax.servlet.http.HttpServletResponse;import java.util.ArrayList;import java.util.Arrays;import java.util.List;@Transactional@Servicepublic class TestServiceImpl implements TestService { @Autowired private ItemDataDao itemDataDao; @Override public void exportExcel(List<String> idList, HttpServletResponse response) { List<List<String>> dataList = new ArrayList<>(); if(idList == null || idList.size() == 0){ dataList = itemDataDao.findAllObject(); }else{ dataList = itemDataDao.findByIds(idList); } List<String> titleList = Arrays.asList("序号","项目编码", "项目名称", "创建时间", "创建人"); ExcelUtil.uploadExcelAboutUser(response,"apply.xlsx",titleList,dataList); }} |
3.5 controller
|
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
|
import cn.exrick.xboot.modules.item.service.TestService;import io.swagger.annotations.ApiOperation;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;import java.util.List;import java.util.Map;@Slf4j@RestController@RequestMapping("/test")public class TestController { @Autowired private TestService testService; @RequestMapping(value = "/exportExcel", method = RequestMethod.POST) @ApiOperation(value = "导出excel",produces="application/octet-stream") public void exportCorpLoanDemand(@RequestBody Map<String,List<String>> map, HttpServletResponse response){ ; log.info("测试:{}",map); testService.exportExcel(map.get("list"),response); }} |
4.测试
测试的话可以使用swagger或者postman,甚至你前端技术足够ok的话也可以写个简单的页面进行测试,我是用的是swaager进行的测试,下面就是我测试的结果了:



原文链接:https://www.cnblogs.com/zaevn00001/p/13353744.html?utm_source=tuicool
spring boot 导出数据到excel的更多相关文章
- Spring Boot 导出Excel表格
Spring Boot 导出Excel表格 添加支持 <!--添加导入/出表格依赖--> <dependency> <groupId>org.apache.poi& ...
- Spring Boot框架下实现Excel服务端导入导出
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.今天 ...
- 从数据库导出数据到excel之POI操作
项目说明: 1:数据库中有两张表,主键关联 2:根据条件查询数据 3:处理为需要的数据封装类型,然后传到导出excel的方法中 <--框架部署就不详谈了,用的spring框架--> 补充: ...
- 1.ASP.NET MVC使用EPPlus,导出数据到Excel中
好久没写博客了,今天特地来更新一下,今天我们要学习的是如何导出数据到Excel文件中,这里我使用的是免费开源的Epplus组件. 源代码下载:https://github.com/caofangshe ...
- 导出数据到Excel --使用ExcelReport有感
先看图,这是几个月前用NPOI写的导出数据到Excel,用了上百行代码,而且难控制,导出来也比较难看 excel打开的效果 下面是我用ExcelReport类库导出到Excel的操作 1.首先引用Ex ...
- 使用Open xml 操作Excel系列之二--从data table导出数据到Excel
由于Excel中提供了透视表PivotTable,许多项目都使用它来作为数据分析报表. 在有些情况下,我们需要在Excel中设计好模板,包括数据源表,透视表等, 当数据导入到数据源表时,自动更新透视表 ...
- Dynamics CRM导出数据到Excel
原创地址:http://www.cnblogs.com/jfzhu/p/4276212.html 转载请注明出处 Pivot Table是微软BI的一个重要工具,所以这里讲一下Dynamics CRM ...
- MVC导出数据到EXCEL新方法:将视图或分部视图转换为HTML后再直接返回FileResult
导出EXCEL方法总结 MVC导出数据到EXCEL的方法有很多种,常见的是: 1.采用EXCEL COM组件来动态生成XLS文件并保存到服务器上,然后转到该文件存放路径即可: 优点:可设置丰富的EXC ...
- php导出数据到excel,防止身份证等数字字符格式变成科学计数的方法
而关于php的也有,但是大多都是用phpExcel导出的方法或者spreadsheet等类或者控件之类的导出方法,而我所在维护的系统却用很简单的方法,如下,网上很少有讲如何设置要导出数据的EXcel格 ...
- NPOI导出数据到Excel
NPOI导出数据到Excel 前言 Asp.net操作Excel已经是老生长谈的事情了,可下面我说的这个NPOI操作Excel,应该是最好的方案了,没有之一,使用NPOI能够帮助开发者在没有安装微 ...
随机推荐
- react 03 组件传值
一 基础 props: 父传子 单向 import React from 'react'; import ReactDOM from 'react-dom'; import './index.css ...
- gitlab中CI/CD过程中的坑
先上观点,azure的pipeline比gitlab ce版好用,gitlab收费版没有用过. 在.gitlab-ci.yml中的特殊字符处理: 解决方法: cmd="[$var1] &am ...
- docker配置文件模板
{ "registry-mirrors": [ "https://bxsfpjcb.mirror.aliyuncs.com" ], "max-conc ...
- python实现Excel的表头与索引之间的转换
字母转数字 def get_index(capital): """ 大写字母(Excel列头)转索引 :param capital: 'A' --> 0, 'AA' ...
- Win10系统将bat文件注册成服务
代码语法: sc create ServiceName binPath= 路径 start= auto 示例语句: sc create Tomcat binPath= F:/tomcat/bin/st ...
- SimplCommerce 核心
EF配置 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks ...
- 前端实现电子签名(web、移动端)通用组件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java.lang.UnsatisfiedLinkError:【Linux运行JAVA调用JNA重新,so,SO报错】
困扰了好半天,我自己新建的项目,包名什么的不一样,太TM坑了,必须要包名一样文件名一样
- C#中定时任务被阻塞问题
目录 解决一个C#中定时任务被阻塞问题 1.摘要 2.C#中定时任务的最简方法 3.定时任务阻塞现象 4.阻塞现象原因分析 5.问题解决 1.摘要 本文会介绍一个C#中最简单定时任务的使用方法,以及会 ...
- lombok安装不了的问题