Alibaba开源工具EasyExcel读写Excel文件

本文介绍了阿里巴巴的开源工具EasyExcel,它是一个轻量级的、高效的处理Excel的Java框架,能够避免OOM问题。文章详细讲解了如何安装EasyExcel,以及如何使用它将数据写入和读出Excel文件,包括创建实体对象、配置监听器等关键步骤。
摘要由CSDN通过智能技术生成

前言
工作中使用 excel 表格处理数据是很常见的操作,经常会使用 excel 文件来导入数据或者导出数据,是不是脑海中已经思考有哪些处理excel框架
小编之前呢就知道Java 解析、生成 Excel 比较有名的框架有 Apache poi、jxl,但他们都存在一个严重的问题就是非常的耗内存,poi 有一套 SAX 模式的 API 可以一定程度的解决一些内存溢出的问题,但仍有一些缺陷,比如版本兼容、代码臃肿等。
下面小编推荐给大家看一款非常好用的表格开源框架EasyExcelAlibaba 的 开源的一款快速、简单、可避免 OOM 的 java 处理 Excel 工具。easyexcel 重写了 poi 对 07 版 Excel 的解析,对原本一个 3M 的 excel 用 POI sax 需要 100M 左右的内存降低到几 M,并且再大的 excel 也不会出现内存溢出。
那我们就话不多说,赶紧上代码干货!

一、EasyExcel安装

环境要求:maven + Java JDK 1.8 + easyexcel 2.1.1,新建一个 maven 应用,然后 pom.xml 文件导入 EasyExcel的依赖,如下图所示

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>easyexcel</artifactId>
	<version>2.1.1</version>
</dependency>

二、将数据写入Excel

1.创建excel对应的实体对象

接下来我们需要创建excel对应的实体对象类,实际上就是Excel数据表中的表头属性,贴代码如图所示。

import com.alibaba.excel.annotation.ExcelProperty;

/**
 * @param
 * @author wcy
 * @create 2022/10/15
 * @return
 * @description
 **/
public class UserData {
   
    @ExcelProperty(value = "用户编号",index = 0)
    private int id;
    @ExcelProperty(value = "用户名",index = 1)
    private String username;
    @ExcelProperty(value = "性别",index = 2)
    private String gender;
    @ExcelProperty(value = "工资",index = 3)
    private Double salary
  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
EasyExcel 是一个基于阿里巴巴开源工具 EasyPOI,专为解决Java中操作 Excel 的问题而生的。它提供了简单易用的 Java API,可以方便地读写 Excel 文件,并支持大批量数据的导入导出。 以下是一个基于 EasyExcelExcel 工具类示例: ```java import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelReader; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.metadata.Table; import com.alibaba.excel.support.ExcelTypeEnum; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.Map; public class ExcelUtil { /** * 读取 Excel 文件数据,返回 List 列表 * * @param inputStream Excel 文件输入流 * @param sheetNo 工作表序号(从 0 开始) * @param headLineNum 标题行数(默认为 1) * @return List 列表 */ public static List<List<String>> readExcel(InputStream inputStream, int sheetNo, int headLineNum) { List<List<String>> list = new ArrayList<>(); ExcelReader excelReader = EasyExcel.read(inputStream).build(); try { Sheet sheet = new Sheet(sheetNo, headLineNum); excelReader.read(sheet); } finally { excelReader.finish(); } return list; } /** * 读取 Excel 文件数据,并通过回调函数处理每行数据 * * @param inputStream Excel 文件输入流 * @param sheetNo 工作表序号(从 0 开始) * @param headLineNum 标题行数(默认为 1) * @param listener 回调函数 */ public static void readExcel(InputStream inputStream, int sheetNo, int headLineNum, AnalysisEventListener<Map<Integer, String>> listener) { ExcelReader excelReader = EasyExcel.read(inputStream, listener).build(); try { Sheet sheet = new Sheet(sheetNo, headLineNum); excelReader.read(sheet); } finally { excelReader.finish(); } } /** * 写入 Excel 文件数据,返回写入的行数 * * @param outputStream Excel 文件输出流 * @param sheetNo 工作表序号(从 0 开始) * @param head 表头数据列表 * @param dataList 数据列表 * @return 写入的行数 */ public static int writeExcel(OutputStream outputStream, int sheetNo, List<String> head, List<List<Object>> dataList) { ExcelWriter excelWriter = EasyExcel.write(outputStream).build(); try { Sheet sheet = new Sheet(sheetNo, 0); Table table = new Table(0); List<List<String>> headList = new ArrayList<>(); headList.add(head); table.setHead(headList); excelWriter.write1(dataList, sheet, table); return dataList.size(); } finally { excelWriter.finish(); } } /** * 获取单元格的值 * * @param cell 单元格 * @return 单元格的值 */ public static Object getCellValue(Cell cell) { if (cell == null) { return null; } CellType cellType = cell.getCellType(); switch (cellType) { case BLANK: return null; case BOOLEAN: return cell.getBooleanCellValue(); case ERROR: return cell.getErrorCellValue(); case FORMULA: return cell.getCellFormula(); case NUMERIC: return cell.getNumericCellValue(); case STRING: return cell.getStringCellValue(); default: return null; } } } ``` 该工具类提供了 Excel 文件的读取和写入功能,并支持大批量数据的导入导出。可以通过调用 `readExcel()` 方法读取 Excel 文件数据,并通过回调函数处理每行数据;也可以通过调用 `writeExcel()` 方法将数据写入 Excel 文件中。`getCellValue()` 方法可以获取单元格的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值