Newer
Older
report-generator / README.md

Report Generator

Annotate member variables or methods that will appear on the report

@Reportable(headerName = "Name", column = "A")
@ReportableMap
Map<String, Object> map

The map's key will be the headerName

Note: The map field must use a LinkedHashMap (or another ordered Map implementation) to guarantee deterministic column ordering. Using a HashMap will produce non-deterministic column order in the generated report.

Complex Map Values

Map values can be a boxed primitive or String (rendered as a single column, as above), or a complex object annotated with @Reportable members — in which case each entry expands into one column per @Reportable field/method on the value's class, and the map key becomes a single header spanning those columns.

public class User {
    @Reportable(headerName = "First Name", column = "A")
    private String firstName;

    @Reportable(headerName = "Last Name", column = "B")
    private String lastName;

    @Reportable(headerName = "Salary", column = "C")
    @NumberFormatter(format = "#,##0.00")
    private double salary;
}

public class Team {
    @Reportable(headerName = "Team", column = "A")
    private String teamName;

    @ReportableMap
    private LinkedHashMap<String, User> users;
}

Given a Team with users = {"Primary": User("Alice", "Smith", 500.5)}:

  • Excel: the header row is Team | Primary with Primary merged across the 3 generated columns, and the data row is <team name> | Alice | Smith | 500.50 — with Salary rendered as a formatted numeric cell via @NumberFormatter, exactly as it would be for a top-level @Reportable field.
  • CSV (no concept of merged cells): the key appears in the first of the generated columns and the rest are left blank.
Team,Primary,,
<team name>,Alice,Smith,500.50

Each nested member's own formatting annotations (@DateFormatter, @NumberFormatter, @StringFormatFormatter, @StringFormatter, @CurrencyFormatter, @BooleanFormatter, @NullFormatter, @CommaFormatter, @TrimFormatter) are applied the same way they would be on a top-level @Reportable field.

If a value's class has no @Reportable-annotated members (for example an unannotated object or a java.util.Date), the entry falls back to the original single-column behavior, rendered via toString().

Note: As with top-level fields, the columns generated for each map key are determined from the first data row's map. If a later row's map is missing an entry for that key, or the entry is null, the corresponding cells are simply left blank rather than shifting the remaining columns.

Annotate reportable member variables or methods with formatters (optional)

@DateFormatter(format = "yyyy-MM-dd")
@NumberFormatter(format = "#,##0.00")
@StringFormatFormatter(text = "Add %s text")
@StringFormatter(format = Format.UPPERCASE)
@CurrencyFormatter(format = Currency.USD)
@BooleanFormatter(falseText = "Not Valid", trueText = "Valid")
@NullFormatter(replacementText = "No Data")
@CommaFormatter
@TrimFormatter

Instantiate and generate

new CSVGenerator(List<?> data, char delimiter).generate(); // returns CSV String
new ExcelGenerator(List<?> data).generate(); // returns POI Workbook

Passing an empty list produces an empty report — no data rows are written. CSV and Excel output will contain headers only.

Auto Size

@AutoSize
@Reportable(headerName = "Name", column = "A")

AutoSize coupled with Reportable will set the Excel columns to auto-adjust with column width to fit content

Date Formatter

@DateFormatter(format = "yyyy-MM-dd")

Data types supported

  • java.util.Date

Format used by java.text.SimpleDateFormat

Number Formatter

@NumberFormatter(format = "#,##0.00")

Data types supported

  • Integer
  • Double
  • Float
  • Long

Format used by java.text.DecimalFormat

String Format Formatter

@StringFormatFormatter(text = "Add %s text")

Data types supported

  • String

String Formatter

@StringFormatter(format = Format.UPPERCASE)

Data types supported

  • String

Options

  • Format.UPPERCASE
  • Format.LOWERCASE
  • Format.CAPITALIZE
  • Format.CAPITALIZE_FULLY

Currency Formatter

@CurrencyFormatter(format = Currency.USD)

Data types supported

  • Integer
  • Double
  • Float
  • Long
  • Big Decimal

Options

  • Currency.USD

Boolean Formatter

@BooleanFormatter(falseText = "Not Valid", trueText = "Valid")

Arguments

  • falseText
  • trueText

Null Formatter

@NullFormatter(replacementText = "No Data")

Data ytpes supported

  • All Objects

Arguments

  • replacementText

Comma Formatter

@CommaFormatter

Data types supported

  • Integer
  • Double
  • Long

Trim Formatter

@TrimFormatter

Data types supported

  • String
@Hyperlink

Creates a hyperlink out of the cell data

Note: Only applies to ExcelGenerator