百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

java百万级别excel导出(easyExcel,阿里出品,超赞)

ztj100 2024-11-05 13:27 26 浏览 0 评论

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导出,如果有什么疑问或者不足,欢迎大家的留言。

相关推荐

10条军规:电商API从数据泄露到高可用的全链路防护

电商API接口避坑指南:数据安全、版本兼容与成本控制的10个教训在电商行业数字化转型中,API接口已成为连接平台、商家、用户与第三方服务的核心枢纽。然而,从数据泄露到版本冲突,从成本超支到系统崩溃,A...

Python 文件处理在实际项目中的困难与应对策略

在Python项目开发,文件处理是一项基础且关键的任务。然而,在实际项目中,Python文件处理往往会面临各种各样的困难和挑战,从文件格式兼容性、编码问题,到性能瓶颈、并发访问冲突等。本文将深入...

The Future of Manufacturing with Custom CNC Parts

ThefutureofmanufacturingisincreasinglybeingshapedbytheintegrationofcustomCNC(ComputerNumericalContro...

Innovative Solutions in Custom CNC Machining

Inrecentyears,thelandscapeofcustomCNCmachininghasevolvedrapidly,drivenbyincreasingdemandsforprecisio...

C#.NET serilog 详解(c# repository)

简介Serilog是...

Custom CNC Machining for Small Batch Production

Inmodernmanufacturing,producingsmallbatchesofcustomizedpartshasbecomeanincreasinglycommondemandacros...

Custom CNC Machining for Customized Solutions

Thedemandforcustomizedsolutionsinmanufacturinghasgrownsignificantly,drivenbydiverseindustryneedsandt...

Revolutionizing Manufacturing with Custom CNC Parts

Understandinghowmanufacturingisevolving,especiallythroughtheuseofcustomCNCparts,canseemcomplex.Thisa...

Breaking Boundaries with Custom CNC Parts

BreakingboundarieswithcustomCNCpartsinvolvesexploringhowadvancedmanufacturingtechniquesaretransformi...

Custom CNC Parts for Aerospace Industry

Intherealmofaerospacemanufacturing,precisionandreliabilityareparamount.Thecomponentsthatmakeupaircra...

Cnc machining for custom parts and components

UnderstandingCNCmachiningforcustompartsandcomponentsinvolvesexploringitsprocesses,advantages,andcomm...

洞察宇宙(十八):深入理解C语言内存管理

分享乐趣,传播快乐,增长见识,留下美好。亲爱的您,这里是LearingYard学苑!今天小编为大家带来“深入理解C语言内存管理”...

The Art of Crafting Custom CNC Parts

UnderstandingtheprocessofcreatingcustomCNCpartscanoftenbeconfusingforbeginnersandevensomeexperienced...

Tailored Custom CNC Solutions for Automotive

Intheautomotiveindustry,precisionandefficiencyarecrucialforproducinghigh-qualityvehiclecomponents.Ta...

关于WEB服务器(.NET)一些经验累积(一)

以前做过技术支持,把一些遇到的问题累积保存起来,现在发出了。1.问题:未能加载文件或程序集“System.EnterpriseServices.Wrapper.dll”或它的某一个依赖项。拒绝访问。解...

取消回复欢迎 发表评论: