关于EasyPoi导出Excel
如果你觉得Easypoi不好用,喜欢用传统的poi,可以参考我的这篇博客:Springmvc导出Excel(maven)
当然了,万变不离其宗。Easypoi的底层原理还是poi。正如MyBatis Plus的原理还是MyBatis那套。只不过它们的共同点是封装起来。
关于Easypoi记得初次接触的时候,给我的感觉是看起来很简单很容易让人理解,而且文档也比较丰富,也是jeecg的开源项目下的子项目。
记得当初为了提高代码开发效率,去码云和github上游荡游荡,突然发现了一个叫jeecg的玩意,于是研究了下,这个研究不是特别深,只是将其项目跑起来,看看它有哪些组件,顺便看看源码,和玩玩它强大的插件式开发和easypoi。
jeecg中的easypoi的项目地址为:https://gitee.com/jeecg/jeasypoi
jeecg中的easypoi的官方详细文档为:http://easypoi.mydoc.io/
大家要牢记一个经济方面的格言:物质基础决定上层建筑。
对计算机专业的同志们而言,良好的计算机基础,是以后编程世界的驰骋飞扬的基石。
所以在校的同志们,一定要好好学习天天向上。
闲话就不多说了,下面进入正题。
一、导入依赖
<!-- easypoi导入导出excel -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.1.0</version>
</dependency>
二、构建实体
@ExcelTarget("FinanceTrade")
@TableName("rms_finance_trade")
public class FinanceTrade extends Model<FinanceTrade> { private static final long serialVersionUID = 1L; @TableId("trade_no")
private String tradeNo; @Excel(name="订单号")
@TableField("order_no")
private String orderNo;
/**
* 用户id
*/
@TableField("user_id")
private String userId;
/**
* 交易创建时间
*/
@Excel(name="创建时间",width=30)
@TableField("create_time")
private String createTime;
/**
* 实际支付金额
*/
@Excel(name="实际支付金额")
private BigDecimal amount;
/**
* 交易状态支付成功转入退款未支付已关闭已撤销支付失败
*/
private String status;
/**
* 流水标题
*/
private String subject; @TableField("finish_time")
private String finishTime;
/**
* 类型:余额支付balance、微信支付wx
*/
private String type;
/**
* 资金流向:1:收入 ;0:支出
*/
private String flows;
/**
* 备注
*/
private String remarks; /**
* 支付类型:余额,微信,混合支付等
*/
@TableField(exist=false)
@Excel(name="支付类型")
private String payType; /**
* 费用名称
*/
@TableField(exist=false)
@Excel(name="费用名称")
private String amountType; @TableField(exist=false)
private String leafNode; @TableField(exist=false)
private String address; @TableField(exist=false)
private String node; @TableField(exist=false)
private String subNode; @TableField(exist=false)
@Excel(name="账户",width=30)
public String loginCode; @TableField(exist=false)
@Excel(name="支付人")
public String userName; /**
* 公司编码
* @return
*/
@TableField(value="company_code")
private String companyCode; /**
* 用户
*/
@TableField(exist=false)
List<SysUser> sysUser; /**
* 订单
*/
@TableField(exist=false)
List<FinanceOrder> order; public String getLoginCode() {
return loginCode;
} public void setLoginCode(String loginCode) {
this.loginCode = loginCode;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getLeafNode() {
return leafNode;
} public void setLeafNode(String leafNode) {
this.leafNode = leafNode;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getNode() {
return node;
} public void setNode(String node) {
this.node = node;
} public String getSubNode() {
return subNode;
} public void setSubNode(String subNode) {
this.subNode = subNode;
} public String getPayType() {
return payType;
} public void setPayType(String payType) {
this.payType = payType;
} public String getAmountType() {
return amountType;
} public void setAmountType(String amountType) {
this.amountType = amountType;
} public String getCompanyCode() {
return companyCode;
} public void setCompanyCode(String companyCode) {
this.companyCode = companyCode;
} public String getTradeNo() {
return tradeNo;
} public void setTradeNo(String tradeNo) {
this.tradeNo = tradeNo;
} public String getOrderNo() {
return orderNo;
} public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
} public String getUserId() {
return userId;
} public void setUserId(String userId) {
this.userId = userId;
} public String getCreateTime() {
return createTime;
} public void setCreateTime(String createTime) {
this.createTime = createTime;
} public BigDecimal getAmount() {
return amount;
} public void setAmount(BigDecimal amount) {
this.amount = amount;
} public String getStatus() {
return status;
} public void setStatus(String status) {
this.status = status;
} public String getSubject() {
return subject;
} public void setSubject(String subject) {
this.subject = subject;
} public String getFinishTime() {
return finishTime;
} public void setFinishTime(String finishTime) {
this.finishTime = finishTime;
} public String getType() {
return type;
} public void setType(String type) {
this.type = type;
} public String getFlows() {
return flows;
} public void setFlows(String flows) {
this.flows = flows;
} public String getRemarks() {
return remarks;
} public void setRemarks(String remarks) {
this.remarks = remarks;
} @Override
protected Serializable pkVal() {
return this.tradeNo;
} public List<SysUser> getSysUser() {
return sysUser;
} public void setSysUser(List<SysUser> sysUser) {
this.sysUser = sysUser;
} public List<FinanceOrder> getOrder() {
return order;
} public void setOrder(List<FinanceOrder> order) {
this.order = order;
} @Override
public String toString() {
return "FinanceTrade [tradeNo=" + tradeNo + ", orderNo=" + orderNo + ", userId=" + userId + ", createTime="
+ createTime + ", amount=" + amount + ", status=" + status + ", subject=" + subject + ", finishTime="
+ finishTime + ", type=" + type + ", flows=" + flows + ", remarks=" + remarks
+ ", payType=" + payType + ", amountType=" + amountType + ", leafNode=" + leafNode + ", address="
+ address + ", node=" + node + ", subNode=" + subNode + ", companyCode=" + companyCode + "]";
}
}
- @Excel 作用到filed上面,是对Excel一列的一个描述
- @ExcelCollection 表示一个集合,主要针对一对多的导出,比如一个老师对应多个科目,科目就可以用集合表示
- @ExcelEntity 表示一个继续深入导出的实体,但他没有太多的实际意义,只是告诉系统这个对象里面同样有导出的字段
- @ExcelIgnore 和名字一样表示这个字段被忽略跳过这个导导出
- @ExcelTarget 这个是作用于最外层的对象,描述这个对象的id,以便支持一个对象可以针对不同导出做出不同处理
上面截图我只是截取官方文档上的一部分,大家想详细知道,可参考官方文档:http://easypoi.mydoc.io/#text_197835
三、编写对应的Controller
@Controller
@RequestMapping(value = "easypoi")
public class ExportExcelTest { @Autowired
private FinanceTradeService financeTradeService; /**
* 导出Excel 营业收入支出明细
* @param companyCode
* @param flows
* @param response
* @return
*/
@GetMapping(value="exportBillDatailInfo")
@ResponseBody
public String exportBillDatailInfo(HttpServletRequest request,HttpServletResponse response){
String companyCode = request.getParameter("companyCode");
String flows = request.getParameter("flows");
System.out.println("companyCode:"+companyCode);
System.out.println("flows:"+flows);
// 获取workbook对象
// 单sheet或多sheet 只需要更改此处即可
Workbook workbook = exportSheets(companyCode,flows) ;
// 判断数据
if(workbook == null) {
return "fail";
}
// 设置excel的文件名称
String excelName = "测试excel" ;
// 重置响应对象
response.reset();
// 当前日期,用于导出文件名称
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String dateStr = "["+excelName+"-"+sdf.format(new Date())+"]";
// 指定下载的文件名--设置响应头
response.setHeader("Content-Disposition", "attachment;filename=" +dateStr+".xls");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 写出数据输出流到页面
try {
OutputStream output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
workbook.write(bufferedOutPut);
bufferedOutPut.flush();
bufferedOutPut.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
return "success";
} /**
* 多sheet导出
* @return
*/
public Workbook exportSheets(String companyCode,String flows){ //将条件放入Map中
Map<String,Object> conditionMap = new HashMap<String,Object>(); conditionMap.put("companyCode", companyCode);
conditionMap.put("flows", flows);
conditionMap.put("start", 0);
conditionMap.put("size", 100); //获得营业明细(含收支)信息
List<FinanceTrade> list = financeTradeService.getSpeedingDetailInfo(conditionMap); // 创建参数对象(用来设定excel得sheet得内容等信息)
ExportParams params1 = new ExportParams() ;
// 设置sheet得名称
params1.setSheetName("营业收支明细"); ; // 创建sheet1使用得map
Map<String,Object> dataMap1 = new HashMap<>();
// title的参数为ExportParams类型,目前仅仅在ExportParams中设置了sheetName
dataMap1.put("title",params1) ;
// 模版导出对应得实体类型
dataMap1.put("entity",FinanceTrade.class) ;
// sheet中要填充得数据
dataMap1.put("data",list) ; // 将sheet1和sheet2使用得map进行包装
List<Map<String, Object>> sheetsList = new ArrayList<>() ;
sheetsList.add(dataMap1);
// 执行方法
return ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF) ;
} }
小结:上面主要是关于Spring+MyBatis Plus+SpringMVC+EasyPoi+MySQL,更多简单例子可以去官方文档上看或者参照下面博客地址也可以:
EasyPoi导入导出:https://www.cnblogs.com/xiexy/p/8044393.html
EasyPoi导出示例:http://www.leftso.com/blog/329.html(该例非常简单,很通俗易懂)
关于EasyPoi导出Excel的更多相关文章
- SpringBoot使用Easypoi导出excel示例
SpringBoot使用Easypoi导出excel示例 https://blog.csdn.net/justry_deng/article/details/84842111
- EasyPoi导出Excel
这几天一直在忙工作中的事情,在工作中有一个问题,可能是因为刚开始接触这个EasyPoi,对其也没有太多的理解,在项目中就使用了,有一个需求,是要导出项目中所有的表格,今天就对这个需求进行分析和实现吧; ...
- 使用easypoi导出excel
EasyPOI是在jeecg的poi模块基础上,继续开发独立出来的,可以说是2.0版本,EasyPoi封装的目的和jeecg一致,争取让大家write less do more ,在这个思路上easy ...
- easyPOI导出excel报错
http-nio--exec- at :: - excel cell export error ,data is :com.jn.ssr.superrescue.web.qc.dto.Automati ...
- EasyPoi 导出Excel(ExcelExportEntity生成表头)
[引入依赖] <!--easypoi--> <dependency> <groupId>cn.afterturn</groupId> <artif ...
- 使用EasyPOI导出excel示例
package com.mtoliv.sps.controller; import java.io.IOException; import java.io.OutputStream; import j ...
- Vue+EasyPOI导出Excel(带图片)
一.前言 平时的工作中,Excel 导入导出功能是非常常见的功能,无论是前端 Vue (js-xlsx) 还是 后端 Java (POI),如果让大家手动编码实现的话,恐怕就很麻烦了,尤其是一些定制化 ...
- java导出excel(easypoi)
介绍 easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 ...
- easypoi导出单个sheet和多个sheet
今天有时间研究了一下easypoi,感觉使用了easypoi导出excel方便了很多,不用写很多复杂的反射,只需要使用注解和一些工具类就可以实现常用的excel的导出,接下来介绍一下easypoi如何 ...
随机推荐
- google vue开发调试插件,简便安装,亲测可用
前言:开发vue项目,使用谷歌浏览器,不得不使用调试插件便于调试 插件地址如下: 链接:https://pan.baidu.com/s/159HqJMeFSF-w5z-tMi7drw 密码:ueez ...
- git使用总结(常用命令)
前言 写这篇文章的目的是让新手能够操作git管理自己的代码,可能你知道git的各种命令但是对其使用顺序并不熟,比如我.所以有必要整合一篇关于命令使用步骤的文章,图片用的人家的,也没询问让不让用,可能会 ...
- 【HTML&CSS】搜狐页面代码编写
<!DOCTYPE html> <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"& ...
- opencv3.2.0形态学滤波之腐蚀
/* 腐蚀(erode)含义: 腐蚀和膨胀是相反的一对操作,所以腐蚀就是求局部最小值的操作,腐蚀操作使原图中 国的高亮部分被腐蚀,效果图比原图有更小的高亮的区域. 腐蚀函数原型API及参数同膨胀相同 ...
- Android 仿美团网,探索使用ViewPager+GridView实现左右滑动查看更多分类的功能
看下效果图,自己考虑下自己会如何实现,然后再继续看看作者的实现~ 不记得什么时候,我留意到到美团网首页有使用ViewPager+GridView实现左右滑动查看更多分类的一个功能,感觉它很有趣,于是想 ...
- demo.testfire.net 靶场测试流程记录
demo.testfire.net span::selection, .CodeMirror-line > span > span::selection { background: #d7 ...
- CVE-2017-17215 - 华为HG532命令注入漏洞分析
前言 前面几天国外有个公司发布了该漏洞的详情.入手的二手 hg532 到货了,分析测试一下. 固件地址:https://ia601506.us.archive.org/22/items/RouterH ...
- Pwn with File结构体(一)
前言 本文由 本人 首发于 先知安全技术社区: https://xianzhi.aliyun.com/forum/user/5274 利用 FILE 结构体进行攻击,在现在的 ctf 比赛中也经常出现 ...
- 如何使用Nginx和uWSGI或Gunicorn在Ubuntu上部署Flask Web应用
你好!欢迎阅读我的博文,你可以跳转到我的个人博客网站,会有更好的排版效果和功能. 此外,本篇博文为本人Pushy原创,如需转载请注明出处:https://pushy.site/posts/151981 ...
- Android如何使用WebView访问https的网站
Android中可以用WebView来访问http和https的网站,但是默认访问https网站时,假如证书不被Android承认,会出现空白页面,且不会有任何提示信息,这时我们必须加多一些配置. 此 ...