Export to Excel"
From Documentation
m (correct highlight (via JWB)) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
− | One of Spreadsheet's powerful feature is to export its book model as an Excel file then we can open the file with Microsoft Excel. Besides, exporting to a file is also the only way to persist | + | One of Spreadsheet's powerful feature is to export its book model as an Excel file then we can open the file with Microsoft Excel. Besides, exporting to a file is also the only way to persist a book model completely and then import it in the future. The following codes demonstrate how to export a book model to a temporary file with <javadoc directory="zss">org.zkoss.zss.api.Exporter</javadoc> and make users download it in a browser: |
− | <source lang='java' | + | <source lang='java' highlight='8, 14'> |
public class ExportComposer extends SelectorComposer<Component> { | public class ExportComposer extends SelectorComposer<Component> { | ||
@Wire | @Wire | ||
Line 32: | Line 32: | ||
} | } | ||
</source> | </source> | ||
− | * Line 8: Get | + | * Line 8: Get a default <code>Exporter</code> which exports as xlsx format. |
* Line 14: Currently, we only support exporting whole book. | * Line 14: Currently, we only support exporting whole book. | ||
− | + | You can get different Exporter by its type: | |
+ | <source lang='java'> | ||
+ | Exporters.getExporter(); //get default exporter, xlsx | ||
+ | Exporters.getExporter("xlsx"); //get xlsx exporter | ||
+ | Exporters.getExporter("xls"); //get xls exporter | ||
+ | </source> | ||
{{ZKSpreadsheetEssentialsPageFooter}} | {{ZKSpreadsheetEssentialsPageFooter}} |
Latest revision as of 12:52, 19 January 2022
One of Spreadsheet's powerful feature is to export its book model as an Excel file then we can open the file with Microsoft Excel. Besides, exporting to a file is also the only way to persist a book model completely and then import it in the future. The following codes demonstrate how to export a book model to a temporary file with Exporter and make users download it in a browser:
public class ExportComposer extends SelectorComposer<Component> {
@Wire
private Spreadsheet ss;
@Listen("onClick = #exportExcel")
public void doExport() throws IOException{
Exporter exporter = Exporters.getExporter();
Book book = ss.getBook();
File file = File.createTempFile(Long.toString(System.currentTimeMillis()),"temp");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
exporter.export(book, fos);
}finally{
if(fos!=null){
fos.close();
}
}
//generate file name upon book type (2007,2003)
String dlname = BookUtil.suggestName(book);
Filedownload.save(new AMedia(dlname, null, null, file, true));
}
}
- Line 8: Get a default
Exporter
which exports as xlsx format. - Line 14: Currently, we only support exporting whole book.
You can get different Exporter by its type:
Exporters.getExporter(); //get default exporter, xlsx
Exporters.getExporter("xlsx"); //get xlsx exporter
Exporters.getExporter("xls"); //get xls exporter
All source code listed in this book is at Github.