Export"
m (→Style) |
|||
Line 7: | Line 7: | ||
Given a Pivottable instance, you can dump the current result in .xls format to an OutputStream. The exported Excel file contains a sheet that reflects the computed result you see on browser windows, without paging. For example, | Given a Pivottable instance, you can dump the current result in .xls format to an OutputStream. The exported Excel file contains a sheet that reflects the computed result you see on browser windows, without paging. For example, | ||
+ | xls format | ||
<source lang="java"> | <source lang="java"> | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
Line 12: | Line 13: | ||
Exports.exportExcel(out, "xls", context, null); // writes Pivottable information to the output stream | Exports.exportExcel(out, "xls", context, null); // writes Pivottable information to the output stream | ||
Filedownload.save(out.toByteArray(), "application/vnd.ms-excel", "pivot.xls"); // file download | Filedownload.save(out.toByteArray(), "application/vnd.ms-excel", "pivot.xls"); // file download | ||
+ | try { | ||
+ | out.close(); | ||
+ | } catch (IOException e) {} | ||
+ | </source> | ||
+ | |||
+ | xlsx format | ||
+ | <source lang="java"> | ||
+ | ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
+ | PivotExportContext context = Exports.getExportContext(pivot, false, null); | ||
+ | Exports.exportExcel(out, "xlsx", context, null); // writes Pivottable information to the output stream | ||
+ | Filedownload.save(out.toByteArray(), "application/vnd.ms-excel", "pivot.xlsx"); // file download | ||
try { | try { | ||
out.close(); | out.close(); |
Revision as of 04:44, 31 May 2012
You can export the computed result of Pivottable to other formats.
Export to Microsoft Excel
Given a Pivottable instance, you can dump the current result in .xls format to an OutputStream. The exported Excel file contains a sheet that reflects the computed result you see on browser windows, without paging. For example,
xls format
ByteArrayOutputStream out = new ByteArrayOutputStream();
PivotExportContext context = Exports.getExportContext(pivot, false, null);
Exports.exportExcel(out, "xls", context, null); // writes Pivottable information to the output stream
Filedownload.save(out.toByteArray(), "application/vnd.ms-excel", "pivot.xls"); // file download
try {
out.close();
} catch (IOException e) {}
xlsx format
ByteArrayOutputStream out = new ByteArrayOutputStream();
PivotExportContext context = Exports.getExportContext(pivot, false, null);
Exports.exportExcel(out, "xlsx", context, null); // writes Pivottable information to the output stream
Filedownload.save(out.toByteArray(), "application/vnd.ms-excel", "pivot.xlsx"); // file download
try {
out.close();
} catch (IOException e) {}
Style
The export utility provides a chance to handle Excel style via POI API. You can specify custom styles by passing a CellStyleConfigurator. For example,
Exports.exportExcel(out, "xls", context, new CellStyleConfigurator() {
public void config(PivotExportCell.Type type, Cell cell, StyleFactory styleFactory) {
switch (type) {
// you can specify style by cell type, use StyleFactory to create new style
}
}
public String getDateFormat(String field) {
//specify date format by filed
return null;
}
});
PivotExportContext
PivotExportContext is an intermediate result for exporting Pivottable to a table/sheet data structure. You can construct it from a Pivottable, or from a PivotModel and a PivotRenderer (so you don't need a component instance).
// construct from a Pivottable
PivotExportContext context = Exports.getExportContext(pivot, open, titles);
// construct from a PivotModel and a PivotRenderer
// in this case, you also have to specify the data field orientation ("column" or "row")
PivotExportContext context = Exports.getExportContext(model, renderer, "column", open, titles);
There are two additional parameters in the API:
- boolean open: Expand all header tree nodes for the export, but the model itself it not changed.
- String[] titles: The text in the title cells (data title, column title, row title, respectively). See title cells definition.
Export to CSV
You can also export to CSV format in a similar manner. For example,
ByteArrayOutputStream out = new ByteArrayOutputStream();
PivotExportContext context = Exports.getExportContext(pivot, true, TITLES);
Exports.exportCSV(out, context);
Filedownload.save(out.toByteArray(), "text/csv", "pivot.csv");
try {
out.close();
} catch (IOException e) {}
Version History
Version | Date | Content |
---|---|---|