Using Alibaba EasyExcel for Reading and Writing Excel Files in Java
This article provides a comprehensive guide on integrating Alibaba's EasyExcel library in Java projects, covering environment setup, reading Excel files with less or more than 1000 rows, exporting data with and without model mapping, handling multiple sheets, and includes full code examples for each operation.
The article introduces Alibaba's EasyExcel library as a high‑performance solution for Excel processing in Java backend applications.
Environment Setup : It lists the required Maven dependencies for easyexcel (mandatory) and optional lombok, showing the XML snippet for inclusion in pom.xml.
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>1.1.2-beat1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>Reading Excel Files (less than 1000 rows) : Demonstrates default and sheet‑specific reads using ExcelUtil.readLessThan1000Row(filePath) and ExcelUtil.readLessThan1000RowBySheet(filePath, sheet). Example code shows how to specify the file path and optional Sheet object.
String filePath = "/home/chenmingjian/Downloads/学生表.xlsx";
List<Object> objects = ExcelUtil.readLessThan1000Row(filePath);
Sheet sheet = new Sheet(1, 1); // sheet index, start row
List<Object> objects = ExcelUtil.readLessThan1000RowBySheet(filePath, sheet);Reading Excel Files (more than 1000 rows) : Uses a streaming approach with ExcelUtil.readMoreThan1000Row and ExcelUtil.readMoreThan1000RowBySheet. A custom ExcelListener class implements AnalysisEventListener to collect rows.
String filePath = "/home/chenmingjian/Downloads/学生表.xlsx";
List<Object> objects = ExcelUtil.readMoreThan1000Row(filePath);
Sheet sheet = new Sheet(1, 2);
List<Object> objects = ExcelUtil.readMoreThan1000RowBySheet(filePath, sheet);Exporting Excel – Single Sheet : Shows two ways to write data. (1) Simple export without model mapping using writeBySimple with a list of rows and a header list. (2) Export with model mapping using a POJO annotated with @ExcelProperty and writeWithTemplate.
// Simple export
String filePath = "/home/chenmingjian/Downloads/测试.xlsx";
List<List<Object>> data = new ArrayList<>();
data.add(Arrays.asList("111", "222", "333"));
List<String> head = Arrays.asList("表头1", "表头2", "表头3");
ExcelUtil.writeBySimple(filePath, data, head);
// Model‑based export
ArrayList<TableHeaderExcelProperty> data = new ArrayList<>();
for (int i = 0; i < 4; i++) {
TableHeaderExcelProperty p = new TableHeaderExcelProperty();
p.setName("cmj" + i);
p.setAge(22 + i);
p.setSchool("清华大学" + i);
data.add(p);
}
ExcelUtil.writeWithTemplate(filePath, data);Exporting Excel – Multiple Sheets : Defines a model class and uses ExcelUtil.writeWithMultipleSheel to generate a workbook containing several sheets, each with its own data list and Sheet configuration.
ArrayList<ExcelUtil.MultipleSheelPropety> list1 = new ArrayList<>();
for (int j = 1; j < 4; j++) {
ArrayList<TableHeaderExcelProperty> list = new ArrayList<>();
// fill list with model objects …
Sheet sheet = new Sheet(j, 0);
sheet.setSheetName("sheet" + j);
ExcelUtil.MultipleSheelPropety prop = new ExcelUtil.MultipleSheelPropety();
prop.setData(list);
prop.setSheet(sheet);
list1.add(prop);
}
ExcelUtil.writeWithMultipleSheel("/home/chenmingjian/Downloads/aaa.xlsx", list1);Utility Class – ExcelUtil : Provides static methods for reading ( readLessThan1000Row, readMoreThan1000Row), writing ( writeBySimple, writeWithTemplate, writeWithMultipleSheel), and internal helpers such as sheet initialization, error handling, and resource cleanup.
Test Class : Contains JUnit test methods that demonstrate each read/write scenario, ensuring the utility works as expected in a Spring Boot context.
Overall, the guide serves as a ready‑to‑use reference for developers who need fast, memory‑efficient Excel I/O in backend Java applications.
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.
