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.
What's inside 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.
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.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.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.
Overview of EasyExcel core capabilities
EasyExcel is designed to provide high-performance Excel processing with three primary advantages:
- Fast Reading: Optimized for quickly reading data from Excel files.
- Concise Mapping: Simplifies code by mapping Excel columns directly to entity classes.
- Large File Support: Reduces memory consumption when reading or writing large files by utilizing disk caching.
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.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.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:
- 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.
- 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.
EasyExcel Core Class Overview
EasyExcel uses a builder pattern to configure Excel operations. The hierarchy is as follows:
- Entry Point:
EasyExcelis the main entry class used to initiate operations. - Workbook Level:
ExcelReaderBuilderandExcelWriterBuildercreateReadWorkbookandWriteWorkbookobjects (representing the entire Excel file). One should be created per file. - Sheet Level:
ExcelReaderSheetBuilderandExcelWriterSheetBuildercreateReadSheetandWriteSheetobjects (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.
- Entry Point:
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.
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();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'sGraphics2Dand sets it as the worksheet background viaXSSFWorkbook.Crucially, you must set
.inMemory(true)during the write process. This is because EasyExcel defaults toSXSSFWorkbookfor memory efficiency, butSXSSFWorkbookdoes not support the complex styling required for background watermarks;inMemory(true)forces the use ofXSSFWorkbook.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());