Styling recipes
Totals row
Section titled “Totals row”sheetsmith does not compute totals or write formulas: the total is a data row like the others, usually the last element of the list, styled with the lastRow slot.
@ExcelSheet(body = @BodyStyles(lastRow = "total"))@ExcelStyle(name = "total", bold = Toggle.TRUE, borderTop = Border.DOUBLE)public record SalesRow( @ExcelColumn(header = "Region", order = 10) String region, @ExcelColumn(header = "Revenue", order = 20, format = "#,##0.00") BigDecimal revenue) {}
List<SalesRow> rows = new ArrayList<>(regions);rows.add(new SalesRow("Total", regions.stream().map(SalesRow::revenue).reduce(BigDecimal.ZERO, BigDecimal::add)));With a preset, the total row also receives the zebra of its parity; add fillPattern = Fill.NO_FILL or a fillColor to total to control it. When the auto-filter is enabled, remember that the total row is part of the filtered range.
Zebra rows without a preset
Section titled “Zebra rows without a preset”@ExcelSheet(header = @HeaderStyles(base = "header"), body = @BodyStyles(even = "zebra"))@ExcelStyle(name = "header", bold = Toggle.TRUE, borderBottom = Border.THIN)@ExcelStyle(name = "zebra", fillColor = "#F2F2F2")public record LogRow( @ExcelColumn(header = "When", order = 10, format = "dd/mm/yyyy hh:mm:ss") LocalDateTime when, @ExcelColumn(header = "Message", order = 20, width = 80) String message) {}Highlighting a key column and right-aligning numbers
Section titled “Highlighting a key column and right-aligning numbers”@ExcelSheet(preset = TablePreset.MEDIUM, body = @BodyStyles(firstColumn = "key"))@ExcelStyle(name = "key", bold = Toggle.TRUE)@ExcelStyle(name = "right", align = Align.RIGHT)public record StockRow( @ExcelColumn(header = "Item", order = 10) String item, @ExcelColumn(header = "On hand", order = 20, format = "#,##0", headerStyle = "right") int onHand, @ExcelColumn(header = "Reserved", order = 30, format = "#,##0", headerStyle = "right") int reserved) {}Numbers are right-aligned by Excel by default (GENERAL alignment); the right style aligns the header texts above them.
Framed table
Section titled “Framed table”@ExcelSheet(title = "Attendance", outerBorder = Border.MEDIUM, outerBorderColor = "#404040", body = @BodyStyles(base = "grid"))@ExcelStyle(name = "grid", border = Border.HAIR, borderColor = "#BFBFBF")public record AttendanceRow( @ExcelColumn(header = "Name", order = 10) String name, @ExcelColumn(header = "Present", order = 20) boolean present) {}The frame is drawn on the outer edges of header and data, over the hairline grid; the title stays outside.
Corporate report with a shared style sheet
Section titled “Corporate report with a shared style sheet”@ExcelStyleSheet@ExcelStyle(name = "corp-title", fontName = "Arial", fontSize = 16, bold = Toggle.TRUE, fontColor = "#0B3D5C")@ExcelStyle(name = "corp-header", fontName = "Arial", bold = Toggle.TRUE, wrapText = Toggle.TRUE, verticalAlign = VerticalAlign.CENTER)@ExcelStyle(name = "corp-cell", fontName = "Arial", fontSize = 10)@ExcelStyle(name = "corp-money", align = Align.RIGHT, dataFormat = "#,##0.00")@ExcelStyle(name = "corp-total", bold = Toggle.TRUE, borderTop = Border.DOUBLE, borderTopColor = "#0B3D5C")public final class CorporateStyles { private CorporateStyles() { }}
@ExcelSheet(title = "Supplier balances", titleStyle = "corp-title", preset = TablePreset.LIGHT, accentColor = "#0B3D5C", styleSheets = CorporateStyles.class, header = @HeaderStyles(base = "corp-header"), body = @BodyStyles(base = "corp-cell", lastRow = "corp-total"), autoFilter = Toggle.TRUE)public record SupplierBalanceRow( @ExcelColumn(header = "Supplier", order = 10, width = 40) String supplier, @ExcelColumn(header = "Balance", order = 20, styles = @ColumnStyles(base = "corp-money")) BigDecimal balance) {}Every report referencing CorporateStyles gets the same fonts and formats; the LIGHT preset with the corporate accent provides lines and zebra. When many reports repeat this configuration, declare it once in a template: see section 15.19.
Many reports with one corporate look
Section titled “Many reports with one corporate look”An application produces many reports, each with its own columns, all in the corporate style. Declare the styles once in a style sheet, the configuration once in a template, and keep the sheet classes down to their title and columns.
@ExcelStyleSheet@ExcelStyle(name = "corp-title", fontSize = 14, bold = Toggle.TRUE)@ExcelStyle(name = "corp-header", bold = Toggle.TRUE, align = Align.CENTER)@ExcelStyle(name = "corp-total", bold = Toggle.TRUE, borderTop = Border.DOUBLE)public final class CorporateStyles { private CorporateStyles() { }}
@ExcelSheetTemplate( preset = TablePreset.MEDIUM, accentColor = "#1F4E79", styleSheets = CorporateStyles.class, titleStyle = "corp-title", autoFilter = Toggle.TRUE, outerBorder = Border.MEDIUM, header = @HeaderStyles(base = "corp-header"), body = @BodyStyles(lastRow = "corp-total"))public final class CorporateReport { private CorporateReport() { }}
@ExcelSheet(title = "Invoices Q3", template = CorporateReport.class)public record InvoiceLine( @ExcelColumn(header = "Number", order = 10) String number, @ExcelColumn(header = "Issued", order = 20) LocalDate issued, @ExcelColumn(header = "Amount", order = 30, format = "#,##0.00") BigDecimal amount) {}
@ExcelSheet(title = "Purchases Q3", template = CorporateReport.class, accentColor = "#375623")public record PurchaseLine( @ExcelColumn(header = "Supplier", order = 10) String supplier, @ExcelColumn(header = "Ordered", order = 20) LocalDate ordered, @ExcelColumn(header = "Amount", order = 30, format = "#,##0.00") BigDecimal amount) {}Every report gets the MEDIUM preset, the bold centred header, the title in 14 points, the auto-filter, the medium frame and the total with a double line on the last row. The purchases report keeps all of it in green. The data list of each report ends with its total row, which takes the style of the slot lastRow. To share the look across applications, see section 10.14.