Export to Different File Format"
Line 4: | Line 4: | ||
===Purpose=== | ===Purpose=== | ||
Export ZK Spreadsheet to different file format (PDF, Html,CSV, Tab delimited and so on) | Export ZK Spreadsheet to different file format (PDF, Html,CSV, Tab delimited and so on) | ||
+ | |||
+ | ====Exporter interface==== | ||
+ | Exporting ZK Spreadsheet to another format is done through <javadoc>org.zkoss.zss.model.Exporter</javadoc> interface. It has three convenient methods to export ZK Spreadsheet contents. | ||
+ | :* Use Exporter#export(Book workbook, OutputStream outputStream) API to export entire workbook. | ||
+ | :* Use Exporter#export(Sheet worksheet, OutputStream outputStream); API to export specific worksheet. | ||
+ | :* Use Exporter#exportSelection(Sheet worksheet, AreaReference area, OutputStream outputStream) API to export partially selected area of workbook. | ||
+ | |||
===ZUML=== | ===ZUML=== | ||
Here is an example ZUML file that display an Excel book file and has three buttons to export entire workbook,selected sheet and specific selection. | Here is an example ZUML file that display an Excel book file and has three buttons to export entire workbook,selected sheet and specific selection. | ||
Line 12: | Line 19: | ||
<hbox> | <hbox> | ||
<button id="exportBtn" label="Export All"></button> | <button id="exportBtn" label="Export All"></button> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
</hbox> | </hbox> | ||
<spreadsheet id="spreadsheet" | <spreadsheet id="spreadsheet" | ||
Line 26: | Line 28: | ||
===Composer=== | ===Composer=== | ||
− | In composer there | + | In composer there is one event handler each for export button defined in above ZUML. To export entire ZK Spreadsheet workbook contents use Exporter#export(Book,OutputStream) API. You should be able to get Book instance either by importing Excel book file using Importer interface or using Spreadsheet#getBook() API if ZK Spreadsheet component is already initialized with an Excel book file. You should also prepare OutputStream to which new format should be written to. For example |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | To export entire ZK Spreadsheet workbook contents use Exporter#export(Book,OutputStream) API. You should be able to get Book instance either by importing Excel book file using Importer interface or using Spreadsheet#getBook() API if ZK Spreadsheet component is already initialized with an Excel book file. You should also prepare OutputStream to which new format should be written to. For example | ||
<source lang="java" high="8,9" start="5"> | <source lang="java" high="8,9" start="5"> | ||
Line 47: | Line 41: | ||
</source> | </source> | ||
− | Here I am using PdfExporter to export ZK Spreadsheet contents to PDF format. Similarly you could implement HtmlExporter,CSVExporter,TabDelimExporter and so on to export to respective formats. | + | Here I am using <javadoc>org.zkoss.zss.model.impl.PdfExporter</javadoc> to export ZK Spreadsheet contents to PDF format. Similarly you could implement HtmlExporter,CSVExporter,TabDelimExporter and so on to export to respective formats. |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
See the full source code for Composer here | See the full source code for Composer here | ||
Revision as of 09:53, 23 November 2010
Purpose
Export ZK Spreadsheet to different file format (PDF, Html,CSV, Tab delimited and so on)
Exporter interface
Exporting ZK Spreadsheet to another format is done through Exporter interface. It has three convenient methods to export ZK Spreadsheet contents.
- Use Exporter#export(Book workbook, OutputStream outputStream) API to export entire workbook.
- Use Exporter#export(Sheet worksheet, OutputStream outputStream); API to export specific worksheet.
- Use Exporter#exportSelection(Sheet worksheet, AreaReference area, OutputStream outputStream) API to export partially selected area of workbook.
ZUML
Here is an example ZUML file that display an Excel book file and has three buttons to export entire workbook,selected sheet and specific selection.
<?page title="ZSS Export to Different File Format" contentType="text/html;charset=UTF-8"?>
<window width="100" height="100%"
apply="org.zkoss.zssessentials.export.ExportComposer">
<hbox>
<button id="exportBtn" label="Export All"></button>
</hbox>
<spreadsheet id="spreadsheet"
src="/WEB-INF/excel/export/export.xlsx" maxrows="200" maxcolumns="40"
vflex="1" width="100%">
</spreadsheet>
</window>
Composer
In composer there is one event handler each for export button defined in above ZUML. To export entire ZK Spreadsheet workbook contents use Exporter#export(Book,OutputStream) API. You should be able to get Book instance either by importing Excel book file using Importer interface or using Spreadsheet#getBook() API if ZK Spreadsheet component is already initialized with an Excel book file. You should also prepare OutputStream to which new format should be written to. For example
public void onClick$exportBtn(Event evt) throws IOException {
Book wb = spreadsheet.getBook();
Exporter c = new PdfExporter();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
c.export(wb, baos);
Filedownload.save(baos.toByteArray(), "application/pdf",
wb.getBookName());
}
Here I am using PdfExporter to export ZK Spreadsheet contents to PDF format. Similarly you could implement HtmlExporter,CSVExporter,TabDelimExporter and so on to export to respective formats. See the full source code for Composer here
Version History
Version | Date | Content |
---|---|---|
All source code listed in this book is at Github.