Backend Development 10 min read

Optimizing Large-Scale Excel Import/Export in Java with EasyExcel, Generics, and Thread Pools

This article explains how to efficiently handle massive Excel import and export tasks in Java by using EasyExcel, Java 8 functional programming, reflection, generics, thread‑pool batching, and custom ReadListener implementations, while also providing Maven dependencies and reusable export utilities.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Optimizing Large-Scale Excel Import/Export in Java with EasyExcel, Generics, and Thread Pools

Java web applications often need to import and export Excel files, but libraries like EasyPOI or Hutool can cause high memory consumption; EasyExcel mitigates this but still risks OOM with very large datasets.

To further optimize, the article proposes four strategies: using Java 8 functional programming for low‑code imports, leveraging reflection for generic single‑interface imports, employing a thread‑pool to batch‑process massive data, and using generics for flexible data export.

Maven dependency for EasyExcel:

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

A generic entity class StuInfo is shown with Lombok annotations, field mappings, and Excel property configurations, illustrating how to map Java objects to Excel columns.

Custom UploadDataListener implements ReadListener to batch‑save rows (default 100) using a predicate filter and a consumer for persistence, handling both per‑row processing and final cleanup.

For schema‑agnostic imports, NonClazzOrientedListener reads each row as a Map<Integer,String> , accumulates rows in batches, and processes them via a consumer, also supporting header analysis to align Excel columns with database fields.

Header handling is demonstrated with an overridden invokeHead method that converts header cells to a string map, filters columns present in the target table, and logs the mapping for dynamic SQL generation.

To maximize CPU utilization for huge datasets, the article suggests wrapping the listener execution in a thread pool, noting that listeners cannot be managed by the IoC container.

Reusable export utilities are provided: commonExport for generic object lists and exportFreely for arbitrary data and custom headers, both configuring HTTP response headers and invoking EasyExcel to write the workbook.

Finally, the article includes a promotional note for an "Enterprise‑Level Backend Practices" booklet covering JVM, database tuning, and performance optimization, priced at 11.9 CNY with a QR code for purchase.

JavaBackend DevelopmentgenericsEasyExcelExcel importthread pool
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

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.