我正在尝试使用简单的代码生成pdf,xls,docs和ppt文件。我可以生成它,但它给出了一个blak页面,并且在Stacktrace中没有错误。我认为它不会从数据库中获取数据。
这是代码,Bean页面:
@ManagedBean
@SessionScoped
public class CompteRapport implements Serializable{
private static final long serialVersionUID = 1L;
private List<Users>listusers;
UsersDAO uDao= new UsersDAOImp();
JasperPrint jasperPrint;
public void init() throws JRException{
JRBeanCollectionDataSource beanCollectionDataSource=new JRBeanCollectionDataSource(listusers);
String reportPath= FacesContext.getCurrentInstance().getExternalContext().getRealPath("/Report/users.jasper");
jasperPrint=JasperFillManager.fillReport(reportPath, new HashMap(),beanCollectionDataSource);
}
public void PDF(ActionEvent actionEvent) throws JRException, IOException{
init();
HttpServletResponse httpServletResponse=(HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
httpServletResponse.addHeader("Content-disposition", "attachment; filename=users.pdf");
ServletOutputStream servletOutputStream=httpServletResponse.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
FacesContext.getCurrentInstance().responseComplete();
}
public void DOCX(ActionEvent actionEvent) throws JRException, IOException{
init();
HttpServletResponse httpServletResponse=(HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.docx");
ServletOutputStream servletOutputStream=httpServletResponse.getOutputStream();
JRDocxExporter docxExporter=new JRDocxExporter();
docxExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
docxExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, servletOutputStream);
docxExporter.exportReport();
FacesContext.getCurrentInstance().responseComplete();
}
public void XLSX(ActionEvent actionEvent) throws JRException, IOException{
init();
HttpServletResponse httpServletResponse=(HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.xlsx");
ServletOutputStream servletOutputStream=httpServletResponse.getOutputStream();
JRXlsxExporter docxExporter=new JRXlsxExporter();
docxExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
docxExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, servletOutputStream);
docxExporter.exportReport();
FacesContext.getCurrentInstance().responseComplete();
}
public void ODT(ActionEvent actionEvent) throws JRException, IOException{
init();
HttpServletResponse httpServletResponse=(HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.odt");
ServletOutputStream servletOutputStream=httpServletResponse.getOutputStream();
JROdtExporter docxExporter=new JROdtExporter();
docxExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
docxExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, servletOutputStream);
docxExporter.exportReport();
FacesContext.getCurrentInstance().responseComplete();
}
public void PPT(ActionEvent actionEvent) throws JRException, IOException{
init();
HttpServletResponse httpServletResponse=(HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.pptx");
ServletOutputStream servletOutputStream=httpServletResponse.getOutputStream();
JRPptxExporter docxExporter=new JRPptxExporter();
docxExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
docxExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, servletOutputStream);
docxExporter.exportReport();
FacesContext.getCurrentInstance().responseComplete();
}
public List<Users> getListusers() {
return listusers;
}
public void setListusers(List<Users> listusers) {
this.listusers = listusers;
}
public UsersDAO getuDao() {
return uDao;
}
public void setuDao(UsersDAO uDao) {
this.uDao = uDao;
}
}