toilet_paper_icon_153

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

You want to generate a sales overview with thefollowing backend details for Excel:
interface RevenueDetails {
    String getCustomerId();
    BigDecimal getRevenue();
    int getOrderCount();
};

The template for the desired format, including the JXLS markup, is generated in Excel.

TP#153 Bild 1 Excel
The following code then generates an Excel file from the backend data and adapts the template’s formulas to the rows:
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);
    }
}
TP#153 Bild 2 Resultat Excel

TP 153 Visual

Further Aspects

---

Author: Jan Wolter / Senior Software Engineer / Office Leipzig

Cookie Settings

This website uses cookies to personalize content and ads, provide social media features, and analyze website traffic. In addition, information about your use of the website is shared with social media, advertising, and analytics partners. These partners may merge the information with other data that you have provided to them or that they have collected from you using the services.

For more information, please refer to our privacy policy. There you can also change your cookie settings later on.

contact icon

Contact us now