FastExcel: High‑Performance Java Library for Excel Read/Write – Features, Usage, and Comparison with EasyExcel
FastExcel is a Java library that builds on EasyExcel to provide higher performance, low‑memory streaming, and additional features such as PDF conversion, offering simple APIs, full compatibility, and detailed code examples for creating entity classes, listeners, and read/write operations.
Introduction
FastExcel is an upgraded framework released by the original EasyExcel author after Alibaba stopped maintaining EasyExcel. It inherits all advantages of EasyExcel while delivering significant performance and functional enhancements.
Features of FastExcel
High‑performance read/write: Optimized for handling massive Excel data with markedly reduced memory consumption.
Simple and easy to use: Provides a concise, intuitive API that lets developers quickly integrate Excel operations of any complexity.
Streaming operations: Supports streaming reads, minimizing the need to load large datasets into memory, suitable for hundreds of thousands to millions of rows.
Full compatibility: Completely compatible with all EasyExcel functionalities, enabling seamless migration.
Continuous updates: Ongoing bug fixes, performance tweaks, and new feature additions.
Detailed Usage of FastExcel
Creating Entity Classes and Listeners
Before reading or writing Excel files, define an entity class where each field maps to a column. Use the @ExcelProperty annotation to specify column names.
import cn.idev.excel.annotation.ExcelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Setter
@Getter
@ToString
public class User {
@ExcelProperty("编号")
private Integer id;
@ExcelProperty("名字")
private String name;
@ExcelProperty("年龄")
private Integer age;
}FastExcel processes Excel rows via event listeners, which helps avoid memory overflow for large files. Below is a sample listener that collects each row into a list and prints a summary after all rows are read.
import cn.idev.excel.context.AnalysisContext;
import cn.idev.excel.event.AnalysisEventListener;
import java.util.ArrayList;
import java.util.List;
public class BaseExcelListener
extends AnalysisEventListener
{
private List
dataList = new ArrayList<>();
@Override
public void invoke(T t, AnalysisContext analysisContext) {
dataList.add(t);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
System.out.println("读取完成,共读取了 " + dataList.size() + " 条数据");
}
public List
getDataList() {
return dataList;
}
}Implementing Write and Read Functions
Excel write functionality
The following example demonstrates writing Excel data using FastExcel.write . It creates test data, then streams it to the HTTP response.
// Excel write example
@GetMapping("/download")
public void download(HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("test", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
// Write data
FastExcel.write(response.getOutputStream(), User.class)
.sheet("模板")
.doWrite(buildData());
}
// Create test data
private List
buildData() {
User user1 = new User();
user1.setId(1);
user1.setName("张三");
user1.setAge(18);
User user2 = new User();
user2.setId(2);
user2.setName("李四");
user2.setAge(19);
return List.of(user1, user2);
}Excel read functionality
The example below shows how to read an uploaded Excel file using FastExcel.read together with the previously defined listener.
// Excel read example
@PostMapping("/upload")
public ResponseEntity
upload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return ResponseEntity.badRequest().body("请选择一个文件上传!");
}
try {
BaseExcelListener
baseExcelListener = new BaseExcelListener<>();
FastExcel.read(file.getInputStream(), User.class, baseExcelListener)
.sheet()
.doRead();
List
dataList = baseExcelListener.getDataList();
System.out.println(dataList);
return ResponseEntity.ok("文件上传并处理成功!");
} catch (IOException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件处理失败!");
}
}Excel to PDF Conversion
FastExcel also supports converting Excel files to PDF, leveraging Apache POI and itext‑pdf. Ensure compliance with the itext‑pdf license when using this feature.
FastExcel.convertToPdf(new File("excelFile"), new File("pdfFile"), null, null);Summary
FastExcel is an efficient and easy‑to‑use Excel processing tool that inherits all advantages of EasyExcel while enhancing performance and adding new capabilities.
Through the provided examples, we see how FastExcel simplifies Excel read/write operations and how event listeners enable streaming processing to manage memory effectively. Whether for enterprise data import/export or personal projects, FastExcel offers strong support.
Differences Between FastExcel and EasyExcel
Performance improvement: FastExcel delivers better and more stable performance than EasyExcel.
API consistency: The APIs are identical, allowing seamless switching.
Additional features: Version 1.0.0 adds the ability to read a specific number of rows and convert Excel to PDF.
Conclusion
FastExcel is a lightweight yet powerful Java library designed for high‑performance, low‑memory Excel file handling. For projects that need to process large‑scale Excel data, FastExcel is a compelling choice, offering streaming processing and a flexible API that make it an ideal tool for Excel manipulation.
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
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.