
JXLS – Generating Excel files easily
Problem
You want to generate Excel files in your desired format from the Java backend without being or becoming an Apache POI expert?
Solution
Using the Java library JXLS, Excel files with a special markup can be used as a template to generate Excel files with data from the backend in a comparatively simple way.
Example
interface RevenueDetails {
String getCustomerId();
BigDecimal getRevenue();
int getOrderCount();
};
The template for the desired format, including the JXLS markup, is generated in Excel.

final Iterable<RevenueDetails> revenueDetails = getRevenueDetails();
try (InputStream is = JxlsExcelWriter.class.getResourceAsStream("template.xlsx")) {
try (OutputStream os = new FileOutputStream("target/revenue-details.xlsx")) {
final var context = new Context();
context.putVar("entries", revenueDetails);
JxlsHelper.getInstance().processTemplate(is, os, context);
}
}


Further Aspects
- JXLS: http://jxls.sourceforge.net/index.html
- Apache POI: https://poi.apache.org/
---
Author: Jan Wolter / Senior Software Engineer / Office Leipzig
Download Toilet Paper #153: JXLS – Generating Excel files easily (PDF)
Want to write the next ToiletPaper? Apply at jambit!