Fala galera,
Comecei na facu a desenvolver em JSF e utilizo o PrimeFaces 3.5.
Fiz algumas pesquisas e ainda não ficou claro para mim o funcionamento do dataExporter.
Os códigos que elaborei não mostram nenhum erro, mas infelizmente não geram os arquivos.
Gostaria de saber se estou errando em alguma coisa ou esquecendo algo.
Utilizo Win7, Netbeans 7.2 e o Tomcat 7.0.29
Agradeço a ajuda.
Segue meus códigos:
index.xhtml
<!-- Tabela -->
<p:dataTable id="tabela" var="usuarioDTO" value="#{usuarioBean.lista}" paginator="true" rows="4"
style="margin: 5px auto; width: 360px; height: 232px;" >
<p:column>
<f:facet name="header" >
<h:outputText value="Código" />
</f:facet>
<h:outputText value="#{usuarioDTO.usuarioId}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Nome" />
</f:facet>
<h:outputText value="#{usuarioDTO.nome}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Login" />
</f:facet>
<h:outputText value="#{usuarioDTO.login}" />
</p:column>
</p:dataTable>
<!-- Exportar Arquivos -->
<h:panelGrid columns="2" style="margin: 5px auto; width: 503px; height: 100px; border-radius: 10px; ">
<p:panel header="Exportar Tabela" >
<h:commandLink>
<p:graphicImage value="/img/icon_excel.png"
style="margin-right: 5px; border: 1px solid #DDD; width: 30px; height: 30px; padding: 3px 3px; border-radius: 10px;"
title="Arquivo EXCEL" />
<p:dataExporter type="xls" target="tabela" fileName="usuariosExcel" postProcessor="#{arquivoBean.gerarExcel}" />
</h:commandLink>
<h:commandLink>
<p:graphicImage value="/img/icon_pdf.png"
style="margin-right: 5px; border: 1px solid #DDD; width: 30px; height: 30px; padding: 3px 3px; border-radius: 10px;"
title="Arquivo PDF" />
<p:dataExporter type="pdf" target="tabela" fileName="usuariosPdf" postProcessor="#{arquivoBean.gerarPDF}" />
</h:commandLink>
<h:commandLink>
<p:graphicImage value="/img/icon_csv.png"
style="margin-right: 5px; border: 1px solid #DDD; width: 30px; height: 30px; padding: 3px 3px; border-radius: 10px;"
title="Arquivo CSV" />
<p:dataExporter type="csv" target="tabela" fileName="usuariosCsv" postProcessor="#{arquivoBean.gerarCsv}" />
</h:commandLink>
<h:commandLink>
<p:graphicImage value="/img/icon_xml.png"
style="margin-right: 5px; border: 1px solid #DDD; width: 30px; height: 30px; padding: 3px 3px; border-radius: 10px;"
title="Arquivo XML" />
<p:dataExporter type="xml" target="tabela" fileName="usuariosXml" postProcessor="#{bean.gerarXml}" />
</h:commandLink>
</p:panel>
<p:panel header="Exportar Página" >
<h:commandLink>
<p:graphicImage value="/img/icon_excel.png"
style="margin-right: 5px; border: 1px solid #DDD; width: 30px; height: 30px; padding: 3px 3px; border-radius: 10px;"
title="Arquivo EXCEL" />
<p:dataExporter type="xls" target="tabela" fileName="usuariosExcel" />
</h:commandLink>
<h:commandLink>
<p:graphicImage value="/img/icon_pdf.png"
style="margin-right: 5px; border: 1px solid #DDD; width: 30px; height: 30px; padding: 3px 3px; border-radius: 10px;"
title="Arquivo PDF" />
<p:dataExporter type="pdf" target="tabela" fileName="usuariosPdf"/>
</h:commandLink>
<h:commandLink>
<p:graphicImage value="/img/icon_csv.png"
style="margin-right: 5px; border: 1px solid #DDD; width: 30px; height: 30px; padding: 3px 3px; border-radius: 10px;"
title="Arquivo CSV" />
<p:dataExporter type="csv" target="tabela" fileName="usuariosCsv" />
</h:commandLink>
<h:commandLink>
<p:graphicImage value="/img/icon_xml.png"
style="margin-right: 5px; border: 1px solid #DDD; width: 30px; height: 30px; padding: 3px 3px; border-radius: 10px;"
title="Arquivo XML" />
<p:dataExporter type="xml" target="tabela" fileName="usuariosXml" />
</h:commandLink>
</p:panel>
</h:panelGrid>
ArquivoBean.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.edu.etep.cadastros.view.managedBean;
import com.itextpdf.text.*;
import java.io.File;
import java.io.IOException;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
/**
*
*
* @Author: Daniel A. Salles
* @Date: 05/08/2013
*
*
*/
@ManagedBean(name = "arquivoBean")
public class ArquivoBean {
/*
* @Method: Gerador de Arquivos Excel.
*
*/
public void gerarXLS(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);
}
}
/*
* @Method: Gerador de Arquivos PDF.
*
*/
public void gerarPDF(Object document) throws IOException, BadElementException, DocumentException {
Document pdf = (Document) document;
pdf.open();
pdf.setPageSize(PageSize.A4);
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String logo = servletContext.getRealPath("") + File.separator + "images" + File.separator + "prime_logo.png";
pdf.add(Image.getInstance(logo));
}
}