报表导出之easypoi的应用
报表导出有很多种方法,像之前我有写过的jxl,poi,jasperreport又或者各种商业软件,这次来简单介绍下用了许久的开源轮子easypoi。
easypoi的底层原理就不介绍了。因为官方文档的不足,本次简单记录下使用easypoi导出excel的各种方法。
首先在具体介绍前,建议大家先看一看easypoi的官方文档,http://easypoi.mydoc.io/
文档中基础使用上的介绍还是比较详细的。
本次我以目前流行的springboot项目为例。
第一步我们需要引入easypoi的依赖,一个自定义的starter
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
注意:引入了这个就不用再引入poi的包了,自定义的starter已经包含相关依赖了。
1、默认导出方式
导出类的配置
@Getter
@Setter
public class GroupCtrlVO {
@Excel(name = "操作人", orderNum = "1", width = 30)
private String ctrlUser;
@Excel(name = "操作时间", orderNum = "2", width = 30, format = "yyyy-MM-dd HH:mm:ss")
private Date ctrlTime;
@Excel(name = "标签名称", orderNum = "3", width = 30)
private String groupName;
@Excel(name = "操作类型", orderNum = "4", width = 30)
private String ctrlOperation;
}
service的配置
@Override
public void export(String login, HttpServletResponse response) {
OutputStream output = null;
try {
WebUserDO userDO = cmWebUserMapper.getWebUserByLogin(login);
List<GroupCtrlVO> list = crmCustomerInfoGroupMapper.listCtrlVo(userDO.getId());
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), GroupCtrlVO.class, list);
response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(login+ "标签操作记录.xls", "UTF-8"));
response.setContentType("application/msexcel");
output = response.getOutputStream();
workbook.write(output);
} catch (Exception e) {
log.error("标签操作记录导出失败!", e);
} finally {
try {
output.close();
} catch (IOException e) {
log.error("标签操作记录输出流关闭失败!", e);
}
}
}
2、模板导出方式
excel模板:相关循环参数参考官方文档

service配置:
@Override
public void export(String startDate, String endDate, HttpServletResponse response) {
OutputStream output = null;
try {
List<Test> list = testMapper.list(startDate,endDate);
TemplateExportParams params = new TemplateExportParams(ExcelPathUtils.convertTemplatePath("static/report/template_test_report.xlsx"));
Map<String, Object> data = new HashMap<String, Object>();
data.put("list", list);
Workbook workbook = ExcelExportUtil.exportExcel(params, data);
response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(startDate+"~"+endDate + "测试报表.xlsx", "UTF-8"));
response.setContentType("application/msexcel");
output = response.getOutputStream();
workbook.write(output); } catch (Exception e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (IOException e) {
log.error("测试报表输出流关闭失败!", e);
}
}
}
注意:导出的字段要和模板循环到字段相同
3、模板多sheet导出
多sheet模板:


service配置:
@Override
public void export(String startDate, String endDate, String strategyName, HttpServletResponse response) {
OutputStream output = null;
try {
//查表1数据
List<Test1DO> list = testMapper.listStrategy(strategyName, startDate, endDate);
//查表2数据
List<Test2DO> listDetail = test2Mapper.listStrategy(strategyName, startDate, endDate);
TemplateExportParams params = new TemplateExportParams(ExcelPathUtils.convertTemplatePath("static/report/template_test_two_sheet_report.xlsx"),true);
Map<String, Object> data = new HashMap<String, Object>();
data.put("list", list);
data.put("listDetail", listDetail);
Workbook workbook = ExcelExportUtil.exportExcel(params, data);
response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(startDate+"~"+endDate + "测试多表单报表.xlsx", "UTF-8"));
response.setContentType("application/msexcel");
output = response.getOutputStream();
workbook.write(output);
} catch (Exception e) {
log.error("测试多表单导出失败!", e);
} finally {
try {
output.close();
} catch (IOException e) {
log.error("测试多表单输出流关闭失败!", e);
}
}
}
因为springboot打成jar包,文件读取路径的问题,上述案例中ExcelPathUtils是我按照自己需求重写的,有需要的同学可以参考下。
/**
* poi导出路径获取,解决jar包找不到路径的问题
* 思路:将文件写入tomcat临时文件夹中
* @author 小卖铺的老爷爷
* @date 2018年12月26日
*/
public class ExcelPathUtils {
public static String convertTemplatePath(String path) {
// 如果是windows 则直接返回
// if (System.getProperties().getProperty("os.name").contains("Windows")) {
// return path;
// }
Resource resource = new ClassPathResource(path);
FileOutputStream fileOutputStream = null;
// 将模版文件写入到 tomcat临时目录
String folder = System.getProperty("catalina.home");
File tempFile = new File(folder + File.separator + path);
// System.out.println("文件路径:" + tempFile.getPath());
// 文件存在时 不再写入
if (tempFile.exists()) {
return tempFile.getPath();
}
File parentFile = tempFile.getParentFile();
// 判断父文件夹是否存在
if (!parentFile.exists()) {
parentFile.mkdirs();
}
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(resource.getInputStream());
fileOutputStream = new FileOutputStream(tempFile);
byte[] buffer = new byte[10240];
int len = 0;
while ((len = bufferedInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return tempFile.getPath();
} }
报表导出之easypoi的应用的更多相关文章
- 【HOW】如何限制Reporting Services报表导出功能中格式选项
Reporting Services报表导出功能中缺省会提供多种导出格式选项,但很多情况下不需要全部的格式选项,因此需要对这些选项进行限制.下面我们以SQL Server 2008 R2为例来说明对这 ...
- JAVA将Excel中的报表导出为图片格式(一)问题背景
如题所示,先抛出一个问题,如何使用JAVA将Excel中的报表导出为图片格式? 首先说一下这个问题的背景,也就是为什么博主会碰到这个问题 随着微信,易信之流大行其道,企业内部的办公交流.绩效考评甚至考 ...
- Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,htm
Ireport 报表导出 Poi + ireport 导出pdf, doc ,excel ,html 格式 下面是报表导出工具类reportExportUtils 需要导出以上格式的报表 只需要调用本 ...
- kettle工具实现报表导出的初步搭建
1.下载kettle 国外网站:http://kettle.pentaho.org/需要FQ,下载慢 2.下载完成启动(windows)-->spoon.bat 3.进入界面,两个主要的tab页 ...
- Dynamics CRM 报表导出EXCEL 列合并问题的解决方法
CRM中的报表导出功能提供了多种格式,excel就是其中之一,这次遇到的问题是导出后打开excel列明合并的问题,具体如下看着相当不美观,物料名称字段占了AB两列,品牌占了CD两列等等. 该问题的源头 ...
- 考勤输入导入OA平台与考勤统计报表导出功能源代码
注:以某某公司为例,每日签到时间为8点整 每日签退时间为17点30分 规则:公司签到签退时间在OA平台中可以视实际情况调整,当天有请假并通过工作流审批通过为有效,当天因公外出并通过工作流审批通过为 ...
- ActiveReports 报表应用教程 (16)---报表导出
葡萄城ActiveReports报表支持多种格式的报表导出,包括PDF.Excel.Word.RTF.HTML.Text.TIFF以及其它图片格式,用户可以将它们应用到Windows Forms.We ...
- 填报表导出excel非可写单元格锁定问题
问题描述: 填报表单元格分为可写和不可写两种状态,当填报表在web上展现的时候可写单元格可以进行数据填报和修改,非可写单元格不可操作. 报表导出为excel时,润乾导出excel包默认情况下不对 ...
- 报表导出excel方式介绍
报表导出excel提供了四种方式,在单元格属性"其他/导出excel方式"可以选择,如下图 一是导出缺省值:报表中的单元格包含两个值,一个真实值一个显示值,但是在excel中 ...
随机推荐
- c#高效准确的条形码、线性条码、QR二维码读写类库-SharpBarcode介绍
SharpBarcode是一款支持.NET(C#,VB)的高效易用的条形码.QR二维码的读取和生成类库. 主要功能: 1.支持几乎所有常见类型的线性条形码和QR二维码的读取,高效读取,准确率高. 2. ...
- jwt认证生成后的token如何传回后端并解析的详解
jwt认证生成后的token后端解析 一.首先前端发送token token所在的位置headers {'authorization':token的值',Content-Type':applicati ...
- spark和深度学习集成调研
http://dy.163.com/v2/article/detail/E2TMAOTU0518KCLV.html http://www.elecfans.com/d/676451.html http ...
- 编写可维护的JavaScript-随笔(七)
将配置数据从代码中分离出来 代码中有些数据有修改的可能,如果放在函数中的话后期修改的时候会带来一些不必要的风险 需要将配置数据从代码中抽取出来,如果配置数据多的话可以放入一个对象中,然后修改抽取出来的 ...
- JavaScript 数组(一)数组基础
一.数组 1.概述 数组 就是将多个元素按一定顺序排放到一个集合中,那么这个集合称之为数组. 数组 也是一种数据类型,属于复杂数据类型(Object). 2.特点 存放的元素有序的. 可以存放不同的数 ...
- Unable to establish SSL connection
当wget出现如下错误: Can't connect to HTTPS URL because the SSL module is not available 需要安装: # sudo apt-get ...
- Linux安装在虚拟机上
虚拟机上安装centos7 minimal 详细操作链接:https://blog.csdn.net/babyxue/article/details/80970526 镜像文件 xxx.iso 本质就 ...
- docker命令小全 this is my note.
服务器类型:linux =>centos 7.X以上版本 常用命令使用紫色加粗标明 1.安装yum-util(为配置docker安装时使用阿里镜像做准备):yum install -y yum- ...
- BLE——低功耗蓝牙(Bluetooth Low Energy)
1.简介 以下蓝牙协议特指低功耗蓝牙协议. 蓝牙协议是由SIG制定并维护的通信协议,蓝牙协议栈是蓝牙协议的具体实现. 各厂商都根据蓝牙协议实现了自己的一套函数库——蓝牙协议栈,所以不同厂商的蓝牙协议栈 ...
- OAuth 第三方登录授权码(authorization code)方式的小例子
假如上面的网站A,可以通过GitHub账号登录: 下面以OAuth其中一种方式,授权码(authorization code)方式为例. 一.第三方登录的原理 所谓第三方登录,实质就是 OAuth 授 ...