Sheet templates
Sheet templates
Section titled “Sheet templates”The sheet classes of section 10.3 and section 10.6 still repeat the title style, the preset, the accent colour, the style sheets, the slots and the auto-filter. With many reports, those lines are copied on every class, and nothing guarantees that every report keeps them in sync.
A sheet template is a class annotated with @ExcelSheetTemplate that declares that configuration once. Sheet classes reference it with @ExcelSheet(template = ...) and take from it every attribute they do not set themselves.
| A template can set | A template cannot set |
|---|---|
preset, accentColor, styleSheets, titleStyle, freezeHeader, autoFilter, autoSizeColumns, outerBorder, outerBorderColor, header, body |
the title, which is the content of a single report; columns; named styles, which belong to style sheets |
The attributes of @ExcelSheetTemplate have the same names, types and meaning as those of @ExcelSheet, so moving configuration from a sheet class to a template is a matter of copy and paste. The complete reference is in section 6.6.
Declaring a template
Section titled “Declaring a template”A template is usually a final class with a private constructor, carrying only the annotation. It references the style sheets whose styles it places in the slots:
@ExcelSheetTemplate( preset = TablePreset.LIGHT, accentColor = "#0B3D5C", styleSheets = CorporateStyles.class, titleStyle = "corp-title", autoFilter = Toggle.TRUE, outerBorder = Border.THIN, outerBorderColor = "#0B3D5C", header = @HeaderStyles(base = "corp-header"), body = @BodyStyles(base = "corp-cell", lastRow = "corp-total"))public final class CorporateReport { private CorporateReport() { }}freezeHeader and autoSizeColumns are not set here: the sheets that use the template keep the defaults, a frozen header and automatic column sizing.
Using a template
Section titled “Using a template”A sheet class references the template and declares its own title and columns. The column slots still use the styles of the template style sheets by name:
@ExcelSheet(title = "Monthly orders", template = CorporateReport.class)public record OrderRow( @ExcelColumn(header = "Order", order = 10) String number, @ExcelColumn(header = "Date", order = 20, styles = @ColumnStyles(base = "corp-date")) LocalDate date, @ExcelColumn(header = "Amount", order = 30, styles = @ColumnStyles(base = "corp-money")) BigDecimal amount) {}
@ExcelSheet(title = "Supplier balances", template = CorporateReport.class)public record SupplierBalanceRow( @ExcelColumn(header = "Supplier", order = 10, width = 40) String supplier, @ExcelColumn(header = "Balance", order = 20, styles = @ColumnStyles(base = "corp-money")) BigDecimal balance) {}Both reports get the corporate title, the LIGHT preset in the corporate blue, the corporate header and cells, the auto-filter, the thin frame and the bold total on the last row.
A sheet class can still decide for itself. Here a very large export switches off automatic sizing and adds the zebra on odd rows, and keeps everything else from the template:
@ExcelSheet(title = "Movements", template = CorporateReport.class, autoSizeColumns = Toggle.FALSE, body = @BodyStyles(odd = "corp-zebra"))public record MovementRow( @ExcelColumn(header = "Id", order = 10, width = 12) long id, @ExcelColumn(header = "Date", order = 20, width = 12, styles = @ColumnStyles(base = "corp-date")) LocalDate date, @ExcelColumn(header = "Amount", order = 30, width = 14, styles = @ColumnStyles(base = "corp-money")) BigDecimal amount) {}The slot body.odd is set on the sheet class; body.base and body.lastRow still come from the template.
What a sheet class takes from its template
Section titled “What a sheet class takes from its template”For every attribute, the sheet class is considered first, then its template, then the defaults. An attribute left unset, that is INHERIT or empty, passes to the next level.
| Attribute | Rule |
|---|---|
preset |
Only the template chooses it. A sheet class that references a template must leave preset unset (V-22). A template that leaves it unset uses the application default preset. |
accentColor |
The sheet class can override it: the same template then works in different colours. When neither sets it, the application default accent colour applies. |
styleSheets |
Both lists apply, at different levels: see section 10.11. |
titleStyle |
The sheet class can override it. The title style of a template is ignored by sheet classes without a title, without error, so the same template serves sheets with and without a title. |
freezeHeader, autoFilter, autoSizeColumns |
The sheet class can override them, in both directions, with Toggle.TRUE or Toggle.FALSE. When neither sets them, the header is frozen, no auto-filter is added and columns are sized automatically. |
outerBorder, outerBorderColor |
Each one separately: a sheet class can change the line of the frame and keep the colour of the template, or the opposite. |
header, body |
Slot by slot: a slot set on the sheet class wins over the same slot of the template, the other slots keep the style named by the template. |
The merge happens once, when the metadata of the sheet class is built. The cascade of section 5.9 then works on the merged configuration, exactly as if every attribute had been declared on the sheet class.
Two consequences are worth knowing:
- The colour of the frame does not follow the accent. A frame colour declared in the template stays the same when a sheet class changes the accent colour, because each attribute is taken separately. A template meant to be used with different accents should leave
outerBorderColorunset, or each sheet class should set its own. - Named styles merge with the preset attribute by attribute. A style that sets a border line without its colour takes the colour that the preset gives to that border, if any: the same template style can therefore look slightly different under different presets. Set the colour in the style when it matters.
Styles available to a sheet class with a template
Section titled “Styles available to a sheet class with a template”The named styles that a sheet class can use, in its own slots, in the slots it takes from its template and in its columns, come from three levels, from the strongest to the weakest:
- the styles declared on the sheet class with
@ExcelStyle; - the styles of the style sheets of the sheet class (
@ExcelSheet.styleSheets); - the styles of the style sheets of the template (
@ExcelSheetTemplate.styleSheets).
A style found at a stronger level replaces entirely a style with the same name from a weaker level. This allows a single report to restyle a slot of the template without touching the template: declaring a style with the same name is enough.
// The total of this report is red: the style "corp-total" of the class// replaces the one of CorporateStyles, also in the slot body.lastRow of the template.@ExcelSheet(title = "Overdue invoices", template = CorporateReport.class)@ExcelStyle(name = "corp-total", bold = Toggle.TRUE, fontColor = "#C00000", borderTop = Border.DOUBLE)public record OverdueInvoiceRow( @ExcelColumn(header = "Invoice", order = 10) String number, @ExcelColumn(header = "Amount", order = 20, styles = @ColumnStyles(base = "corp-money")) BigDecimal amount) {}Rule V-08 applies only between the style sheets of the same level. A style sheet referenced by both the template and the sheet class is not a conflict.
Template rules
Section titled “Template rules”A template is shared by many sheet classes, so it must be valid on its own, whatever the sheet classes that use it. Its errors name the template class, with @ExcelSheetTemplate as element.
| Rule | Consequence |
|---|---|
A sheet class can reference only a class annotated with @ExcelSheetTemplate (V-21). |
A forgotten annotation is reported on the sheet class. |
A sheet class that references a template leaves preset unset (V-22). |
The preset belongs to the template. To use another preset, declare another template. |
| A template is not also a sheet class or a style sheet (V-23). | Templates, sheet classes and style sheets are separate classes. |
A template declares no @ExcelStyle (V-24). |
Shared styles belong to style sheets, which the template references. A style declared on a template is ignored, so a slot that uses it also reports V-06: moving the style to a style sheet fixes both. |
| A template has no column, inherited ones included (V-25). | Columns belong to the sheet classes. |
| Every style named by the template exists in its own style sheets (V-06). | A template cannot rely on styles that some sheet classes happen to declare: even if a sheet class defines the missing style, the template is reported. |
| The style sheets of the template are valid, the colours of the template are valid (V-07 to V-09, V-13). | Reported on the template or on its style sheets. |
| A template cannot reference another template. | There is no template attribute on @ExcelSheetTemplate: each sheet class references one template at most. |
The errors of an invalid template are reported together with those of every sheet class that uses it, once each, without further errors on the sheet classes caused by the template. Sheetsmith.validate also accepts a template class and validates it on its own, and the startup validation of the Spring Boot integration validates every template found in the scanned packages, even when no sheet class references it (section 12.4).
Style sheet or template?
Section titled “Style sheet or template?”| Need | Use |
|---|---|
| Share what a cell looks like: fonts, fills, borders, formats | a style sheet |
| Share how a whole sheet is set up: preset, accent, slots, filter, frame, title style | a template, referencing one or more style sheets |
| Use corporate styles in a report with an unusual layout | the style sheet alone, without the template |
| Give one report a different colour, without changing anything else | the template, with accentColor on the sheet class |
| Give one report a different preset | another template |
A style sheet can be used by any number of templates and sheet classes: the same CorporateStyles can serve a CorporateReport template, a CorporateListing template with a different layout, and a few sheet classes with no template at all.
Distributing the corporate style
Section titled “Distributing the corporate style”Style sheets and templates are ordinary classes. To share them across applications, place them, together with a recommended SheetsmithDefaults and DocumentProperties, in a small internal library on which every application depends:
public final class CorporateSheetsmith {
public static final SheetsmithDefaults DEFAULTS = new SheetsmithDefaults("dd/mm/yyyy", "dd/mm/yyyy hh:mm", "#,##0.00", TablePreset.LIGHT, "#0B3D5C");
public static final DocumentProperties DOCUMENT = new DocumentProperties("Example Ltd", "Example Reporting");
public static Sheetsmith.Builder builder() { return Sheetsmith.builder().defaults(DEFAULTS).documentProperties(DOCUMENT); }
private CorporateSheetsmith() { }}The library then contains CorporateStyles, CorporateReport and CorporateSheetsmith, and every application writes only its sheet classes. In Spring Boot applications, the defaults and the document properties go in a shared application.yml fragment or profile (see section 12.3).
A shared template is a visual contract. Changing a template, or the styles of its style sheets, changes every sheet that uses it, in every application that upgrades the library, without any change to their code. Sometimes that is exactly the goal, as with a new corporate colour; often it is not. Treat a shared template as an API:
- change it only when every report that uses it must change;
- when only some reports must look different, publish a new template next to the existing one, instead of modifying it;
- record visible changes in the release notes of the shared library, so that the applications know what will change when they upgrade.