Backend Development 13 min read

Java Excel Export Utility: Design, Implementation, and Usage with Apache POI

This article explains how to build a flexible Java utility for exporting large volumes of report data to Excel using Apache POI, covering customizable headers, field formatting, multithreaded data retrieval, asynchronous processing, and deployment steps.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Java Excel Export Utility: Design, Implementation, and Usage with Apache POI

The article introduces a need to export over a hundred reports in a company project and presents a reusable Java utility based on Apache POI for generating Excel files.

Key features include exporting arbitrary data types, freely setting table headers, and customizing field formats.

A usage example demonstrates setting the data list, header information, and format map, with an @Override public void export(HttpServletResponse response, String fileName) { ... } method that creates an ExcelUtils instance and streams the workbook to the client.

The core ExcelUtils class contains three member variables: private List list; , private List<ExcelHeaderInfo> excelHeaderInfos; , and private Map<String, ExcelFormat> formatInfo; . The ExcelHeaderInfo constructor ExcelHeaderInfo(int firstRow, int lastRow, int firstCol, int lastCol, String title) defines merged header regions, and the ExcelFormat enum provides format constants such as FORMAT_INTEGER , FORMAT_DOUBLE , FORMAT_PERCENT , and FORMAT_DATE .

Header creation iterates over excelHeaderInfos , merges cells with sheet.addMergedRegion(new CellRangeAddress(...)) , creates cells at the first row/column, sets values and styles, and adjusts column widths.

Data transformation converts a list of objects to a two‑dimensional string array using reflection and BeanUtils.getProperty , enabling generic handling of any report entity.

Content creation writes each cell based on the format map, handling integers, doubles, percentages, and dates with appropriate POI cell types and styles.

For large data sets, the article explains a multithreaded approach using Callable and FutureTask to query chunks of data in parallel while preserving order by storing tasks in a list and retrieving results sequentially.

To avoid interface timeouts, it suggests asynchronous processing with Spring's @Async annotation, returning a file identifier immediately and providing separate endpoints for status checking and file download.

The article also provides a MySQL table definition for ttl_product_info and deployment steps: create the table, run the application, and access http://localhost:8080/api/excelUtils/export to download the generated Excel file.

backendJavaMultithreadingExceldata-exportApache POI
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.