Export to PDF"

From Documentation
m (correct highlight (via JWB))
 
(19 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{ZKSpreadsheetEssentialsPageHeader}}
 
{{ZKSpreadsheetEssentialsPageHeader}}
 +
 +
{{Deprecated|url=http://books.zkoss.org/wiki/ZK_Spreadsheet_Essentials}}
 +
 
__TOC__
 
__TOC__
 +
{{ZSS EE}}
 +
 
===Purpose===
 
===Purpose===
 
Export ZK Spreadsheet to PDF format
 
Export ZK Spreadsheet to PDF format
  
 
=== Getting Pdf Exporter===
 
=== Getting Pdf Exporter===
ZK Spreadsheet comes with <javadoc>org.zkoss.zss.model.Exporter</javadoc> implementation that allows ZK Spreadsheet contents to be exported to PDF format<ref>Available in zssex.jar</ref>.
+
ZK Spreadsheet comes with <javadoc directory="zss">org.zkoss.zss.model.Exporter</javadoc> implementation that allows ZK Spreadsheet contents to be exported to PDF format<ref>Available in zssex.jar</ref>.
To export ZK Spreadsheet to PDF format use <javadoc method="getExporter(java.lang.String)">org.zkoss.zss.model.Exporters</javadoc> as shown below.
+
Get PDF exporter using <javadoc directory="zss" method="getExporter(java.lang.String)">org.zkoss.zss.model.Exporters</javadoc> as shown below.
 
<source lang="java">
 
<source lang="java">
 
Exporter c = Exporters.getExporter("pdf");
 
Exporter c = Exporters.getExporter("pdf");
Line 12: Line 17:
  
 
<references/>
 
<references/>
 +
 
===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 of ZUML file that display an Excel book file and has three buttons to export entire workbook,selected sheet and specific selection.
 
<source lang="xml">
 
<source lang="xml">
 
<?page title="ZSS Export to Different File Format" contentType="text/html;charset=UTF-8"?>
 
<?page title="ZSS Export to Different File Format" contentType="text/html;charset=UTF-8"?>
Line 36: Line 42:
 
In composer there are three event handlers each for three export buttons defined in above ZUML.
 
In composer there are three event handlers each for three export buttons defined in above ZUML.
  
====Exporting entire workbook====
+
====Exporting the 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
+
To export the entire ZK Spreadsheet workbook content use <javadoc directory="zss" method="export(org.zkoss.zss.model.Book,java.io.OutputStream)">org.zkoss.zss.model.Exporter</javadoc> API. You should be able to get Book instance either by importing Excel book file using Importer interface or using <javadoc directory="zss" method="getBook()">org.zkoss.zss.ui.Spreadsheet</javadoc> 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" highlight="8,9" start="5">
 
public void onClick$exportBtn(Event evt) throws IOException {
 
public void onClick$exportBtn(Event evt) throws IOException {
 
Book wb = spreadsheet.getBook();
 
Book wb = spreadsheet.getBook();
Line 50: Line 56:
 
</source>
 
</source>
  
Here I am using <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> for exporting ZK Spreadsheet contents to Html, CSV, TabDelimExporter etc. formats.
+
Here I am using <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 content to PDF format. Similarly you could implement <javadoc directory="zss">org.zkoss.zss.model.Exporter</javadoc> for exporting ZK Spreadsheet contents to Html, CSV, TabDelimExporter etc. 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 content use Exporter#export(Worksheet worksheet, OutputStream outputStream) API. First get the Worksheet 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" highlight="6,9" start="5">
 
public void onClick$exportSheetBtn(Event evt) throws IOException {
 
public void onClick$exportSheetBtn(Event evt) throws IOException {
Sheet sheet = spreadsheet.getSelectedSheet();
+
Worksheet sheet = spreadsheet.getSelectedSheet();
 
Exporter c = Exporters.getExporter("pdf");
 
Exporter c = Exporters.getExporter("pdf");
 
ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Line 64: Line 71:
 
}
 
}
 
</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(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
  
<source lang="java" high="21,22" start="13">
+
<source lang="java" highlight="21,22" start="13">
 
public void onClick$exportCurrentSelectionBtn(Event evt) throws IOException {
 
public void onClick$exportCurrentSelectionBtn(Event evt) throws IOException {
 
Rect rect = spreadsheet.getSelection();
 
Rect rect = spreadsheet.getSelection();
Line 83: Line 91:
 
</source>
 
</source>
  
See the full source code for Composer [https://code.google.com/p/zkbooks/source/browse/trunk/zssessentials/examples/src/org/zkoss/zssessentials/export/ExportToPDFComposer.java here]
+
See the full source code for Composer click [https://code.google.com/p/zkbooks/source/browse/trunk/zssessentials/examples/src/org/zkoss/zssessentials/export/ExportToPDFComposer.java here]
  
 
=Version History=
 
=Version History=

Latest revision as of 12:56, 19 January 2022


Stop.png This article is out of date, please refer to http://books.zkoss.org/wiki/ZK_Spreadsheet_Essentials for more up to date information.

Available in ZK Spreadsheet EE only

Purpose

Export ZK Spreadsheet to PDF format

Getting Pdf Exporter

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

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

ZUML

Here is an example of 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 the entire workbook

To export the entire ZK Spreadsheet workbook content 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 I am using Exporters to get Exporter implementation to export ZK Spreadsheet content to PDF format. Similarly you could implement Exporter for exporting ZK Spreadsheet contents to Html, CSV, TabDelimExporter etc. formats.

Exporting specific worksheet

To export specific worksheet content use Exporter#export(Worksheet worksheet, OutputStream outputStream) API. First get the 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("pdf");
		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(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("pdf");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		c.exportSelection(spreadsheet.getSelectedSheet(), new AreaReference(
				area), baos);
		Filedownload.save(baos.toByteArray(), "application/pdf", spreadsheet
				.getBook().getBookName());
	}

See the full source code for Composer click here

Version History

Last Update : 2022/01/19


Version Date Content
     


All source code listed in this book is at Github.


Last Update : 2022/01/19

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