Boost Java Excel Performance with FastExcel: Features, Usage, and Comparison
This article introduces FastExcel, an upgraded Java library for high‑performance Excel read/write, outlines its key features, provides step‑by‑step code examples for entity creation, event listeners, writing, reading, PDF conversion, compares it with EasyExcel, and concludes with its suitability for large‑scale data processing.
Introduction
FastExcel is an upgraded framework released by the original EasyExcel author after Alibaba stopped maintaining EasyExcel. It inherits all advantages and significantly improves performance and functionality.
Features of FastExcel
High-performance read/write : optimized for large‑scale Excel data with low memory usage.
Simple to use : concise API for quick integration.
Streaming operations : supports streaming reads, suitable for hundreds of thousands to millions of rows.
Full compatibility : compatible with all EasyExcel features.
Continuous updates : ongoing bug fixes and enhancements.
Detailed Usage
Creating Entity Class and Listener
Entity Class
Define an entity class where each field maps to an Excel column, using the
@ExcelPropertyannotation to specify column names.
<code>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;
}
</code>Event Listener
FastExcel uses an event listener for row‑by‑row reading, preventing memory overflow.
<code>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;
}
}
</code>Read and Write Operations
Excel Write
Example of writing data to an Excel file.
<code>// Excel write
@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);
}
</code>Excel Read
Example of reading an Excel file using the previously defined listener.
<code>// Excel read
@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("文件处理失败!");
}
}
</code>Excel to PDF Conversion
FastExcel can convert Excel files to PDF using Apache POI and iText PDF (license compliance required).
<code>FastExcel.convertToPdf(new File("excelFile"), new File("pdfFile"), null, null);
</code>Comparison with EasyExcel
Performance improvement : Faster and more stable.
API consistency : Fully compatible API for seamless migration.
Additional features : Supports reading specific rows and Excel‑to‑PDF conversion.
Conclusion
FastExcel is a lightweight yet powerful Java library designed for high‑performance, low‑memory Excel processing. Its streaming capabilities and flexible API make it an ideal choice for projects that need to handle large Excel datasets efficiently.
Project URL
https://github.com/CodePhiliaX/fastexcel
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.