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 ...
随机推荐
- [原创]mysql 5.6安装配置,主从分离,读写分离简单教程
文章中参考使用了多个博客的资料,汇总而成!其流程准确性被人亦本人实践! https://blog.csdn.net/qq_35206261/article/details/81321201 https ...
- java主函数参数传递args
a.javaJava应用程序的主入口方法main(String[] args),表示该方法需要接收一个字符串数组类型的参数, 如果该参数不指定,agrs接收的是null. 程序: public C ...
- 表格Table好看的样式表
caption { padding: 0 0 5px 0; width: 700px; font: italic 11px "Trebuchet MS" , Verdana, Ar ...
- C# 判断当前请求是GET、还是POST ?
方法一: HttpContext.Current.Request.RequestType == "POST" //当前请求为:POST 方法二: if(Request.Serv ...
- Django项目:CRM(客户关系管理系统)--62--52PerfectCRM实现CRM客户报名流程学生合同审核
# sales_urls.py # ————————47PerfectCRM实现CRM客户报名流程———————— from django.conf.urls import url from bpm. ...
- leyou_04_vue.js的ajax请求方式
1.异步查询数据,自然是通过ajax查询,大家首先想起的肯定是jQuery.但jQuery与MVVM的思想不吻合,而且ajax只是jQuery的一小部分.因此不可能为了发起ajax请求而去引用这么大的 ...
- Weekly Challenges - Week 11
一拖再拖,忍无可忍,自己懒的没救了. 一周前就结束的比赛,到现在才想起来补. 最后一题貌似又遇到了splay...还是不会!!!shit 题目链接 A题--快速幂 #include <cmath ...
- ERROR:ORA-30076: 对析出来源无效的析出字段
DEBUG:key: sql: select count(*) as col_0_0_ from jc_user cmsuser0_ where 1=1 and cmsuser0_.register_ ...
- line-height:2和line-height:2em的区别,它们是有区别的
line-height:2是2倍的意思,如果内部有不同大小文字的情况下,以最大文字为倍数. line-height:2em也是2倍文字大小的意思,但如果内部有大文字,它还是会以父容 器的大小来计算. ...
- python intern(字符串驻留机制)
python 中为了提高字符串的使用用效率和节约内存,对于由 字母.数字.下划线组成的标识符采用了 intern 机制,即对于短字符串,将其赋值给多个对象时,内存中只有一个副本,多个对象共享这个副本. ...