spring boot 使用POI导出数据到Excel表格
在spring boot 的项目经常碰到将数据导出到Excel表格的需求,而POI技术则对于java操作Excel表格提供了API,POI中对于多种类型的文档都提供了操作的接口,但是其对于Excel表格的操作无疑是最强大的。
1.POI简介
Apache POI 是用 Java 编写的免费开源的跨平台的 Java API,Apache POI 提供 API 给 Java 程式对 Microsoft Office(Excel、WORD、PowerPoint、Visio 等,主要实现用于 Excel)格式档案读和写的功能,POI 为 “ Poor Obfuscation Implementation ” 的首字母缩写,意为简洁版的模糊实现。
POI结构:
HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF - 提供读写Microsoft Word DOC97格式档案的功能。
XWPF - 提供读写Microsoft Word DOC2003格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读Microsoft Visio格式档案的功能。
HPBF - 提供读Microsoft Publisher格式档案的功能。
HSMF - 提供读Microsoft Outlook格式档案的功能。
因为使用的是POI对Excel的操作,所以先介绍一下HSSF中的常用类:
类名 说明
HSSFWorkbook Excel的文档对象
HSSFSheet Excel的表单
HSSFRow Excel的行
HSSFCell Excel的格子单元
HSSFFont Excel字体
HSSFDataFormat 格子单元的日期格式
HSSFHeader Excel文档Sheet的页眉
HSSFFooter Excel文档Sheet的页脚
HSSFCellStyle 格子单元样式
HSSFDateUtil 日期
HSSFPrintSetup 打印
HSSFErrorConstants 错误信息表
2.在项目中导入POI的依赖
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.</version>
</dependency>
3.controller层
@GetMapping("/export")
public ResponseEntity<byte[]> exportEmp(){
List list = 数据库查询到所有要导出的数据;
return EmpUtils.exportEmp(employeeList);
}
首先从数据库查询到具体的要导出到Excel的数据,controller层的返回值为ResponseEntity<byte[]>。
4.Java使用poi的构建Excel表格
public class EmpUtils {
public static ResponseEntity<byte[]> exportEmp(List<Employee> employeeList) {
//1.创建一个excel文档
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
//2.创建文档摘要
hssfWorkbook.createInformationProperties();
//3.获取并配置文档摘要信息
DocumentSummaryInformation docInfo = hssfWorkbook.getDocumentSummaryInformation();
//文档类别
docInfo.setCategory("XXX信息");
//文档管理员
docInfo.setManager("hope");
//文档所属公司
docInfo.setCompany("xxxx");
//文档版本
docInfo.setApplicationVersion(1);
//4.获取文档摘要信息
SummaryInformation summaryInformation = hssfWorkbook.getSummaryInformation();
//文档标题
summaryInformation.setAuthor("hopec");
//文档创建时间
summaryInformation.setCreateDateTime(new Date());
//文档备注
summaryInformation.setComments("文档备注");
//5.创建样式
//创建标题行的样式
HSSFCellStyle headerStyle = hssfWorkbook.createCellStyle();
//设置该样式的图案颜色为黄色
// headerStyle.setFillForegroundColor(IndexedColors.GREEN.index);//设置图案颜色
// headerStyle.setFillBackgroundColor(IndexedColors.RED.index);//设置图案背景色
headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
//设置图案填充的样式
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//设置日期相关的样式
HSSFCellStyle dateCellStyle = hssfWorkbook.createCellStyle();
//这里的m/d/yy 相当于yyyy-MM-dd
dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
HSSFSheet sheet = hssfWorkbook.createSheet("xxx信息表");
//设置每一列的宽度
sheet.setColumnWidth(0,5*256);
sheet.setColumnWidth(1,12*256);
sheet.setColumnWidth(2,10*256);
sheet.setColumnWidth(3,5*256);
sheet.setColumnWidth(4,16*256);
sheet.setColumnWidth(5,20*256);
sheet.setColumnWidth(6,10*256);
sheet.setColumnWidth(7,10*256);
sheet.setColumnWidth(8,18*256);
sheet.setColumnWidth(9,12*256);
//6.创建标题行
HSSFRow r0 = sheet.createRow(0);
HSSFCell c0 = r0.createCell(0);
c0.setCellValue("编号");
c0.setCellStyle(headerStyle);
HSSFCell c1 = r0.createCell(1);
c1.setCellStyle(headerStyle);
c1.setCellValue("姓名");
HSSFCell c2 = r0.createCell(2);
c2.setCellStyle(headerStyle);
c2.setCellValue("工号");
HSSFCell c3 = r0.createCell(3);
c3.setCellStyle(headerStyle);
c3.setCellValue("性别");
HSSFCell c4 = r0.createCell(4);
c4.setCellStyle(headerStyle);
c4.setCellValue("出生日期");
HSSFCell c5 = r0.createCell(5);
c5.setCellStyle(headerStyle);
c5.setCellValue("身份证号码");
HSSFCell c6 = r0.createCell(6);
c6.setCellStyle(headerStyle);
c6.setCellValue("婚姻状况");
HSSFCell c7 = r0.createCell(7);
c7.setCellStyle(headerStyle);
c7.setCellValue("民族");
HSSFCell c8 = r0.createCell(8);
c8.setCellStyle(headerStyle);
c8.setCellValue("籍贯");
HSSFCell c9 = r0.createCell(9);
c9.setCellStyle(headerStyle);
c9.setCellValue("政治面貌");
HSSFCell c10 = r0.createCell(10);
for (int i = 0; i < employeeList.size(); i++) {
Employee employee= employeeList.get(i);
HSSFRow row = sheet.createRow(i+1);
row.createCell(0).setCellValue(employee.getId());
row.createCell(1).setCellValue(employee.getName());
row.createCell(2).setCellValue(employee.getWorkID());
row.createCell(3).setCellValue(employee.getGender());
HSSFCell cell4 = row.createCell(4);
//单独设置日期的样式
cell4.setCellStyle(dateCellStyle);
cell4.setCellValue(employee.getBirthday());
row.createCell(5).setCellValue(employee.getIdCard());
row.createCell(6).setCellValue(employee.getWedlock());
row.createCell(7).setCellValue(employee.getNation().getName());
row.createCell(8).setCellValue(employee.getNativePlace());
row.createCell(9).setCellValue(employee.getPoliticsstatus().getName());
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
HttpHeaders headers = new HttpHeaders();
try {
//将数据表这几个中文的字转码 防止导出后乱码
headers.setContentDispositionFormData("attachment",
new String("数据表.xls".getBytes("UTF-8"),"ISO-8859-1"));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
hssfWorkbook.write(stream);
} catch (IOException e) {
e.printStackTrace();
}
return new ResponseEntity<byte[]>(stream.toByteArray(),headers, HttpStatus.CREATED);
}
}
5.页面调用
exportEmp(){
this.$confirm('此操作将导出员工数据, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
window.open('/employee/basic/export','_parent')
}).catch(() => {
this.$message({
type: 'info',
message: '已取消导出'
});
});
},
其中/employee/basic/export为后台接口的restfulAPI地址。
spring boot 使用POI导出数据到Excel表格的更多相关文章
- 导出数据到Excel表格
开发工具与关键技术:Visual Studio 和 ASP.NET.MVC,作者:陈鸿鹏撰写时间:2019年5月25日123下面是我们来学习的导出数据到Excel表格的总结首先在视图层写导出数据的点击 ...
- Java操作Jxl实现导出数据生成Excel表格数据文件
实现:前台用的框架是Easyui+Bootstrap结合使用,需要引入相应的Js.Css文件.页面:Jsp.拦截请求:Servlet.逻辑处理:ClassBean.数据库:SQLserver. 注意: ...
- Spring Boot利用poi导出Excel
至于poi的用法就不多说了,网上多得很,但是发现spring boot结合poi的就不多了,而且大多也有各种各样的问题. public class ExcelData implements Seria ...
- Python导出数据到Excel表格-NotImplementedError: formatting_info=True not yet implemented
在使用Python写入数据到Excel表格中时出现报错信息记录:“NotImplementedError: formatting_info=True not yet implemented” 报错分析 ...
- spring boot:使用poi导出excel电子表格文件(spring boot 2.3.1)
一,什么是poi? 1,poi poi是用来兼容微软文档格式的java api, 它是apache的顶级项目之一, 也是我们在生产环境中导出excel时使用最多的库 2,poi官方网站: http:/ ...
- spring boot 整合 poi 导出excel
一. 第一种方式 1.首先从中央仓库中导入架包Poi3.14以及Poi-ooxml3.14. <dependency> <groupId>org.apache.poi</ ...
- java利用poi导出数据到excel
背景: 上一篇写到利用jtds连接数据库获取对应的数据,本篇写怎样用poi将数据到处到excel中,此程序为Application 正文: 第三方poi jar包:poi驱动包下载 代码片段: /** ...
- 使用poi导出数据到excel
一.首先是导入poi所需要的jar包,我是用的是maven,添加jar包依赖 <dependency> <groupId>org.apache.poi</groupId& ...
- PHP批量导出数据为excel表格
之前用插件phoexcel写过批量导入数据,现在用到了批量导出,就记录一下,这次批量导出没用插件,是写出一个表格,直接输出 //$teacherList 是从数据库查出来的二维数组 $execlnam ...
随机推荐
- SQL ORM框架
[LINQ]using (SqlConnection conn = new SqlConnection(conStr)) { string sql = $@"select * from vi ...
- python-web-习题
1.简单描述 webbrowser.requests.BeautifulSoup 和 selenium 模块之间的不同 webbrowser模块有一个 open() 方法,它启动 web 浏览器,打开 ...
- 安卓自定义View进阶-Canvas之画布操作 转载
安卓自定义View进阶-Canvas之画布操作 转载 https://www.gcssloop.com/customview/Canvas_Convert 本来想把画布操作放到后面部分的,但是发现很多 ...
- 前端在本地启动服务预览html页面
在开发移动端项目时浏览器里出来的效果往往到真机上和预想的有出入,在开发过程中知道了一个可以在本地自己启动一个服务器在手机预览的办法. 1.首先在终端安装http. npm i http-server ...
- java虚拟机(十一)--GC日志分析
GC相关:java虚拟机(六)--垃圾收集器和内存分配策略 java虚拟机(五)--垃圾回收机制GC 打印日志相关参数: -XX:+PrintGCDetails -XX:PrintGCTimestam ...
- Django项目:CRM(客户关系管理系统)--56--47PerfectCRM实现CRM客户报名流程01
#urls.py """PerfectCRM URL Configuration The `urlpatterns` list routes URLs to views. ...
- JavaScript怎么解析后台传入的json字符串
var data = "{'name': '张三', 'age': 23, 'gender': true}"; //json字符串 var jso = JSON.parse(dat ...
- Django-rest Framework(四)
序列化模块时rest-framework的很重要的组成部分 rest-framework序列化模块(核心) 一. 为什么要使用序列化组件? 后台的数据多以后台的对象存在,经过序列化后,就可以格式化 ...
- Python爬虫笔记【一】模拟用户访问之设置处理cookie(2)
学习的课本为<python网络数据采集>,大部分代码来此此书. 做完请求头的处理,cookie的值也是区分用户和机器的一个方式.所以也要处理一下cookie,需要用requests模块,废 ...
- 抽象工厂模式(Abstract Factory)(抽象化)
不管是简单工厂模式还是工厂方法模式,在整个模式中只能有一个抽象产品,但在现实生活中,一个工厂只创建单个产品的例子很少,因为现在的工厂都是多元化发展. (1)产品等级结构:即产品的继承结构,如一个抽象类 ...