Export
You can export the computed Pivottable result to Excel or CSV.
Export to Microsoft Excel
Starting with Pivottable 4.0.0, Excel export uses the Keikai 7 spreadsheet engine and requires the keikai-model and keikai-native dependencies. See Using Maven for dependency configuration.
If you are upgrading from an earlier version, see the Migration Guide for 4.0.0 for required dependency changes and updates to export customization code.
Create a PivotExportContext from the component and pass it to Exports.exportExcel(). The exported sheet contains the complete computed result without paging.
XLS Format
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
PivotExportContext context = Exports.getExportContext(pivot, false, null);
Exports.exportExcel(out, "xls", context, null);
Filedownload.save(out.toByteArray(), "application/vnd.ms-excel", "pivot.xls");
}
XLSX Format
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
PivotExportContext context = Exports.getExportContext(pivot, false, null);
Exports.exportExcel(out, "xlsx", context, null);
Filedownload.save(out.toByteArray(),
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"pivot.xlsx");
}
Customize Excel Styles
since 4.0.0
Pivottable 4.0.0 exposes Keikai model objects to custom style code. Implement CellStyleConfigurator with SCell instead of the former POI Cell type:
Exports.exportExcel(out, "xlsx", context, new CellStyleConfigurator() {
@Override
public void config(PivotExportCell.Type type, SCell cell,
StyleFactory styleFactory) {
// Customize the cell with the Keikai model API.
}
@Override
public String getDataFormat(String field) {
// Return a format pattern for the specified data field.
return null;
}
});
The overloads that accept an existing workbook now take io.keikai.model.SBook. Call SBook.destroy() after finishing the export when your application creates the book. See Export into an Existing Book for the migration example and resource lifecycle requirements.
Create an Export Context
PivotExportContext is an intermediate table or sheet representation. You can create it from a Pivottable component or directly from a PivotModel and PivotRenderer.
// Create from a Pivottable component.
PivotExportContext context = Exports.getExportContext(pivot, open, titles);
// Create without a component instance.
PivotExportContext context =
Exports.getExportContext(model, renderer, "column", open, titles);
The parameters control the exported result:
open: Expands all header tree nodes for the export without changing the model.titles: Specifies the data, column, and row title cells, in that order. See Title Cells.
Export to CSV
CSV export does not require the Keikai libraries:
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
PivotExportContext context = Exports.getExportContext(pivot, true, TITLES);
Exports.exportCSV(out, context);
Filedownload.save(out.toByteArray(), "text/csv", "pivot.csv");
}