Master EasyExcel: Fast, Low‑Memory Excel Import/Export in Java
This guide explains how to use Alibaba's EasyExcel library for efficient, low‑memory Excel import and export in Java, covering its core features, common annotations, Maven dependencies, listener implementation, and practical code examples for both HTTP‑based and local file operations.
1. EasyExcel Overview
EasyExcel is an Alibaba open‑source POI plugin that simplifies Excel handling by avoiding POI’s complex API, reducing memory usage with SAX‑style streaming, and preventing OOM errors during large data processing.
It loads files row by row, discarding unnecessary style information to lower memory consumption.
2. Key Advantages
Annotation‑driven custom operations.
Simplified input/output with dedicated interfaces.
Supports cell merging and other flexible features.
3. Common Annotations
@ExcelProperty – Maps a field to a specific Excel column by name or index. Use either all indexes, all names, or none; mixing them is discouraged.
@ExcelIgnore – Excludes the annotated field from Excel read/write.
@DateTimeFormat – Converts Excel date strings to Java dates using a java.text.SimpleDateFormat pattern.
@NumberFormat – Converts Excel numeric strings to Java numbers using a java.text.DecimalFormat pattern.
@ExcelIgnoreUnannotated – Fields without @ExcelProperty are ignored during read/write.
4. Maven Dependencies
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>5. Listener Implementation
/**
* EasyExcel import listener
*/
public class ExcelListener extends AnalysisEventListener {
private List<Object> datas = new ArrayList<>();
@Override
public void invoke(Object o, AnalysisContext analysisContext) {
datas.add(o);
doSomething(o); // custom business logic
}
private void doSomething(Object object) {
// e.g., persist to database
}
public List<Object> getDatas() {
return datas;
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
// cleanup if needed
}
}6. HTTP‑Based Excel Import
try {
String filename = file.getOriginalFilename();
InputStream inputStream = file.getInputStream();
ExcelListener listener = new ExcelListener();
ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.XLS, null, listener);
excelReader.read(new Sheet(1, 0, Test.class));
List<Object> list = listener.getDatas();
if (list.size() > 1) {
for (Object obj : list) {
// process each row
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}7. HTTP‑Based Excel Export
try {
String filenames = "111111";
String userAgent = request.getHeader("User-Agent");
if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
filenames = URLEncoder.encode(filenames, "UTF-8");
} else {
filenames = new String(filenames.getBytes("UTF-8"), "ISO-8859-1");
}
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
response.addHeader("Content-Disposition", "filename=" + filenames + ".xlsx");
EasyExcel.write(response.getOutputStream(), Test.class)
.sheet("sheet")
.doWrite(testList);
} catch (Exception e) {
// handle exception
}8. Local File Import & Export
// Import from local file
File file = new File("C:/Users/Administrator/Desktop/json.xlsx");
InputStream inputStream = new FileInputStream(file);
ExcelListener listener = new ExcelListener();
ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.XLS, null, listener);
excelReader.read(new Sheet(1, 0, Test.class));
List<Object> list = listener.getDatas();
// Export to local file
String outPath = "C:/Users/Administrator/Desktop/json" + System.currentTimeMillis() + ".xlsx";
EasyExcel.write(outPath, Test.class)
.sheet("sheet")
.doWrite(testList);The above steps provide a complete introduction to using EasyExcel for efficient Excel read/write operations in Java projects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
