FastExcel: High-Performance Java Excel Library Surpassing EasyExcel
This article introduces FastExcel, a high-performance Java library for Excel processing that succeeds EasyExcel, detailing its streaming API, entity mapping, read/write code examples, PDF conversion, and performance advantages over its predecessor.
01 Introduction
FastExcel is an upgraded framework released by the original EasyExcel author after Alibaba announced the discontinuation of EasyExcel maintenance. It inherits all advantages of EasyExcel while delivering significant performance and functional improvements.
02 FastExcel Features
High-performance read/write: Optimized for efficient handling of large-scale Excel data with significantly reduced memory footprint.
Simple and intuitive API: Enables quick integration for both simple and complex Excel operations.
Streaming operations: Supports streaming reads to minimize one-time data loading, ideal for hundreds of thousands or millions of rows.
Full compatibility: Fully compatible with all EasyExcel features, allowing seamless migration.
Continuous updates: Ongoing bug fixes, performance optimizations, and new feature additions.
03 FastExcel Usage Guide
Create Entity Class and Listener
Entity Class
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;
}Event Listener
FastExcel uses event listeners for row-by-row reading, crucial for large files to avoid memory overflow. The listener collects rows and provides a callback after all data is read.
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;
}
}Implement Write and Read
Excel Write
Example of writing data to an Excel file via a Spring MVC endpoint. The response is configured for .xlsx download, and FastExcel.write streams the data.
// 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<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 of reading an uploaded Excel file using FastExcel.read with the previously defined listener.
// Excel读取功能
@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
FastExcel supports converting Excel files to PDF, relying on Apache POI and itext-pdf. Note that itext-pdf license compliance must be ensured.
FastExcel.convertToPdf(
new File("excelFile"),
new File("pdfFile"),
null,
null
);04 Summary
FastExcel is an efficient, easy-to-use Excel processing tool that not only inherits all EasyExcel strengths but also enhances performance and functionality. The examples demonstrate how FastExcel simplifies read/write operations and leverages event listeners for streaming processing to manage memory effectively. It provides strong support for enterprise data import/export and personal projects alike.
05 FastExcel vs EasyExcel Differences
Performance improvement: FastExcel outperforms EasyExcel in both speed and stability.
API consistency: APIs are identical, enabling zero-code migration.
New features: Version 1.0.0 adds reading specified row ranges and Excel-to-PDF conversion.
06 Conclusion
FastExcel is a lightweight yet powerful Java library designed for high-performance, low-memory Excel file processing. For projects handling large-scale Excel data, FastExcel is a compelling choice. Its streaming processing and flexible API make it an ideal tool for Excel file manipulation.
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.
