FastExcel: High‑Performance Java Excel Read/Write Library – Features, Usage, and Comparison with EasyExcel
FastExcel is a Java library that builds on EasyExcel to provide high‑performance, low‑memory Excel read/write, stream processing, PDF conversion and a simple API, with detailed usage examples, code snippets, and a comparison of its advantages over EasyExcel.
FastExcel is an upgraded Java framework released after the original EasyExcel project was discontinued by Alibaba, inheriting all of EasyExcel's advantages while delivering significant performance and feature enhancements.
Key Features
High‑performance read/write: Optimized for large‑scale Excel data with reduced memory consumption.
Simple API: Intuitive interfaces enable quick integration for both simple and complex Excel operations.
Streaming support: Stream‑based reading minimizes memory usage, suitable for handling hundreds of thousands to millions of rows.
Full compatibility: Seamlessly compatible with all EasyExcel functionalities.
Continuous updates: Ongoing bug fixes, performance tweaks, and new features.
Detailed Usage
Creating Entity Class and Listener
Define an entity class where each field maps to an Excel column using the @ExcelProperty annotation.
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;
}Implement an event listener to process rows one‑by‑one, preventing memory overflow.
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;
}
}Write Excel Example
Use FastExcel.write to export data to an Excel file.
// Excel写入功能
@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");
// 写入数据
FastExcel.write(response.getOutputStream(), User.class)
.sheet("模板")
.doWrite(buildData());
}
// 创建测试数据
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);
}Read Excel Example
Use FastExcel.read together with the previously defined listener to import data.
// Excel读取功能
@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 can convert Excel files to PDF using Apache POI and itext‑pdf (license compliance required).
FastExcel.convertToPdf(new File("excelFile"), new File("pdfFile"), null, null);FastExcel vs. EasyExcel
Performance boost: Faster and more stable than EasyExcel.
API compatibility: Identical API enables seamless migration.
Additional features: Supports reading specific rows and Excel‑to‑PDF conversion in version 1.0.0.
Conclusion
FastExcel is a lightweight yet powerful Java library designed for high‑performance, low‑memory Excel processing. Its streaming capabilities, flexible API, and added features such as PDF conversion make it an excellent choice for projects that need to handle large‑scale Excel data efficiently.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.