Export Grid or Listbox to PDF or Excel"

From Documentation
Line 64: Line 64:
 
ZK Exporter leverage the famous iText library to create PDF documents. Renderer will use iText API directly. The PdfExporter conatins different Factory to create objects.
 
ZK Exporter leverage the famous iText library to create PDF documents. Renderer will use iText API directly. The PdfExporter conatins different Factory to create objects.
  
The PDF generation use iText will basically has 5 steps
+
The PdfExporter contains Factories that help create necessary objects. <br/>
1. Create a Document
+
1. DocumentFactory to create Document <br/>
2. Get PdfWriter instance
+
2. PdfWriterFactory to get PdfWriter instance <br/>
3. Open the document
+
3. PdfPTableFactory to get PdfPTable instance <br/>
4. Rendering content
+
4. FontFactory to get Font <br/>
5. Close document
+
5. PdfPCellFactory to create PdfPCell <br/>
 
 
The PdfExporter contains Factories that help create necessary objects.
 
1. DocumentFactory to create Document
 
2. PdfWriterFactory to get PdfWriter instance
 
3. PdfPTableFactory to get PdfPTable instance
 
4. FontFactory to get Font
 
5. PdfPCellFactory to create PdfPCell
 
  
 
*Render header using Interceptor.beforeRendering API
 
*Render header using Interceptor.beforeRendering API

Revision as of 09:05, 26 November 2012

Export Grid or Listbox to PDF or Excel

WarningTriangle-32x32.png This page is under construction, so we cannot guarantee the accuracy of the content!

DocumentationSmall Talks2012DecemberExport Grid or Listbox to PDF or Excel
Export Grid or Listbox to PDF or Excel

Author
Sam Chuang, Engineer, Potix Corporation
Date
November 26, 2012
Version
ZK 6.0 and later

Introduction

Web application developers often require to transform data into different format. For instance, to PDF format that enhance read accessibility and to Excel format that can further analyze data. This smalltalk is going to introduce a easy way that can transform from Grid or Listbox to PDF/Excel.

Demo

Grid/Listbox
Export Grid or Listbox to PDF or Excel demo.png

PDF
Export Grid or Listbox to PDF or Excel PDF Format.png

Excel
Export Grid or Listbox to PDF or Excel Excel Format.png

Usage

Export Listbox/Grid

As you can see the following sample code, export Listbox/Grid to PDF or Excel is quite straightforward.

PDF

	@Command
	public void exportGrid(@BindingParam("ref") Grid grid) throws Exception {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		
		PdfExporter exporter = new PdfExporter();
		exporter.export(grid, out);
		
		AMedia amedia = new AMedia("FirstReport.pdf", "pdf", "application/pdf", out.toByteArray());
		Filedownload.save(amedia);		
		out.close();
	}

Excel

	@Command
	public void exportListboxToExcel(@BindingParam("ref") Listbox listbox) throws Exception {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		
		ExcelExporter exporter = new ExcelExporter();
		exporter.export(listbox, out);
		
		AMedia amedia = new AMedia("FirstReport.xlsx", "xls", "application/file", out.toByteArray());
		Filedownload.save(amedia);
		out.close();
	}

Export data by Renderer

Although export Listbox/Grid to PDF or Excel is quite straightforward. However, it does have some limitation. For instance, it cannot export full contents with Grid/Listbox with ROD enabled. In such use case, you can use renderer to render raw data to PDF/Excel directly. Another advantage of renderer is that you can highly customize the rendering result.

PDF

ZK Exporter leverage the famous iText library to create PDF documents. Renderer will use iText API directly. The PdfExporter conatins different Factory to create objects.

The PdfExporter contains Factories that help create necessary objects.
1. DocumentFactory to create Document
2. PdfWriterFactory to get PdfWriter instance
3. PdfPTableFactory to get PdfPTable instance
4. FontFactory to get Font
5. PdfPCellFactory to create PdfPCell

  • Render header using Interceptor.beforeRendering API
		final PdfExporter exporter = new PdfExporter();
		final PdfPCellFactory cellFactory = exporter.getPdfPCellFactory();
		final FontFactory fontFactory = exporter.getFontFactory();
		
		final String[] headers = new String[]{"Name", "Top Nutrients", "% of Daily", "Calories", "Quantity"};
		exporter.setInterceptor(new Interceptor <PdfPTable> () {
			
			@Override
			public void beforeRendering(PdfPTable table) {
				for (int i = 0; i < headers.length; i++) {
					String header = headers[i];
					Font font = fontFactory.getFont(FontFactory.FONT_TYPE_HEADER);
					
					PdfPCell cell = cellFactory.getHeaderCell();
					cell.setPhrase(new Phrase(header, font));
					if ("% of Daily".equals(header) || "Calories".equals(header)) {
						cell.setHorizontalAlignment(Element.ALIGN_CENTER);
					}
					table.addCell(cell);
				}
				table.completeRow();
			}
			
			@Override
			public void afterRendering(PdfPTable table) {
			}
		});
  • Render contents using RowRenderer or GroupRenderer
		exporter.export(headers.length, _model.getData(), new GroupRenderer<PdfPTable, Food>() {

			@Override
			public void render(PdfPTable table, Food food, boolean isOddRow) {
				Font font = fontFactory.getFont(FontFactory.FONT_TYPE_CELL);

				PdfPCell cell = cellFactory.getCell(isOddRow);
				cell.setPhrase(new Phrase(food.getName(), font));
				table.addCell(cell);
				
				cell = cellFactory.getCell(isOddRow);
				cell.setPhrase(new Phrase(food.getTopNutrients(), font));
				table.addCell(cell);
				
				cell = cellFactory.getCell(isOddRow);
				cell.setPhrase(new Phrase("" + food.getDailyPercent(), font));
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(cell);
				
				cell = cellFactory.getCell(isOddRow);
				cell.setPhrase(new Phrase("" + food.getCalories(), font));
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(cell);
				
				cell = cellFactory.getCell(isOddRow);
				cell.setPhrase(new Phrase(food.getQuantity(), font));
				table.addCell(cell);
				
				table.completeRow();
			}

			@Override
			public void renderGroup(PdfPTable table, Collection<Food> foods) {
				Iterator<Food> iterator = foods.iterator();
				if (iterator.hasNext()) {
					Food food = iterator.next();
					Font font = fontFactory.getFont(FontFactory.FONT_TYPE_GROUP);

					PdfPCell cell = cellFactory.getGroupCell();					
					cell.setPhrase(new Phrase(food.getCategory(), font));
					cell.setColspan(headers.length);
					table.addCell(cell);
					
					table.completeRow();
				}
			}

			@Override
			public void renderGroupfoot(PdfPTable table, Collection<Food> foods) {
				Font font = fontFactory.getFont(FontFactory.FONT_TYPE_GROUPFOOT);

				PdfPCell cell = cellFactory.getGroupCell();
				cell.setPhrase(new Phrase("Total size: " + (foods != null ? foods.size() : 0), font));
				table.addCell(cell);
				
				cell = cellFactory.getCell(false);
				cell.setColspan(headers.length - 1);
				table.addCell(cell);
				table.completeRow();
			}
		}, out);

Excel

Summary

Download

  • The demo web application can be downloaded here - Github
  • The export.jar can be downloaded here- Github


Comments



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