Backend Development 9 min read

Universal Java Class-to-Excel Export Using Reflection and CSV Formatting

This article explains how to build a generic Java utility that exports any list of objects to an Excel‑compatible CSV file by using reflection to extract field names for headers and values for rows, includes Maven dependencies, custom annotations, and practical usage examples.

Architect's Guide
Architect's Guide
Architect's Guide
Universal Java Class-to-Excel Export Using Reflection and CSV Formatting

The article demonstrates how to create a universal Excel export utility in Java that works with any class type by leveraging CSV formatting and reflection.

Core idea: Convert a list of unknown objects into a CSV file by extracting field names for headers and values for rows, using reflection to build maps.

Dependencies (Maven):

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.15</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.15</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.69</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>

Utility class MyCsvFileUtil.java provides methods to create CSV files, write files, build file names, generate header rows, build content rows, and convert objects to maps via reflection:

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import java.io.*;
import java.lang.reflect.*;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
public class MyCsvFileUtil {
    public static final String FILE_SUFFIX = ".csv";
    public static final String CSV_DELIMITER = ",";
    public static final String CSV_TAIL = "\r\n";
    protected static final String DATE_STR_FILE_NAME = "yyyyMMddHHmmssSSS";

    public static void createCsvFile(String savePath, String contextStr) throws IOException { /* ... */ }
    public static void writeFile(String fileName, String content) { /* ... */ }
    public static String buildCsvFileFileName(List dataList) { /* ... */ }
    public static String buildCsvFileTableNames(List dataList) { /* ... */ }
    public static String buildCsvFileBodyMap(List dataLists) { /* ... */ }
    public static
Map
toMap(T entity) { /* ... */ }
}

Usage example: a controller method obtains a list of District objects, generates a file name, writes the header and body using the utility, and saves the CSV file.

@RequestMapping("/createCsvFileJcTest")
public void createCsvFileJcTest() {
    List
districts = districtService.queryByParentCodes(Arrays.asList("110100"));
    String fileName = "D:\\mycsv\\" + MyCsvFileUtil.buildCsvFileFileName(districts);
    String tableNames = MyCsvFileUtil.buildCsvFileTableNames(districts);
    MyCsvFileUtil.writeFile(fileName, tableNames);
    String contentBody = MyCsvFileUtil.buildCsvFileBodyMap(districts);
    MyCsvFileUtil.writeFile(fileName, contentBody);
}

Extension: a custom annotation @JcExcelName allows specifying human‑readable column titles, and helper methods resolve these titles to build customized headers.

@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface JcExcelName {
    String name() default "";
}

Additional helper methods resolveExcelTableName and buildCsvFileTableNamesNew demonstrate how to extract annotation values and construct the final header row.

Testing code shows the utility working with different classes, producing correctly formatted CSV/Excel files, and confirms the approach is simple yet powerful for generic data export scenarios.

JavaBackend DevelopmentReflectionMavenannotationsCSVExcel Export
Architect's Guide
Written by

Architect's Guide

Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.

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.