FastExcel: High-Performance Java Excel Library Surpasses EasyExcel
This article introduces FastExcel, a high-performance Java Excel library created by the original EasyExcel author, featuring streaming read/write, full API compatibility, new PDF conversion, and code examples for entity mapping, event listeners, and large-file processing.
Introduction
FastExcel is a next-generation Excel processing framework released by the original EasyExcel author after Alibaba announced the discontinuation of EasyExcel maintenance. It inherits all EasyExcel advantages while delivering significant performance and functional improvements.
Key Features
High-performance read/write: Optimized for large-scale Excel data with minimal memory footprint.
Simple API: Clean, intuitive interfaces for quick integration, from basic operations to complex data processing.
Streaming operations: Supports streaming reads to avoid loading millions of rows into memory at once.
Full compatibility: 100% compatible with existing EasyExcel functionality for seamless migration.
Continuous updates: Ongoing bug fixes, performance optimizations, and new feature additions.
Usage Guide
Entity Class Definition
Define a POJO where each field maps to an Excel column using @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;
}Event Listener for Streaming Read
Implement AnalysisEventListener to process rows incrementally and avoid OOM:
import cn.idev.excel.context.AnalysisContext;
import cn.idev.excel.event.AnalysisEventListener;
import java.util.ArrayList;
import java.util.List;
public class BaseExcelListener<T> extends AnalysisEventListener<T> {
private List<T> 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<T> getDataList() {
return dataList;
}
}Excel Write Example
Write data to an HTTP response stream for download:
@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());
}
private List<User> 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 Example
Read uploaded file using the listener:
@PostMapping("/upload")
public ResponseEntity<String> upload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return ResponseEntity.badRequest().body("请选择一个文件上传!");
}
try {
BaseExcelListener<User> baseExcelListener = new BaseExcelListener<>();
FastExcel.read(file.getInputStream(), User.class, baseExcelListener)
.sheet()
.doRead();
List<User> 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
Convert Excel files to PDF using Apache POI and iText (ensure license compliance):
FastExcel.convertToPdf(
new File("excelFile"),
new File("pdfFile"),
null,
null
);Summary
FastExcel simplifies Excel read/write operations and enables memory-efficient streaming via event listeners. It suits both enterprise data import/export and personal projects.
FastExcel vs EasyExcel
Performance: FastExcel outperforms EasyExcel with better stability.
API consistency: Identical API allows zero-code migration.
New features: Version 1.0.0 adds reading specific row ranges and Excel-to-PDF conversion.
Conclusion
FastExcel is a lightweight yet powerful Java library designed for high-performance, low-memory Excel processing. Its streaming model and flexible API make it an ideal choice for large-scale Excel workloads.
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.
IoT Full-Stack Technology
Dedicated to sharing IoT cloud services, embedded systems, and mobile client technology, with no spam ads.
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.
