Export"

From Documentation
m
Line 50: Line 50:
  
 
==Export to CSV==
 
==Export to CSV==
 +
 +
You can also export to CSV format in a similar manner. For example,
 +
 +
<source lang="java">
 +
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) {}
 +
</source>
 +
 +
&nbsp;
  
 
==Version History==
 
==Version History==

Revision as of 06:50, 13 March 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,

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) {}

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, CellStyle style, Font font) {
		switch (type) {
			// you can specify style by cell type
		}
	}
});

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

Last Update : 2012/03/13


Version Date Content
     



Last Update : 2012/03/13

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.