EasyExcel Documentation

website·Indexed 17 days ago

https://easyexcel.opensource.alibaba.com/

Documentation for EasyExcel, an Alibaba open-source library for reading and writing Excel and CSV files. Includes guides on basic functions, template filling, dynamic headers, cell merging, image handling, and a comprehensive Q&A section covering common issues and expert solutions.

Tokens
52.6K
Snippets
189
Records
284
Agent score
98%

What's inside EasyExcel

  1. Overview of Alibaba EasyExcel

    EasyExcel is an open-source Java library by Alibaba designed for reading and writing Excel files. It is specifically optimized for handling large files to prevent OutOfMemory (OOM) errors. Unlike Apache POI or jxl, EasyExcel re-implements the parsing of Excel 07 files to significantly reduce memory consumption while maintaining high performance and ease of use.
  2. Overview of EasyExcel for Java

    EasyExcel is a high-efficiency Excel processing tool for Java designed to prevent OutOfMemory (OOM) errors when handling large files. It uses optimized parsing algorithms to significantly reduce memory consumption compared to Apache POI and jxl. For Excel 2007+ files, it consumes far less memory than POI's SAX mode. For Excel 2003 files, it wraps POI's SAX mode with a model conversion layer to improve usability.
  3. Overview of EasyExcel for Java

    EasyExcel is a high-efficiency Excel processing tool for Java designed to prevent OutOfMemory (OOM) errors when handling large files. Unlike traditional libraries like Apache POI or jxl, EasyExcel optimizes the parsing of Excel 07 files to significantly reduce memory consumption, ensuring stability even when processing millions of rows of data.
  4. Overview of EasyExcel for Java

    EasyExcel is a high-efficiency Excel processing tool designed for Java. Its primary purpose is to prevent OutOfMemory (OOM) errors when reading or writing large Excel files. Compared to traditional libraries like Apache POI and jxl, EasyExcel is more lightweight and efficient for large datasets.

    Key advantages include:

    • High Memory Efficiency: Re-implements the parsing logic for .xlsx files to significantly reduce memory footprint, allowing the processing of millions of rows without OOM.
    • Simplified API: Provides an intuitive API that abstracts low-level details for reading and writing.
    • High Performance: Optimized for speed when handling large volumes of data.
    • Model Conversion: For .xls files, it wraps POI's SAX mode with a model conversion layer to improve the developer experience.
  5. Overview of EasyExcel core capabilities

    EasyExcel is designed to provide high-performance Excel processing with three primary advantages:

    1. Fast Reading: Optimized for quickly reading data from Excel files.
    2. Concise Mapping: Simplifies code by mapping Excel columns directly to entity classes.
    3. Large File Support: Reduces memory consumption when reading or writing large files by utilizing disk caching.
  6. Overview of EasyExcel

    EasyExcel is an open-source Java project designed for reading and writing Excel files with minimal memory consumption. It is specifically optimized to handle large Excel files (hundreds of megabytes) while keeping memory usage low. For example, it can read a 75MB Excel file (460,000 rows, 25 columns) within one minute using only 64MB of memory.
  7. EasyExcel Maintenance Status (v3.1.x-4.x.x)

    EasyExcel versions 3.1.x through 4.x.x have entered maintenance mode. The project will continue to provide bug fixes to ensure basic functional stability, but no new features will be actively added. Users are encouraged to evaluate and migrate to other data processing tools if needed.
  8. Understand EasyExcel memory management advantages over Apache POI

    EasyExcel optimizes memory consumption when processing large Excel files to prevent OutOfMemory (OOM) errors. Its primary advantages include:

    1. Optimized Parsing for .xlsx (07 version): EasyExcel rewrites the parsing mechanism used by Apache POI. While Apache POI (even in SAX mode) may require ~100MB of memory to process a 3MB file due to in-memory decompression and storage, EasyExcel reduces this requirement to a few megabytes and maintains stable memory usage regardless of file size.
    2. Model Conversion for .xls (03 version): For older Excel formats, EasyExcel wraps Apache POI's SAX mode with a model conversion layer. This simplifies the development process and improves memory efficiency by abstracting the underlying complexity.
  9. EasyExcel Core Class Overview

    EasyExcel uses a builder pattern to configure Excel operations. The hierarchy is as follows:

    • Entry Point: EasyExcel is the main entry class used to initiate operations.
    • Workbook Level: ExcelReaderBuilder and ExcelWriterBuilder create ReadWorkbook and WriteWorkbook objects (representing the entire Excel file). One should be created per file.
    • Sheet Level: ExcelReaderSheetBuilder and ExcelWriterSheetBuilder create ReadSheet and WriteSheet objects (representing individual sheets). One must be created for every sheet processed.
    • Data Processing:
      • ReadListener: Called after every row is read to process data.
      • WriteHandler: Called during various writing stages (e.g., creating cells or tables) to handle data.
    • Configuration Inheritance: Settings are inherited. Workbook-level configurations are inherited by the Sheet. Parameters set before the .sheet() method apply to the whole workbook; those set after apply to the specific sheet.
  10. Understand EasyExcel writing hierarchy (Workbook, Sheet, Table)

    EasyExcel uses a hierarchical structure for writing files:

    • WriteWorkbook: Represents the entire Excel file (.xlsx/.xls).
    • WriteSheet: Represents a single worksheet/tab within the workbook.
    • WriteTable: Used when a single worksheet contains multiple distinct data tables.
  11. Perform synchronous reading of Excel files

    Synchronous reading loads the Excel data directly into memory and returns a list. This method is not recommended for large datasets as it may cause memory issues. You can read the data into a specific Java class or as a list of maps where the key is the column index and the value is the cell content.
    String fileName = "demo.xlsx";
    
    // Option 1: Read into a specific class
    List<DemoData> list = EasyExcel.read(fileName).head(DemoData.class).sheet().doReadSync();
    
    // Option 2: Read into a List of Maps (Key: Column Index, Value: Cell Value)
    List<Map<Integer, String>> listMap = EasyExcel.read(fileName).sheet().doReadSync();
  12. Add watermarks to exported Excel files using EasyExcel

    To add a watermark to an Excel file during export with EasyExcel, you must implement a custom SheetWriteHandler (e.g., WaterMarkHandler) that generates a watermark image using Java's Graphics2D and sets it as the worksheet background via XSSFWorkbook.

    Crucially, you must set .inMemory(true) during the write process. This is because EasyExcel defaults to SXSSFWorkbook for memory efficiency, but SXSSFWorkbook does not support the complex styling required for background watermarks; inMemory(true) forces the use of XSSFWorkbook.

    EasyExcel.write(response.getOutputStream(), DemoData.class)
        .inMemory(true) // Required to use XSSFWorkbook instead of SXSSFWorkbook
        .registerWriteHandler(new WaterMarkHandler("My Watermark Text"))
        .sheet("Template")
        .doWrite(data());