如果你觉得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(该例非常简单,很通俗易懂)
来源:oschina
链接:https://my.oschina.net/u/4328499/blog/3883045