EasyExcel Method Encapsulation for Simplified Excel Import and Export in Java
This article introduces a lightweight Java wrapper around Alibaba's EasyExcel library that enables one‑line Excel import and export, explains required Maven dependencies, essential utility classes, and provides complete code examples for reading, writing single‑sheet and multi‑sheet workbooks with custom model mappings.
Recently I discovered Alibaba's EasyExcel project on GitHub and created a simple wrapper so that a single method can handle both Excel import and export; the wrapper has been updated to fix bugs and is shared here for reference.
The source code of the wrapper is available at https://github.com/HowieYuan/easyexcel-method-encapsulation .
EasyExcel is known for its low memory consumption; although its feature set is currently basic, it fully satisfies common read/write scenarios.
1. Maven Dependency
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>1.0.2</version>
</dependency>2. Required Classes
• ExcelUtil : utility class that provides static methods for reading and writing Excel files.
• ExcelListener : a listener extending AnalysisEventListener to collect rows during import. Example implementation:
public class ExcelListener extends AnalysisEventListener {
// temporary storage for data
private List
datas = new ArrayList<>();
@Override
public void invoke(Object object, AnalysisContext context) {
datas.add(object);
doSomething(object);
}
private void doSomething(Object object) {
// custom business logic
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
// cleanup if needed
}
public List
getDatas() {
return datas;
}
public void setDatas(List
datas) {
this.datas = datas;
}
}• ExcelWriterFactory : used for exporting multiple sheets. • ExcelException : custom exception wrapper for EasyExcel errors. 3. Reading Excel Import is as simple as calling ExcelUtil.readExcel() . Example Spring controller method: @RequestMapping(value = "readExcel", method = RequestMethod.POST) public Object readExcel(MultipartFile excel) { return ExcelUtil.readExcel(excel, new ImportInfo()); } The ImportInfo class must extend BaseRowModel and use @ExcelProperty to map columns: public class ImportInfo extends BaseRowModel { @ExcelProperty(index = 0) private String name; @ExcelProperty(index = 1) private String age; @ExcelProperty(index = 2) private String email; // getters and setters omitted for brevity } 4. Exporting Excel (single sheet) Use ExcelUtil.writeExcelWithSheets() to generate a workbook with one sheet: @RequestMapping(value = "writeExcel", method = RequestMethod.GET) public void writeExcel(HttpServletResponse response) throws IOException { List list = getList(); String fileName = "一个 Excel 文件"; String sheetName = "第一个 sheet"; ExcelUtil.writeExcel(response, list, fileName, sheetName, new ExportInfo()); } The ExportInfo model also extends BaseRowModel and defines column headers via @ExcelProperty(value = "列名", index = n) : public class ExportInfo extends BaseRowModel { @ExcelProperty(value = "姓名", index = 0) private String name; @ExcelProperty(value = "年龄", index = 1) private String age; @ExcelProperty(value = "邮箱", index = 2) private String email; @ExcelProperty(value = "地址", index = 3) private String address; } 5. Exporting Excel (multiple sheets) First call writeExcelWithSheets() for the initial sheet, then chain write() for subsequent sheets, and finish with finish() : public void writeExcelWithSheets(HttpServletResponse response) throws IOException { List list = getList(); String fileName = "一个 Excel 文件"; String sheetName1 = "第一个 sheet"; String sheetName2 = "第二个 sheet"; String sheetName3 = "第三个 sheet"; ExcelUtil.writeExcelWithSheets(response, list, fileName, sheetName1, new ExportInfo()) .write(list, sheetName2, new ExportInfo()) .write(list, sheetName3, new ExportInfo()) .finish(); } 6. Complex Header Example For multi‑line headers, define a model with an array of header values: public class MultiLineHeadExcelModel extends BaseRowModel { @ExcelProperty(value = {"表头1","表头1","表头31"}, index = 0) private String p1; @ExcelProperty(value = {"表头1","表头1","表头32"}, index = 1) private String p2; @ExcelProperty(value = {"表头3","表头3","表头3"}, index = 2) private int p3; // ... other fields omitted for brevity } The article concludes with a friendly reminder to like and share if the content is helpful.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.