Boost Excel Processing in Spring Boot 3 with FastExcel – High‑Performance Read/Write Guide
This article introduces the FastExcel library as a high‑performance alternative to Apache POI for handling massive Excel files in Spring Boot 3, presents benchmark results, and provides detailed code examples for writing, reading, and uploading large spreadsheets with multithreading and memory‑efficient settings.
Introduction
When projects need to read or write extremely large Excel files for data analysis or reporting, the traditional Apache POI library consumes a lot of memory and processes slowly. Its streaming API mitigates some issues but still has limitations such as a sliding‑window mechanism, temporary file usage, and optional shared‑string handling that can increase heap usage.
The open‑source FastExcel component offers a simpler API, limited styling support, and high performance by directly streaming XML to the output stream while minimizing memory consumption. It also supports multithreaded sheet generation and full shared‑string and style handling.
Benchmark
Generating a workbook with one sheet, 100,000 rows and 4 columns shows that FastExcel is about 10× faster than Apache POI in non‑streaming mode and uses only 1/12 of the heap memory. POI’s streaming API approaches FastExcel’s speed but still suffers from the earlier mentioned constraints.
Practical Cases
Environment Setup
<properties>
<fastexcel.version>0.19.0</fastexcel.version>
</properties>
<dependency>
<groupId>org.dhatim</groupId>
<artifactId>fastexcel</artifactId>
<version>${fastexcel.version}</version>
</dependency>
<dependency>
<groupId>org.dhatim</groupId>
<artifactId>fastexcel-reader</artifactId>
<version>${fastexcel.version}</version>
</dependency>Write Excel File
Creating a workbook with a sheet and various cell types:
try (OutputStream os = new FileOutputStream("app.xlsx");
Workbook wb = new Workbook(os, "Spring全家桶实战案例", "1.0")) {
Worksheet ws = wb.newWorksheet("实战案例");
ws.value(0, 0, "Spring Boot3实战案例200讲");
ws.value(0, 1, LocalDate.now());
ws.value(0, 2, "Pack_xg");
ws.value(0, 3, 70D);
}Setting workbook properties:
wb.properties()
.setTitle("title property")
.setCategory("category property")
.setSubject("subject property")
.setKeywords("keywords property")
.setDescription("description property")
.setManager("manager property")
.setCompany("company property")
.setHyperlinkBase("hyperlinkBase property");Applying styles and formats, merging cells, conditional formatting, rotation, and global font settings are demonstrated with concise API calls such as ws.style(0,0).bold().fill(Fill.GRAY125).set(); and ws.style(0,0).format("yyyy-MM-dd H:mm:ss").set();.
Formulas and Hyperlinks
Formulas are added via ws.formula(row, col, "SUM(A1:A10)");. Hyperlinks can be inserted into single cells or ranges using
ws.hyperlink(0,0,new HyperLink("https://www.baidu.com","Baidu"));and
ws.range(1,0,1,1).setHyperlink(new HyperLink("./dev_soft/test.pdf","dev_folder"));.
Other Features
Set worksheet tab color.
Keep worksheet active.
Configure paper size and orientation.
Adjust margins.
Create frozen panes.
For a complete list, see the project repository.
Read Excel File
FastExcel’s reader module provides a streamlined API that reads only cell values, ignoring styles and charts. Example of reading rows:
try (InputStream is = new FileInputStream("app.xlsx");
ReadableWorkbook wb = new ReadableWorkbook(is)) {
Sheet sheet = wb.getFirstSheet();
try (Stream<Row> rows = sheet.openStream()) {
rows.forEach(r -> {
String c1 = r.getCellAsString(0).get();
LocalDateTime c2 = r.getCellAsDate(1).get();
String c3 = r.getCellAsString(2).get();
BigDecimal c4 = r.getCellAsNumber(3).get();
System.err.println(c1 + ", " + c2 + "," + c3 + ", " + c4);
});
}
}Reading all rows into a list:
List<Row> rows = sheet.read();Large File Write & Read
Writing one million rows demonstrates that FastExcel can generate the file in about 5.9 seconds:
try (OutputStream os = new FileOutputStream("app.xlsx");
Workbook wb = new Workbook(os, "App", "1.0")) {
Worksheet ws = wb.newWorksheet("测试");
StopWatch watch = new StopWatch();
watch.start();
// header
ws.value(0,0,"Column 1");
// ... other headers ...
for (int i = 1; i < 1_000_000; i++) {
String value = "data-" + i;
ws.value(i,0,i);
ws.value(i,1,value);
ws.value(i,2,value);
ws.value(i,3,value);
ws.value(i,4,value);
ws.value(i,5,value);
ws.value(i,6,value);
ws.value(i,7,value);
ws.value(i,8,value);
ws.value(i,9,value);
}
wb.finish();
watch.stop();
System.out.println("耗时 :: " + watch.getTime(TimeUnit.MILLISECONDS));
} catch (Exception e) { e.printStackTrace(); }Reading the same million‑row file completes in roughly 5 seconds:
try (InputStream is = new FileInputStream("app.xlsx");
ReadableWorkbook wb = new ReadableWorkbook(is)) {
StopWatch watch = new StopWatch();
watch.start();
wb.getSheets().forEach(sheet -> {
try (Stream<Row> rows = sheet.openStream()) {
rows.skip(1).forEach(r -> {
BigDecimal num = r.getCellAsNumber(0).orElse(null);
String str = r.getCellAsString(1).orElse(null);
// process
});
} catch (Exception e) { e.printStackTrace(); }
});
watch.stop();
System.out.println("耗时 :: " + watch.getTime(TimeUnit.MILLISECONDS));
}Upload Large Excel for Parsing
Example Spring MVC controller that parses an uploaded Excel file without loading the whole workbook into memory:
@PostMapping
public ResponseEntity<?> read(@RequestParam MultipartFile file) {
try (ReadableWorkbook wb = new ReadableWorkbook(file.getInputStream())) {
Sheet sheet = wb.getFirstSheet();
try (Stream<Row> rows = sheet.openStream()) {
rows.forEach(r -> {
String c2 = r.getCellAsString(1).get();
String c3 = r.getCellAsString(2).get();
System.err.println(c2 + "," + c3);
});
}
} catch (Exception e) { e.printStackTrace(); }
return ResponseEntity.ok("success");
}Performance charts illustrate memory usage during upload.
Conclusion
FastExcel provides a lightweight, high‑performance solution for both writing and reading massive Excel files in Spring Boot applications, dramatically reducing execution time and heap memory consumption compared with Apache POI. The library’s simple API, multithreading support, and flexible configuration make it suitable for large‑scale data processing scenarios.
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.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.
