1.为什么需要excel到处?
导出功能在各个领域都被广泛的运用,当用户想把数据下载下来的时候,此时excel是一个不错的选择。
2.如何选择合适的excel导出?
选择的问题一般都比较纠结,选择了一个版本之后发现另外一个版本更适合,所以我们就应该选择一些我们相对较熟悉或者符合自己开发习惯的就行,没有必要纠结到底选择那个版本。
3.easyexcel工具
Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。easyexcel重写了poi对07版Excel的解析,能够原本一个3M的excel用POI sax依然需要100M左右内存降低到KB级别,并且再大的excel不会出现内存溢出,03版依赖POI的sax模式。在上层做了模型转换的封装,让使用者更加简单方便(此处引用gitHub)
好了,废话不多说,直接进入主题,如何运用easyExcel进行excel到处。
第一:引入jar包.
gradle:
compile("com.alibaba:easyexcel:1.0.4")
maven:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>1.0.4</version>
</dependency>
核心代码:ExcelUtils.java
package com.dctp.cloud.boss.util; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.metadata.BaseRowModel; import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.support.ExcelTypeEnum; import com.dctp.cloud.bo.BoUtil; import com.dctp.cloud.boss.bo.OTCTradeBo; import org.apache.poi.ss.formula.functions.T; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; /** * @Description: excel操作工具类 * @Author: yaomaoyang * @CreateDate: 2018/9/29 18:05 * @UpdateUser: yaomaoyang * @UpdateDate: 2018/9/29 18:05 * @UpdateRemark: 修改内容 * @Version: 1.0 */ public class ExcelUtils { /** * 导出 * @param list * @param response * @param clazz * @return */ public static BoUtil export(List<? extends BaseRowModel> list, HttpServletResponse response, Class<? extends BaseRowModel> clazz) { BoUtil boUtil = BoUtil.getDefaultFalseBo(); ServletOutputStream out = null; try { out = response.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true); try { boUtil = BoUtil.getDefaultTrueBo(); String fileName = new String( (new SimpleDateFormat("yyyy-MM-dd").format(new Date())).getBytes(), "UTF-8"); Sheet sheet2 = new Sheet(2, 3,clazz, "sheet", null); writer.write(list, sheet2); //response.setContentType("multipart/form-data"); response.setCharacterEncoding("utf-8"); response.setContentType("application/vnd.ms-excel"); response.setHeader("content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "utf-8")); //response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); out.flush(); boUtil.setMsg("导出成功"); } catch (Exception e) { e.printStackTrace(); boUtil.setMsg("导出失败"); return boUtil; } finally { writer.finish(); try { out.close(); } catch (IOException e) { e.printStackTrace(); } return boUtil; } } }
AdvertisingIndentBo.java
package com.dctp.cloud.boss.bo; import java.math.BigDecimal; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.metadata.BaseRowModel; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Builder; @SuppressWarnings("deprecation") @Data @Builder @NoArgsConstructor @AllArgsConstructor public class AdvertisingIndentBo extends BaseRowModel { // 订单编号 @ExcelProperty(value = {"广告编号"},index = 0) private String id; // 交易类型:1 买入 2卖出 private Integer tradeType; @ExcelProperty(value = {"交易"},index = 1) //交易类型(导出时使用) private String tradeTypeStr; // 用户ID begin private Long userID; private Integer login; private String alaisName; @ExcelProperty(value = {"会员"},index = 2) private String realname; private String phone; private String email; // 用户信息 end // 资产ID private Long coinID; @ExcelProperty(value = {"资产名称"},index = 3) private String coinName; // 交易状态 private Integer tradeState; //交易状态(导出时使用) @ExcelProperty(value = {"状态"},index = 4) private String tradeStateStr; // 取价类型 private Integer priceType; // 取价类型(导出时使用) @ExcelProperty(value = {"取价类型"},index = 5) private String priceTypeStr; // 平台实时价格的百分比 @ExcelProperty(value = {"溢价"},index = 6) private BigDecimal premium; // 交易单价 @ExcelProperty(value = {"价格"},index = 7) private BigDecimal price; // 交易数量 private BigDecimal cionNum; // 剩余数量 private BigDecimal residueNum; // 交易最小金额 @ExcelProperty(value = {"最小金额"},index = 8) private BigDecimal minPrice; // 交易最大金额 @ExcelProperty(value = {"最大金额"},index = 9) private BigDecimal maxPrice; // 付款方式类型 @ExcelProperty(value = {"付款类型"},index = 10) private String payType; // 付款期限30-120分钟之内 @ExcelProperty(value = {"付款期限"},index = 11) private Integer payTime; // 创建时间 @ExcelProperty(value = {"创建时间"},index = 12) private String createTime; // 交易密码 private String password; /** * 创建人 */ private Long createBy; /** * 修改人 */ private Long updateBy; private String activeFlag; }
controller: boUtil = ExcelUtils.export(list, response, AdvertisingIndentBo.class);
list:LIst<AdvertisingIndentBo>:你需要到处的数据集合。
BaseRowModel:为什么要继承BaseRowModel,因为他是基类。
@ExcelProperty:对应excel中的表头。
value:表头的名称
index:排序
总结
以上就是esatExcel的excel到处,如果有什么疑问或者不足,欢迎大家的留言。