Java Excel Import/Export Utility with Custom Annotations
This article explains how to create a reusable Java utility for Excel import and export using custom annotations, detailing the required EnableExport, EnableExportField, and ImportIndex annotations, the implementation of parsing and exporting methods with Apache POI, and providing sample code and usage examples.
In many backend projects, frequent Excel import/export tasks can be streamlined by building a generic utility. The author introduces three custom annotations— EnableExport , EnableExportField , and ImportIndex —that mark which classes and fields participate in the process and allow configuration of column names, widths, and import indices.
The import method receives an Excel File and a target Class , reads the first sheet with Apache POI, iterates rows starting from the third line, extracts cell values as strings, and uses Alibaba FastJSON's TypeUtils.cast to convert each value to the appropriate field type based on the ImportIndex annotation. Each populated object is added to a result list which is returned to the caller.
public static List
parseExcelToList(File excel, Class clazz) { ... }The export method accepts a List of objects, a Class , and an HttpServletResponse . It creates a new workbook, builds a sheet, sets row heights and column widths, writes a merged title row, creates a header row using the field annotations, fills data rows, adds optional drop‑down validation, and finally writes the workbook to the HTTP response.
public static void exportExcel(List
data, Class
clazz, HttpServletResponse response) { ... }Supporting helper methods are provided to generate basic cell styles, title cell styles, merged title rows, header rows, and data‑validation (drop‑down) lists. These methods encapsulate styling details such as borders, alignment, background colors, and merged regions.
private static HSSFCellStyle getBasicCellStyle(HSSFWorkbook workbook) { ... }
private static HSSFCellStyle getTitleCellStyle(HSSFWorkbook workbook) { ... }
private static void createTitle(HSSFWorkbook workbook, HSSFRow row, HSSFCell cell, HSSFSheet sheet, int colCount, String title) { ... }
private static void createHeadRow(HSSFWorkbook workbook, HSSFRow row, HSSFCell cell, HSSFSheet sheet, List
colNames) { ... }
public static void createDataValidation(Sheet sheet, Map
selectListMap) { ... }Usage examples show how to annotate a POJO, call the import method to obtain a List of objects, and call the export method to generate an Excel file with a single line of code. The source code is hosted on GitHub at https://github.com/xyz0101/excelutils .
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.