Export to HTML

From Documentation
Revision as of 07:36, 7 March 2011 by Peterkuo (talk | contribs) (Created page with "{{ZKSpreadsheetEssentialsPageHeader}} __TOC__ {{ZSS EE}} ===Purpose=== Export ZK Spreadsheet to HTML format === Getting Html Exporter=== ZK Spreadsheet comes with <javadoc dire...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Available in ZK Spreadsheet EE only

Purpose

Export ZK Spreadsheet to HTML format

Getting Html Exporter

ZK Spreadsheet comes with Exporter implementation that allows ZK Spreadsheet contents to be exported to HTML format[1]. Get HTML exporter using Exporters.getExporter(String) as shown below.

Exporter c = Exporters.getExporter("html");
  1. Available in zssex.jar

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>
		<listbox id="sheets" mold="select"/>
		<button id="exportSheetBtn" label="Export Sheet"></button>
		<button id="exportCurrentSelectionBtn"
			label="Export Selection">
		</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 are three event handlers each for three export buttons defined in above ZUML.

Exporting entire workbook

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

Here I am using Exporters to get Exporter implementation to export ZK Spreadsheet contents to HTML format. Similarly you could implement Exporter for exporting ZK Spreadsheet contents to PDF, CSV, TabDelimExporter etc. formats.

Exporting specific worksheet

To export specific worksheet contents use Exporter#export(Worksheet worksheet, OutputStream outputStream) API. First get Worksheet instance by Spreadsheet#getSelectedSheet() or Spreadsheet#getSheet(int). Second prepare OutputStream to which new format should be written to. For example

	public void onClick$exportSheetBtn(Event evt) throws IOException {
		Worksheet sheet = spreadsheet.getSelectedSheet();
		Exporter c = Exporters.getExporter("html");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		c.export(sheet, baos);
		Filedownload.save(baos.toByteArray(), "text/html", spreadsheet
				.getBook().getBookName());
	}

Exporting partial selection

To export specific area of worksheet indicated by current selection of multiple cells, use Exporter#exportSelection(Worksheet worksheet, AreaReference area, OutputStream outputStream) API. First get Worksheet instance by Spreadsheet#getSelectedSheet() or Spreadsheet#getSheet(int). Second get current selection using Spreadsheet#getSelection() API and prepare an AreaReference and finally prepare OutputStream to which new format should be written to. For example

	public void onClick$exportCurrentSelectionBtn(Event evt) throws IOException {
		Rect rect = spreadsheet.getSelection();
		String area = spreadsheet.getColumntitle(rect.getLeft())
				+ spreadsheet.getRowtitle(rect.getTop()) + ":"
				+ spreadsheet.getColumntitle(rect.getRight())
				+ spreadsheet.getRowtitle(rect.getBottom());
		Exporter c = Exporters.getExporter("html");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		c.exportSelection(spreadsheet.getSelectedSheet(), new AreaReference(
				area), baos);
		Filedownload.save(baos.toByteArray(), "text/html", spreadsheet
				.getBook().getBookName());
	}

See the full source code for Composer here

Version History

Last Update : 2011/03/07


Version Date Content
     


All source code listed in this book is at Github.

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