java生成excel花费的时间_java百万级别excel导出(easyExcel,阿里出品,超赞)

java百万级别excel导出(easyExcel,阿里出品,超赞)-1.jpg (9.89 KB, 下载次数: 0)

2018-11-6 01:39 上传

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:

com.alibaba

easyexcel

1.0.4

核心代码: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:你需要处处的数据集合。

BaseRowModel:为什么要继承BaseRowModel,因为他是基类。

@ExcelProperty:对应excel中的表头。

value:表头的名称

index:排序

总结

以上就是esatExcel的excel导出,如果有什么疑问或者不足,欢迎巨匠的留言。

更多内容回复查看:

游客,如果您要查看本帖隐藏内容请回复

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值