Backend Development 16 min read

Efficient Excel Processing with EasyExcel in Java

This article introduces EasyExcel, a Java library that simplifies high‑performance Excel read/write operations, explains its core concepts, provides practical code examples for reading, writing, and filling Excel files, and discusses performance‑optimizing techniques and important usage considerations.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Efficient Excel Processing with EasyExcel in Java

EasyExcel is a Java‑based library that offers fast, memory‑efficient handling of large Excel files, abstracting away performance and memory concerns while supporting various formats such as Excel, CSV, and TSV.

The article first presents an overview of EasyExcel, highlighting its ability to read and write Excel data without manual POI handling, and mentions its widespread adoption in Java projects.

Reading Excel Files

EasyExcel provides multiple ways to read data, including reading specific sheets or columns. A basic example for reading a specific sheet is shown below:

//读取指定Sheet
List<YourModelName> list = EasyExcel.read(fileName)
    .sheet(sheetNo)
    .head(YourModelName.class)
    .doReadSync();

Reading specific columns can be done with a custom ReadRowFilter :

//读取指定列
List<Object> list = EasyExcel.read(fileName)
    .sheet(sheetNo)
    .headRow(rowNo)
    .readRowFilter(new ReadRowFilter() {
        @Override
        public boolean doFilter(List<Object> list) {
            if (list.get(0).equals("某一列的值")) {
                return true;
            }
            return false;
        }
    })
    .doReadSync();

Writing Excel Files

Writing data back to Excel is equally straightforward. The following snippet writes data to a specific sheet:

//向指定Sheet写入数据
ExcelWriter excelWriter = null;
try {
    excelWriter = EasyExcel.write(fileName, YourModelName.class).build();
    WriteSheet writeSheet = EasyExcel.writerSheet(sheetNo).build();
    excelWriter.write(dataList, writeSheet);
} finally {
    if (excelWriter != null) {
        excelWriter.finish();
    }
}

Writing to a specific row can be achieved by adding a WriteTable and setting the relative head row index:

//向指定行写入数据
ExcelWriter excelWriter = null;
try {
    excelWriter = EasyExcel.write(fileName, YourModelName.class).build();
    WriteSheet writeSheet = EasyExcel.writerSheet(sheetNo).build();
    excelWriter.write(dataList, writeSheet, new WriteTable())
        .relativeHeadRowIndex(rowNo)
        .doWrite();
} finally {
    if (excelWriter != null) {
        excelWriter.finish();
    }
}

Filling Excel

EasyExcel also supports object‑based filling and custom filling. Example of object filling:

//对象填充
List<YourModelName> dataList = getDataList();
OutputStream outputStream = new FileOutputStream(fileName);
EasyExcel.write(outputStream, YourModelName.class)
    .sheet(sheetNo)
    .doWrite(dataList);
outputStream.close();

Custom filling with separate header and data lists:

//自定义填充
List<Object> headList = getHeadList();
List<List<Object>> dataList = getDataList();
ExcelWriter excelWriter = null;
try {
    excelWriter = EasyExcel.write(fileName).build();
    WriteSheet writeSheet = EasyExcel.writerSheet(sheetNo).build();
    FillConfig fillConfig = FillConfig.builder()
        .direction(WriteDirectionEnum.VERTICAL)
        .build();
    excelWriter.fill(headList, fillConfig, writeSheet);
    excelWriter.fill(dataList, fillConfig, writeSheet);
} finally {
    if (excelWriter != null) {
        excelWriter.finish();
    }
}

Important Considerations

EasyExcel trims cell content by default; disable trimming when preserving whitespace.

For large datasets, use asynchronous reading to improve performance.

//禁止EasyExcel对单元格内容进行trim操作
Object value = cell.getStringCellValue();
cell.setCellValue(new HSSFRichTextString(value.toString()));
//异步读取
EasyExcel.read(fileName, YourModelName.class, new ReadListener
() {
    // override methods
}).sheet().doRead();

Core Implementation Overview

The library is built on top of Apache POI and leverages Java reflection, generics, and annotations to map Excel rows to Java objects. Key steps for reading include creating a Workbook , iterating rows and cells, and using reflection to populate object fields. Writing follows the reverse process, creating a Workbook , setting headers, and writing object fields into cells.

Performance optimizations employed by EasyExcel include thread‑pool based concurrent read/write, caching of Workbook and Sheet objects, and batch processing of rows.

Annotations

Annotations such as @ExcelProperty , @ExcelIgnore , @ExcelPropertyRange , and @ExcelHeadRowNumber control column mapping, ignore fields, value ranges, and header row positions respectively.

Sample Core Classes

The ExcelReader class handles reading logic, with methods like read(ReadSheet) and process(ReadSheet) that convert rows into Java objects. The ExcelWriter class manages writing, with write(List, WriteSheet) and internal write(List) methods that generate headers and populate cells.

In conclusion, EasyExcel provides a concise, high‑performance solution for Java developers to handle Excel data, offering flexible APIs for reading, writing, and filling, while abstracting the complexities of POI and enabling efficient large‑scale data processing.

BackendJavaPerformancereflectionEasyExcelExcelDataProcessing
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.