EasyExcel Quick Guide: Features, Annotations, Dependencies, and Usage Examples
This article introduces Alibaba's EasyExcel library, explains its memory‑efficient design, lists its main advantages and common annotations, provides Maven dependencies, and demonstrates import and export code snippets for Java backend applications.
EasyExcel is an Alibaba open‑source POI plugin that simplifies Excel read/write by using a streaming SAX approach, reducing memory consumption and avoiding OOM errors.
Key advantages include annotation‑based custom operations, simple input/output interfaces, and support for cell merging.
Common annotations: @ExcelProperty: maps a field to an Excel column by name or index. @ExcelIgnore: excludes a field from read/write. @DateTimeFormat: formats date values using java.text.SimpleDateFormat. @NumberFormat: formats numeric values using java.text.DecimalFormat. @ExcelIgnoreUnannotated: only annotated fields participate in read/write.
Typical 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>A listener class extending AnalysisEventListener can collect rows during import:
public class ExcelListener extends AnalysisEventListener {
private List<Object> datas = new ArrayList<>();
@Override
public void invoke(Object o, AnalysisContext ctx) {
datas.add(o);
// custom processing
}
@Override
public void doAfterAllAnalysed(AnalysisContext ctx) {
// optional cleanup
}
public List<Object> getDatas() { return datas; }
public void setDatas(List<Object> datas) { this.datas = datas; }
}Import example reads an uploaded file, creates the listener, and obtains a list of objects:
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) {
Test test = (Test) obj;
// process each test object
}
}Export example writes data to the HTTP response:
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);Local file import/export uses the same listener and ExcelReader / EasyExcel.write methods with file streams:
String filePath = "C:\\Users\\Administrator\\Desktop\\json.xlsx";
File file = new File(filePath);
InputStream is = new FileInputStream(file);
ExcelListener listener = new ExcelListener();
ExcelReader reader = new ExcelReader(is, ExcelTypeEnum.XLS, null, listener);
reader.read(new Sheet(1, 0, Test.class));
List<Object> list = listener.getDatas();
// ...process list...
String outPath = "C:\\Users\\Administrator\\Desktop\\output" + System.currentTimeMillis() + ".xlsx";
EasyExcel.write(outPath, Test.class).sheet("sheet").doWrite(testList);The article ends with an invitation to follow the author’s WeChat public account for additional resources and a reminder that the content originates from the original author.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
