Values and types
Value objects and money
Section titled “Value objects and money”public record Money(BigDecimal amount, Currency currency) { }
@Component // application-wide: every Money column, in every sheet classpublic class MoneyConverter implements CellConverter<Money> { @Override public CellValue convert(Money value, ConversionContext context) { return CellValue.number(value.amount().doubleValue()); }}
@ExcelSheetpublic record InvoiceRow( @ExcelColumn(header = "Invoice", order = 10) String number, @ExcelColumn(header = "Total", order = 20, format = "#,##0.00") Money total, @ExcelColumn(header = "Currency", order = 30) Currency currency) {}The Currency column needs no converter: it is written as its ISO code, such as EUR.
Dates and times with time zones
Section titled “Dates and times with time zones”@BeanCellConverter<Instant> instantConverter(@Value("${app.export.zone:Europe/Rome}") ZoneId zone) { return (value, context) -> CellValue.dateTime(LocalDateTime.ofInstant(value, zone));}@ExcelSheetpublic record AuditRow( @ExcelColumn(header = "At", order = 10, format = "dd/mm/yyyy hh:mm:ss") Instant at, @ExcelColumn(header = "User", order = 20) String user) {}The zone is an explicit application choice. Without the converter, AuditRow violates V-10.
Codes and identifiers as text
Section titled “Codes and identifiers as text”Postal codes, product codes, IBANs and long numeric identifiers must not become numbers: leading zeros would disappear and digits beyond the 15th would be lost.
@ExcelSheetpublic record AccountRow( @ExcelColumn(header = "Postal code", order = 10) String postalCode, // String: already text @ExcelColumn(header = "Reference", order = 20, converter = LongAsText.class) long reference) {}Keep such values as String in the sheet class whenever possible.
Links to the pages of another system
Section titled “Links to the pages of another system”A report lists orders, and each order number should open the page of the order in the web shop. The order number is a domain type; an application converter turns every column of that type into a link.
public record OrderNumber(String value) { }
@Componentpublic class OrderNumberConverter implements CellConverter<OrderNumber> {
@Override public CellValue convert(OrderNumber value, ConversionContext context) { String path = URLEncoder.encode(value.value(), StandardCharsets.UTF_8); return CellValue.link(Hyperlink.of("https://shop.example.com/orders/" + path, value.value())); }}
@ExcelSheet(title = "Orders to ship")public record ShipmentRow( @ExcelColumn(header = "Order", order = 10) OrderNumber order, @ExcelColumn(header = "Customer", order = 20) String customer, @ExcelColumn(header = "Ship by", order = 30, format = "dd/mm/yyyy") LocalDate shipBy) {}The cell shows the order number and opens its page. Encoding the number keeps the address valid whatever characters the number contains. When only one column should be a link, declare the converter on that field with @ExcelColumn(converter = OrderNumberConverter.class) and do not make it a bean (section 11.9).
E-mail links
Section titled “E-mail links”@ExcelSheet(title = "Suppliers")public record SupplierRow( @ExcelColumn(header = "Supplier", order = 10) String name, @ExcelColumn(header = "Contact", order = 20) Hyperlink contact) {
static SupplierRow of(String name, String email) { String subject = URLEncoder.encode("Order confirmation", StandardCharsets.UTF_8).replace("+", "%20"); return new SupplierRow(name, Hyperlink.of("mailto:" + email + "?subject=" + subject, email)); }}Clicking the cell opens the mail client with the recipient and the subject filled in. The cell shows the address. URLEncoder encodes spaces as +, which mail clients show literally in a subject: replace them with %20.