Export to Different File Format"

From Documentation
Line 2: Line 2:
  
 
__TOC__
 
__TOC__
==Purpose==
+
===Purpose===
Export ZK Spreadsheet to different file format (Html,CSV, Tab delimited and so on)
+
Export ZK Spreadsheet to different file format (PDF, Html,CSV, Tab delimited and so on)
==Exporter interface==
+
===ZUML===
Exporting ZK Spreadsheet to another format is done through ''Exporter'' interface. It has three convenient methods to export ZK Spreadsheet contents.
+
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.
:* 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.
 
===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
 
 
<source lang="xml">
 
<source lang="xml">
 
<window width="100" height="100%" apply="org.zkoss.zsspdf.demo.MySpreadsheetComposer">
 
<window width="100" height="100%" apply="org.zkoss.zsspdf.demo.MySpreadsheetComposer">
Line 27: Line 22:
 
</window>
 
</window>
 
</source>
 
</source>
In above  code ZK Spreadsheet component is initialized with default.xls Excel book file. Exporting this Excel book file is shown below.
+
 
 +
====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.
 +
====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
  
 
<source lang="java" high="8,9" start="5">
 
<source lang="java" high="8,9" start="5">
Line 40: Line 42:
  
 
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 PdfExporter to export ZK Spreadsheet contents to PDF format. Similarly you could implement HtmlExporter,CSVExporter,TabDelimExporter and so on to export to respective formats.
===Exporting specific worksheet===
+
====Exporting specific worksheet====
 
To export specific worksheet contents use Exporter#export(Sheet worksheet, OutputStream outputStream) API. First get Sheet instance by Spreadsheet#getSelectedSheet() or Spreadsheet#getSheet(int). Second prepare OutputStream to which new format should be written to. For example
 
To export specific worksheet contents use Exporter#export(Sheet worksheet, OutputStream outputStream) API. First get Sheet instance by Spreadsheet#getSelectedSheet() or Spreadsheet#getSheet(int). Second prepare OutputStream to which new format should be written to. For example
 
<source lang="java" high="6,9" start="5">
 
<source lang="java" high="6,9" start="5">
Line 51: Line 53:
 
}
 
}
 
</source>
 
</source>
===Exporting partial selection===
+
====Exporting partial selection====
 
To export specific area of worksheet indicated by current selection of multiple cells, use Exporter#exportSelection(Sheet worksheet, AreaReference area, OutputStream outputStream) API. First get Sheet 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
 
To export specific area of worksheet indicated by current selection of multiple cells, use Exporter#exportSelection(Sheet worksheet, AreaReference area, OutputStream outputStream) API. First get Sheet 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
 
<source lang="java" high="17,20" start="13">
 
<source lang="java" high="17,20" start="13">

Revision as of 03:06, 23 November 2010


Export to Different File Format



Purpose

Export ZK Spreadsheet to different file format (PDF, Html,CSV, Tab delimited and so on)

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.

<window width="100" height="100%" apply="org.zkoss.zsspdf.demo.MySpreadsheetComposer">
<vbox>
<button id="exportBtn" label="Export"></button>
<button id="exportSheetBtn" label="Export"></button>
<button id="exportCurrentSelectionBtn" label="Export"></button>
</vbox>
<spreadsheet id="spreadsheet" 
			src="/default.xls"
			maxrows="200" 
			maxcolumns="40" 
			vflex="1" 
			width="100%">
</spreadsheet>
</window>

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.

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() throws IOException {
		Book wb = spreadsheet.getBook();
		ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
		Exporter c = new PdfExporter();
		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.

Exporting specific worksheet

To export specific worksheet contents use Exporter#export(Sheet worksheet, OutputStream outputStream) API. First get Sheet 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() throws IOException {
		Sheet sheet = spreadsheet.getSelectedSheet();
		Exporter c = new PdfExporter();
		ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
		c.export(sheet, baos);
		Filedownload.save(baos.toByteArray(),"application/pdf", spreadsheet.getBook().getBookName());
	}

Exporting partial selection

To export specific area of worksheet indicated by current selection of multiple cells, use Exporter#exportSelection(Sheet worksheet, AreaReference area, OutputStream outputStream) API. First get Sheet 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() throws IOException {
		Sheet sheet = spreadsheet.getSelectedSheet();
		Exporter c = new PdfExporter();
		ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
		Rect rect = spreadsheet.getSelection();
		String area = ss.getColumntitle(rect.getLeft()) + ss.getRowtitle(rect.getTop()) + ":" + 
			ss.getColumntitle(rect.getRight()) + ss.getRowtitle(rect.getBottom());
		c.exportSelection(sheet,  new AreaReference(area), baos);
		Filedownload.save(baos.toByteArray(),"application/pdf", spreadsheet.getBook().getBookName());
	}

Version History

Last Update : 2010/11/23


Version Date Content
     


All source code listed in this book is at Github.


Last Update : 2010/11/23

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