<a:form> <a:dataTable id="tbl" var="car" value="#{customizedDocumentsView.cars}" paginator="true" rows="10"> <a:column> <f:facet name="header"> <h:outputText value="Id" /> </f:facet> <h:outputText value="#{car.id}" /> </a:column> <a:column> <f:facet name="header"> <h:outputText value="Year" /> </f:facet> <h:outputText value="#{car.year}" /> </a:column> <a:column> <f:facet name="header"> <h:outputText value="Brand" /> </f:facet> <h:outputText value="#{car.brand}" /> </a:column> <a:column> <f:facet name="header"> <h:outputText value="Color" /> </f:facet> <h:outputText value="#{car.color}" /> </a:column> </a:dataTable> <h:commandLink id="excel"> <a:graphicImage name="/demo/images/excel.png" /> <a:dataExporter type="xls" target="tbl" fileName="cars" postProcessor="#{customizedDocumentsView.postProcessXLS}" /> </h:commandLink> <h:commandLink id="pdf"> <a:graphicImage name="/demo/images/pdf.png" /> <a:dataExporter type="pdf" target="tbl" fileName="cars" preProcessor="#{customizedDocumentsView.preProcessPDF}" /> </h:commandLink> <a:dataTable id="tbl2" var="car" value="#{customizedDocumentsView.cars2}" paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {Exporters}" paginator="true" rows="10" style="margin-top: 25px;"> <f:facet name="{Exporters}"> <div style="float:right"> <h:commandLink> <a:graphicImage name="/demo/images/excel.png" width="24"/> <a:dataExporter type="xls" target="tbl2" fileName="cars" options="#{customizedDocumentsView.excelOpt}" /> </h:commandLink> <h:commandLink> <a:graphicImage name="/demo/images/pdf.png" width="24"/> <a:dataExporter type="pdf" target="tbl2" fileName="cars" options="#{customizedDocumentsView.pdfOpt}" /> </h:commandLink> </div> </f:facet> <a:column> <f:facet name="header"> <h:outputText value="Id" /> </f:facet> <h:outputText value="#{car.id}" /> </a:column> <a:column> <f:facet name="header"> <h:outputText value="Year" /> </f:facet> <h:outputText value="#{car.year}" /> </a:column> <a:column> <f:facet name="header"> <h:outputText value="Brand" /> </f:facet> <h:outputText value="#{car.brand}" /> </a:column> <a:column> <f:facet name="header"> <h:outputText value="Color" /> </f:facet> <h:outputText value="#{car.color}" /> </a:column> </a:dataTable> </a:form>
package com.jarch.showcase.view.data.dataexporter; import com.lowagie.text.BadElementException; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import java.io.File; import java.io.IOException; import java.io.Serializable; import java.util.List; import javax.annotation.PostConstruct; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.primefaces.component.export.ExcelOptions; import org.primefaces.component.export.PDFOptions; import org.primefaces.showcase.domain.Car; import org.primefaces.showcase.service.CarService; @ManagedBean public class CustomizedDocumentsView implements Serializable { private List<Car> cars; private List<Car> cars2; private ExcelOptions excelOpt; private PDFOptions pdfOpt; @ManagedProperty("#{carService}") private CarService service; @PostConstruct public void init() { cars = service.createCars(100); cars2 = service.createCars(100); customizationOptions(); } public void customizationOptions() { excelOpt = new ExcelOptions(); excelOpt.setFacetBgColor("#F88017"); excelOpt.setFacetFontSize("10"); excelOpt.setFacetFontColor("#0000ff"); excelOpt.setFacetFontStyle("BOLD"); excelOpt.setCellFontColor("#00ff00"); excelOpt.setCellFontSize("8"); pdfOpt = new PDFOptions(); pdfOpt.setFacetBgColor("#F88017"); pdfOpt.setFacetFontColor("#0000ff"); pdfOpt.setFacetFontStyle("BOLD"); pdfOpt.setCellFontSize("12"); } public List<Car> getCars() { return cars; } public List<Car> getCars2() { return cars2; } public ExcelOptions getExcelOpt() { return excelOpt; } public void setExcelOpt(ExcelOptions excelOpt) { this.excelOpt = excelOpt; } public PDFOptions getPdfOpt() { return pdfOpt; } public void setPdfOpt(PDFOptions pdfOpt) { this.pdfOpt = pdfOpt; } public void setService(CarService service) { this.service = service; } public void postProcessXLS(Object document) { HSSFWorkbook wb = (HSSFWorkbook) document; HSSFSheet sheet = wb.getSheetAt(0); HSSFRow header = sheet.getRow(0); HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setFillForegroundColor(HSSFColor.GREEN.index); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); for(int i=0; i < header.getPhysicalNumberOfCells();i++) { HSSFCell cell = header.getCell(i); cell.setCellStyle(cellStyle); } } public void preProcessPDF(Object document) throws IOException, BadElementException, DocumentException { Document pdf = (Document) document; pdf.open(); pdf.setPageSize(PageSize.A4); ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); String logo = externalContext.getRealPath("") + File.separator + "resources" + File.separator + "demo" + File.separator + "images" + File.separator + "prime_logo.png"; pdf.add(Image.getInstance(logo)); } }