简体   繁体   English

导出到.pdf JAVA时文件显示多个文件

[英]The file show more than one when export into .pdf JAVA

Hi I tried make a exporting data into .pdf, so when I view a "detail" it will show a set of data and on detail have an exporting into .pdf, when the data exporting into .pdf, the data will become a one file. 您好,我尝试将数据导出到.pdf,所以当我查看“详细信息”时,它将显示一组数据,并详细说明了将数据导出到.pdf的情况,当数据导出到.pdf时,数据将成为一个数据文件。 But when I try to exporting the file more than one like this 但是当我尝试导出多个这样的文件时

enter image description here 在此处输入图片说明

but it's not the same data it's different data from database, and I want to ask how this can happened and how to fixed this ?? 但这不是相同的数据,而是不同于数据库的数据,我想问问这怎么发生以及如何解决?

this the code on bean: 这在豆上的代码:

    @PostConstruct
public void init(){

    this.setLazyMasterReportDataModel(new LazyMasterReportDataModel()); 
}

public JasperPrint exportTo() {     
    if(this.listReportMaster == null || this.listReportMaster.isEmpty()){           
        FacesMessage messageFailed = new FacesMessage(FacesMessage.SEVERITY_INFO,"Info","No data found");           
        RequestContext.getCurrentInstance().showMessageInDialog(messageFailed);         
        return null;
    }
    String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources/report/PRPKReportPDF.jasper");     
    JRBeanCollectionDataSource  beanCollectionDataSource = new JRBeanCollectionDataSource(this.listReportMaster);       
    try {           
        JasperPrint jasperPrint = JasperFillManager.fillReport(path, null, beanCollectionDataSource);           
        return jasperPrint;         
    } catch (JRException e) {           
        e.printStackTrace();
        return null;
    }
}   

public void exportToPdf(ActionEvent actionEvent){   
    if(this.lazyMasterReportDataModel != null){
        System.out.println("masuk exporttopdf");
        String sql = ((LazyMasterReportDataModel) this.lazyMasterReportDataModel).getSqlReportPrint();
        List<Object> listObject = ((LazyMasterReportDataModel) this.lazyMasterReportDataModel).getObjectSqlListReportPrint();
        this.listReportMaster = reportMasterPRPKController.getPRPKForReport(sql, listObject);           

        JasperPrint jasperPrint = exportTo();
        String fileName = "PRPKNew_Report".concat("_").concat(".pdf");
        if(jasperPrint != null) reportMasterPRPKController.exportToPDF(fileName, jasperPrint);
        else System.out.println("jasperprint null");
    }else{
        System.out.println("keluar exporttopdf");
        FacesMessage messageFailed = new FacesMessage(FacesMessage.SEVERITY_INFO,"Info","No data found");           
        RequestContext.getCurrentInstance().showMessageInDialog(messageFailed);
    }
}

this code on controller 该代码在控制器上

public void exportToPDF(String fileName, JasperPrint jasperPrint){
    try{            
        System.out.println("masuk controller exportpdf");
        HttpServletResponse httpServletResponse = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        httpServletResponse.addHeader("Content-disposition", "attachment; filename=".concat(fileName));
        ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
        servletOutputStream.flush();
        servletOutputStream.close(); 
        FacesContext.getCurrentInstance().responseComplete();   
        System.out.println("selesai print");
    } catch (JRException | IOException e) {         
        e.printStackTrace();
    }       
}

thank's before 谢谢之前

The variable listObject in exportToPdf method seems to contains the list of objects passed to the report generation. exportToPdf方法中的变量listObject似乎包含传递给报告生成的对象的列表。

For each of these objects, a detail band will be generated . 对于这些对象中的每一个, 都会生成一个细节带 If you have only one detail band and 100 objects in this list, 100 band will be generated in the pdf export. 如果此列表中只有一个明细区域和100个对象,则在pdf导出中将生成100个区域。

I would advise you to only provide one object to your pdf generation if you want only one detail band to be displayed. 如果您只希望显示一个明细带,我建议您仅为pdf生成一个对象。

See this question about the detail band for more info 有关详细信息,请参阅此问题 ,以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM