关于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如何 ...
随机推荐
- WPF流程图制作系列相关基础二
我们现在知道 thumb ,可以让用户自行拖动其在 canvas上移动,在这个而基础上 我们可以试着往流程图方向靠近一下. 我们知道,流程图,都是一个一个的流程块,然后用线连起来的,这一个一个的 ...
- MVC 中文显示乱码问题
在学习中遇到中文乱码的问题,在网上搜了一下大神的解决方法,总结了一点知识点. 项目中的代码 //GET: /HelloWorld/Welcome/ public string Welcome(stri ...
- Code Signal_练习题_adjacentElementsProduct
Given an array of integers, find the pair of adjacent elements that has the largest product and retu ...
- javascript中让你捉摸不定的this
this到底指向谁,估计很多人在使用javascript的过程中都遇到过,这个关键字如果没搞懂,在一些高级功能中都会困难重重,搜了下相关文章,介绍的都挺多的,也有很深入的,比如汤姆大叔的<深入理 ...
- A Complete Tutorial to Learn Data Science with Python from Scratch
A Complete Tutorial to Learn Data Science with Python from Scratch Introduction It happened few year ...
- 一些不错的Android开源音视频播放器
摘要:来自Github上的一点点整理,希望对你有用! 整理了一下Github上几个开源的音视频播放器项目,有兴趣的同学可以clone代码去研究学习. 1.UniversalMusicPlayer ht ...
- Vue 框架-10-搭建脚手架 CLI
Vue 框架-10-搭建脚手架 CLI + 批处理快捷启动 脚手架是通过 webpack 搭建的开发环境 使用 ES6 语法 打包和压缩 JS 为一个文件 项目文件在环境中,而不是浏览器 实现页面自动 ...
- CSS 小结笔记之清除浮动
浮动是一个非常好用的属性,但是有时会出现一些问题,需要进行清除浮动.例如 <!DOCTYPE html> <html lang="en"> <head ...
- git pull 错误:The following untracked working tree files would be overwritten by merge
错误描述: $ git pull origin alphaFrom https://github.com/shirley-wu/HeartTrace * branch alpha ...
- 在 Azure 中备份 Linux 虚拟机
可以通过定期创建备份来保护数据. Azure 备份可创建恢复点,这些恢复点存储在异地冗余的恢复保管库中. 从恢复点还原时,可以还原整个 VM,或只是还原特定的文件. 本文介绍如何将单个文件还原到运行 ...