Export to Excel"
m (correct highlight (via JWB)) |
|||
(11 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__ | ||
===Purpose=== | ===Purpose=== | ||
Line 5: | Line 9: | ||
=== Getting Excel Exporter=== | === Getting Excel Exporter=== | ||
− | ZK Spreadsheet comes with <javadoc>org.zkoss.zss.model.Exporter</javadoc> implementation that allows ZK Spreadsheet contents to be exported to Excel format. | + | ZK Spreadsheet comes with <javadoc directory="zss">org.zkoss.zss.model.Exporter</javadoc> implementation that allows ZK Spreadsheet contents(the Book) to be exported to the Excel format(xls or xlsx per the Book type). |
− | Get Excel exporter using <javadoc method="getExporter(java.lang.String)">org.zkoss.zss.model.Exporters</javadoc> as shown below. | + | Get Excel 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("excel"); | Exporter c = Exporters.getExporter("excel"); | ||
Line 14: | Line 18: | ||
===ZUML=== | ===ZUML=== | ||
− | Here is an example ZUML file that | + | Here is an example of ZUML file that displays an Excel book file and has a button to export the entire workbook. |
<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 30: | Line 34: | ||
===Composer=== | ===Composer=== | ||
− | In composer there is one event handler for export button defined in above ZUML. | + | In composer there is one event handler for export button defined in the above ZUML. |
− | ====Exporting entire workbook==== | + | ====Exporting the entire workbook==== |
− | To export entire ZK Spreadsheet workbook contents use | + | To export the entire ZK Spreadsheet workbook contents 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 Excel format should be written to. For example |
− | <source lang="java" | + | <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 46: | Line 50: | ||
</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 Excel format. | + | 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 contents to Excel format. |
See the full source code for Composer [https://code.google.com/p/zkbooks/source/browse/trunk/zssessentials/examples/src/org/zkoss/zssessentials/export/ExportToExcelComposer.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/ExportToExcelComposer.java here] |
Latest revision as of 12:56, 19 January 2022
This article is out of date, please refer to http://books.zkoss.org/wiki/ZK_Spreadsheet_Essentials for more up to date information.
Purpose
Export ZK Spreadsheet to Excel format
Getting Excel Exporter
ZK Spreadsheet comes with Exporter implementation that allows ZK Spreadsheet contents(the Book) to be exported to the Excel format(xls or xlsx per the Book type). Get Excel exporter using Exporters.getExporter(String) as shown below.
Exporter c = Exporters.getExporter("excel");
ZUML
Here is an example of ZUML file that displays an Excel book file and has a button to export the entire workbook.
<?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 for export button defined in the above ZUML.
Exporting the entire workbook
To export the 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 Excel format should be written to. For example
public void onClick$exportBtn(Event evt) throws IOException {
Book wb = spreadsheet.getBook();
Exporter c = Exporters.getExporter("excel");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
c.export(wb, baos);
Filedownload.save(baos.toByteArray(), "application/file",
wb.getBookName());
}
Here I am using Exporters to get Exporter implementation to export ZK Spreadsheet contents to Excel format.
See the full source code for Composer here
Version History
Version | Date | Content |
---|---|---|
All source code listed in this book is at Github.