Convert Figma logo to code with AI

alibaba logoeasyexcel

快速、简洁、解决大文件内存溢出的java处理Excel工具

31,958
7,491
31,958
480

Top Related Projects

1,915

Mirror of Apache POI

A Python module for creating Excel XLSX files.

A pure PHP library for reading and writing spreadsheet files

34,966

📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs

Quick Overview

EasyExcel is a Java-based Excel reading and writing framework developed by Alibaba. It aims to provide a simpler and more efficient way to handle Excel files compared to traditional POI, with a focus on memory optimization for large files.

Pros

  • High performance and low memory usage, especially for large Excel files
  • Easy to use API with fluent interface
  • Supports both .xls and .xlsx formats
  • Provides annotation-based mapping for easy object-to-Excel conversion

Cons

  • Limited support for complex Excel features (e.g., formulas, charts)
  • Documentation is primarily in Chinese, which may be challenging for non-Chinese speakers
  • Less mature compared to Apache POI, with potentially fewer community resources

Code Examples

  1. Reading an Excel file:
EasyExcel.read("path/to/file.xlsx", DemoData.class, new ReadListener<DemoData>() {
    @Override
    public void invoke(DemoData data, AnalysisContext context) {
        // Process each row of data
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // Do something after all data is processed
    }
}).sheet().doRead();
  1. Writing data to an Excel file:
List<DemoData> dataList = // ... your data
EasyExcel.write("path/to/output.xlsx", DemoData.class).sheet("Sheet1").doWrite(dataList);
  1. Using annotations for column mapping:
public class DemoData {
    @ExcelProperty("Name")
    private String name;

    @ExcelProperty("Age")
    private Integer age;

    // getters and setters
}

Getting Started

  1. Add EasyExcel dependency to your project:
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.2.1</version>
</dependency>
  1. Create a model class with annotations:
@Data
public class User {
    @ExcelProperty("User ID")
    private Long id;

    @ExcelProperty("Name")
    private String name;

    @ExcelProperty("Email")
    private String email;
}
  1. Read or write Excel files using EasyExcel methods as shown in the code examples above.

Competitor Comparisons

1,915

Mirror of Apache POI

Pros of POI

  • More comprehensive support for various Microsoft Office formats (Excel, Word, PowerPoint, etc.)
  • Longer history and larger community support
  • More feature-rich, offering advanced functionality for complex document manipulation

Cons of POI

  • Higher memory consumption, especially for large files
  • Steeper learning curve due to its extensive API
  • Slower performance compared to EasyExcel for simple Excel operations

Code Comparison

POI:

Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Hello, POI!");

EasyExcel:

EasyExcel.write("output.xlsx", ExcelModel.class)
    .sheet("Sheet1")
    .doWrite(Arrays.asList(new ExcelModel("Hello, EasyExcel!")));

Summary

POI offers a more comprehensive solution for working with various Microsoft Office formats, while EasyExcel focuses specifically on Excel operations with improved performance and ease of use. POI provides more advanced features but comes with higher memory usage and a steeper learning curve. EasyExcel, on the other hand, offers a simpler API and better performance for basic Excel tasks, making it a good choice for projects primarily dealing with Excel files.

A Python module for creating Excel XLSX files.

Pros of XlsxWriter

  • Written in Python, offering seamless integration with Python projects
  • Supports a wide range of Excel features, including charts, images, and data validation
  • Excellent documentation and extensive examples

Cons of XlsxWriter

  • Limited to writing Excel files, cannot read or modify existing files
  • May have slower performance for very large datasets compared to EasyExcel

Code Comparison

XlsxWriter:

import xlsxwriter

workbook = xlsxwriter.Workbook('example.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Hello Excel')
workbook.close()

EasyExcel:

EasyExcel.write("example.xlsx", ExampleData.class)
    .sheet("Sheet1")
    .doWrite(dataList);

Key Differences

  • XlsxWriter is Python-based, while EasyExcel is Java-based
  • EasyExcel focuses on simplicity and ease of use for reading and writing Excel files
  • XlsxWriter offers more advanced Excel features but is limited to writing files
  • EasyExcel provides better performance for large datasets and memory optimization

Both libraries are popular choices for working with Excel files, with XlsxWriter being more suitable for Python developers and EasyExcel catering to Java developers. The choice between them depends on the specific project requirements, programming language preference, and desired Excel features.

A pure PHP library for reading and writing spreadsheet files

Pros of PhpSpreadsheet

  • More mature and widely adopted in the PHP ecosystem
  • Supports a broader range of spreadsheet formats (XLSX, XLS, ODS, CSV, HTML)
  • Extensive documentation and community support

Cons of PhpSpreadsheet

  • Generally slower performance, especially with large datasets
  • Higher memory consumption compared to EasyExcel
  • Steeper learning curve for beginners

Code Comparison

PhpSpreadsheet:

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World!');
$writer = new Xlsx($spreadsheet);
$writer->save('hello_world.xlsx');

EasyExcel:

EasyExcel.write("hello_world.xlsx", DemoData.class)
    .sheet("Sheet1")
    .doWrite(data());

While both libraries aim to simplify spreadsheet manipulation, EasyExcel focuses on performance and ease of use for Java developers, particularly when dealing with large datasets. PhpSpreadsheet offers more comprehensive features and format support for PHP projects but may require more resources. The code comparison highlights the simplicity of EasyExcel's API compared to PhpSpreadsheet's more verbose approach.

34,966

📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs

Pros of sheetjs

  • Broader language support, including JavaScript, TypeScript, and various frameworks
  • More comprehensive file format support, including legacy Excel formats
  • Larger community and ecosystem, with more resources and third-party integrations

Cons of sheetjs

  • Steeper learning curve due to more complex API
  • Larger file size, which may impact performance in some applications
  • Commercial use requires a paid license

Code Comparison

easyexcel (Java):

EasyExcel.read("demo.xlsx", DemoData.class, new DemoDataListener())
    .sheet()
    .doRead();

sheetjs (JavaScript):

const workbook = XLSX.readFile('demo.xlsx');
const worksheet = workbook.Sheets[workbook.SheetNames[0]];
const data = XLSX.utils.sheet_to_json(worksheet);

Both libraries provide straightforward methods for reading Excel files, but easyexcel's API is more concise and tailored for Java developers. sheetjs offers more flexibility and options, but requires more code to achieve similar results.

easyexcel is specifically designed for Java applications and excels in handling large Excel files with low memory usage. It's particularly well-suited for server-side processing in Java environments.

sheetjs, on the other hand, offers broader language and format support, making it a versatile choice for web applications and cross-platform development. However, its larger file size and more complex API may be considerations for some projects.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

EasyExcel

Build Status Maven central License

新手必读

JAVA解析Excel工具

Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。
easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析依然需要100M左右内存,改用easyexcel可以降低到几M,并且再大的excel也不会出现内存溢出;03版依赖POI的sax模式,在上层做了模型转换的封装,让使用者更加简单方便

16M内存23秒读取75M(46W行25列)的Excel(3.2.1+版本)

当然还有极速模式 能更快,但是内存占用会在100M多一点 img

最新版本


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

帮忙点个⭐Star

开源不易,如果觉得EasyExcel对您的工作还是有帮助的话,请帮忙在github star 的右上角点个⭐Star,您的支持是使EasyExcel变得更好最大的动力。

如何获取帮助

优先建议自己通过文档来解决问题

其次建议通过issues来解决解决问题

可以尝试在以下2个链接搜索问题,如果不存在可以尝试创建issue。

  • 去 github 搜索issues
  • 去 gitee 搜索issues

通过 issues 解决问题,可以给后面遇到相同问题的同学查看,所以比较推荐这种方式。
不管github、gitee都会定期有人回答您的问题,比较紧急可以在提完issue以后在钉钉群艾特群主并发送issue地址帮忙解决。
QQ 公司不让用,有时候也会去看,但是核心肯定还是在钉钉。

也可以加入钉钉&QQ群来解决问题

加入钉钉或QQ群,看完公告可以获得帮助 。
比较推荐钉钉群,QQ 公司不让用,当然QQ群也会有热心网友帮忙解决。
QQ1群(已满): 662022184
QQ2群(已满): 1097936804
QQ3群(已满): 453928496
QQ4群(已满): 496594404
QQ5群(已满): 451925680
QQ6群(已满): 784741035
QQ7群(已满): 667889383
QQ8群: 113968681
钉钉1群(已满): 21960511
钉钉2群(已满): 32796397
钉钉3群(已满): 33797247
钉钉4群(已满): 33491624
钉钉5群(已满): 32134498
钉钉6群(已满): 34707941
钉钉7群(已满): 35235427
钉钉8群(已满): 44752220
钉钉9群(已满): 11045002277
钉钉10群(已满): 27360019755
钉钉11群(已满):24330026964
钉钉12群(已满):27210038956
钉钉13群:83695000992

维护者

姬朋飞(玉霄)、庄家钜

快速开始

读Excel

demo代码地址:https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java
详细文档地址:https://easyexcel.opensource.alibaba.com/docs/current/quickstart/read

    /**
    * 最简单的读
    * <p>1. 创建excel对应的实体对象 参照{@link DemoData}
    * <p>2. 由于默认一行行的读取excel,所以需要创建excel一行一行的回调监听器,参照{@link DemoDataListener}
    * <p>3. 直接读即可
    */
    @Test
    public void simpleRead() {
        String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
        // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead();
    }

写Excel

demo代码地址:https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java
详细文档地址:https://easyexcel.opensource.alibaba.com/docs/current/quickstart/write

    /**
     * 最简单的写
     * <p>1. 创建excel对应的实体对象 参照{@link com.alibaba.easyexcel.test.demo.write.DemoData}
     * <p>2. 直接写即可
     */
    @Test
    public void simpleWrite() {
        String fileName=TestFileUtil.getPath()+"write"+System.currentTimeMillis()+".xlsx";
        // 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        EasyExcel.write(fileName,DemoData.class).sheet("模板").doWrite(data());
    }

web上传、下载

demo代码地址:https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java

    /**
    * 文件下载(失败了会返回一个有部分数据的Excel)
    * <p>
    * 1. 创建excel对应的实体对象 参照{@link DownloadData}
    * <p>
    * 2. 设置返回的 参数
    * <p>
    * 3. 直接写,这里注意,finish的时候会自动关闭OutputStream,当然你外面再关闭流问题不大
    */
    @GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName=URLEncoder.encode("测试","UTF-8").replaceAll("\\+","%20");
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        EasyExcel.write(response.getOutputStream(),DownloadData.class).sheet("模板").doWrite(data());
    }
    
    /**
     * 文件上传
     * <p>1. 创建excel对应的实体对象 参照{@link UploadData}
     * <p>2. 由于默认一行行的读取excel,所以需要创建excel一行一行的回调监听器,参照{@link UploadDataListener}
     * <p>3. 直接读即可
     */
    @PostMapping("upload")
    @ResponseBody
    public String upload(MultipartFile file)throws IOException{
        EasyExcel.read(file.getInputStream(),UploadData.class,new UploadDataListener(uploadDAO)).sheet().doRead();
        return"success";
    }