Export to Different File Format"

From Documentation
Line 6: Line 6:
  
 
====Exporter interface====
 
====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.
+
Exporting ZK Spreadsheet to another format is done through <javadoc directory="zss">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(Book workbook, OutputStream outputStream) API to export entire workbook.
 
:* Use Exporter#export(Sheet worksheet, OutputStream outputStream); API to export specific worksheet.
 
:* 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.
 
:* Use Exporter#exportSelection(Sheet worksheet, AreaReference area, OutputStream outputStream) API to export partially selected area of workbook.
  
You can use <javadoc>org.zkoss.zss.model.Exporters</javadoc> to get specific <javadoc>org.zkoss.zss.model.Exporter</javadoc> implementation. For example to get <javadoc>org.zkoss.zss.model.Exporter</javadoc> implementation for PDF format use
+
You can use <javadoc directory="zss">org.zkoss.zss.model.Exporters</javadoc> to get specific <javadoc directory="zss">org.zkoss.zss.model.Exporter</javadoc> implementation. For example to get <javadoc directory="zss">org.zkoss.zss.model.Exporter</javadoc> implementation for PDF format use
 
<source lang="java">
 
<source lang="java">
 
Exporter pdfExporter = Exporters.getExporter("pdf");
 
Exporter pdfExporter = Exporters.getExporter("pdf");
 
</source>
 
</source>
  
You can also implement <javadoc>org.zkoss.zss.model.Exporter</javadoc> for exporting to another format such as CSV or HTML. You will need to register your implementation in zk.xml by defining <code>org.zkoss.zss.model.Exporter.class</code> library property. For example
+
You can also implement <javadoc directory="zss">org.zkoss.zss.model.Exporter</javadoc> for exporting to another format such as CSV or HTML. You will need to register your implementation in zk.xml by defining <code>org.zkoss.zss.model.Exporter.class</code> library property. For example
 
<source lang="xml">
 
<source lang="xml">
 
<library-property>
 
<library-property>
Line 55: Line 55:
 
</source>
 
</source>
  
Here we use <javadoc>org.zkoss.zss.model.Exporters</javadoc> to get <javadoc>org.zkoss.zss.model.Exporter</javadoc> implementation to export ZK Spreadsheet contents to PDF format. Similarly you could implement <javadoc>org.zkoss.zss.model.Exporter</javadoc> to export to CSV,HTML etc. formats.
+
Here we use <javadoc directory="zss">org.zkoss.zss.model.Exporters</javadoc> to get <javadoc directory="zss">org.zkoss.zss.model.Exporter</javadoc> implementation to export ZK Spreadsheet contents to PDF format. Similarly you could implement <javadoc directory="zss">org.zkoss.zss.model.Exporter</javadoc> to export to CSV,HTML etc. formats.
  
 
See the full source code for Composer [https://code.google.com/p/zkbooks/source/browse/trunk/zssessentials/examples/src/org/zkoss/zssessentials/export/ExportComposer.java here]
 
See the full source code for Composer [https://code.google.com/p/zkbooks/source/browse/trunk/zssessentials/examples/src/org/zkoss/zssessentials/export/ExportComposer.java here]

Revision as of 00:52, 20 December 2010


Export to Different File Format



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.

You can use Exporters to get specific Exporter implementation. For example to get Exporter implementation for PDF format use

Exporter pdfExporter = Exporters.getExporter("pdf");

You can also implement Exporter for exporting to another format such as CSV or HTML. You will need to register your implementation in zk.xml by defining org.zkoss.zss.model.Exporter.class library property. For example

	<library-property>
		<name>org.zkoss.zss.model.Exporter.class</name>
		<value>html=org.xyz.com.HtmlExporter</value>
	</library-property>

NOTE: Exporter type name and implementing class name is separated with = character. You can register multiple Exporter implementations by separating type-name=class-name pairs by a comma.

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 = Exporters.getExporter("pdf");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		c.export(wb, baos);
		Filedownload.save(baos.toByteArray(), "application/pdf",
				wb.getBookName());
	}

Here we use Exporters to get Exporter implementation to export ZK Spreadsheet contents to PDF format. Similarly you could implement Exporter to export to CSV,HTML etc. formats.

See the full source code for Composer here

Version History

Last Update : 2010/12/20


Version Date Content
     


All source code listed in this book is at Github.


Last Update : 2010/12/20

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