不要查看第二个JasperPrint中的数据

时间:2017-01-27 12:44:39

标签: java jasper-reports

我想从许多其他JasperReport创建一个报告。但我没有看到第一个JasperPrint之后的数据。如果我要更改地点jasperPrintTrackingjasperPrintDeparture。您将看到我首先编写的JasperPrint数据,但第二个JasperPrint没有任何数据。如果我最初写了jasperPrintTracking - 我会看到来自jasperPrintTracking的数据,但是看不到来自jasperPrintDeparture的数据,他们没有下载这些数据。为什么会这样?

public class RoadReport {
    JasperPrint jasperPrintDeparture;
    JasperPrint jasperPrintTracking;
    CardFile cardFile = new CardFile();

    public void createRoadReport (List <StatisticsOnPassengerTrainByMonth> passsenger, List<StatisticsOnCommuterTrainByMonth> commuter, List<StatisticsOnFreightTrainByMonth> freight){
        JRBeanCollectionDataSource passengerDataSource = new JRBeanCollectionDataSource(passsenger);
        JRBeanCollectionDataSource commuterDataSource = new JRBeanCollectionDataSource(commuter);
        JRBeanCollectionDataSource freightDataSource  = new JRBeanCollectionDataSource(freight);

        Map <String, Object> parametr = new HashMap<String, Object>();
        parametr.put("PassengerDataSource", passengerDataSource);
        parametr.put("CommuterDataSource", commuterDataSource);
        parametr.put("FreightDataSource", freightDataSource);

        try {
            jasperPrintDeparture = JasperFillManager.fillReport((FacesContext.getCurrentInstance().getExternalContext().getRealPath("/ReportRoadDeparture.jasper")), parametr, new JREmptyDataSource());
            jasperPrintTracking = JasperFillManager.fillReport((FacesContext.getCurrentInstance().getExternalContext().getRealPath("/ReportRoadTracking.jasper")), parametr, new JREmptyDataSource());
            multipageLinking(jasperPrintDeparture, jasperPrintTracking);
            JasperExportManager.exportReportToPdfFile(jasperPrintDeparture, FacesContext.getCurrentInstance().getExternalContext().getRealPath("/report1.pdf"));
        } catch (JRException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("rawtypes")
    private JasperPrint multipageLinking(JasperPrint file1, JasperPrint file2) {
        List pages = file2.getPages();
        for (int count = 0; count <
                pages.size(); count++) {
            file1.addPage((JRPrintPage) pages.get(count));
        }

        return file1;
    }
}

报告图片如下

enter image description here

1 个答案:

答案 0 :(得分:0)

贝克扬,你的方法没有帮助。结果是一样的。我发现解决方案是我的问题。对于每个jasperPrint变量,您必须创建新的JRBeanCollectionDataSource。我的代码如下。

public void createRoadReport (List <StatisticsOnPassengerTrainByMonth> passsenger, List<StatisticsOnCommuterTrainByMonth> commuter, List<StatisticsOnFreightTrainByMonth> freight, String dateReport) throws JRException{
    List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();

    for (int i = 0; i < 3; i++) {
        JRBeanCollectionDataSource passengerDataSource = new JRBeanCollectionDataSource(passsenger);
        JRBeanCollectionDataSource commuterDataSource = new JRBeanCollectionDataSource(commuter);
        JRBeanCollectionDataSource freightDataSource  = new JRBeanCollectionDataSource(freight);

        Map <String, Object> parametr = new HashMap<String, Object>();
        parametr.put("PassengerDataSource", passengerDataSource);
        parametr.put("CommuterDataSource", commuterDataSource);
        parametr.put("FreightDataSource", freightDataSource);
        parametr.put("date", dateReport);
        switch (i) {
        case 0:
            jasperPrintDeparture = JasperFillManager.fillReport((FacesContext.getCurrentInstance().getExternalContext().getRealPath("/ReportRoadDeparture.jasper")), parametr, new JREmptyDataSource());
            jasperPrintList.add(jasperPrintDeparture);
        break;
        case 1:
            jasperPrintTracking = JasperFillManager.fillReport((FacesContext.getCurrentInstance().getExternalContext().getRealPath("/ReportRoadTracking.jasper")), parametr, new JREmptyDataSource());
            jasperPrintList.add(jasperPrintTracking);
        break;
        case 2:
            jasperPrintArrival = JasperFillManager.fillReport((FacesContext.getCurrentInstance().getExternalContext().getRealPath("/ReportRoadArrival.jasper")), parametr, new JREmptyDataSource());
            jasperPrintList.add(jasperPrintArrival);
        break;
        }
    }

        //multipageLinking(jasperPrintDeparture, jasperPrintTracking);
        //JasperExportManager.exportReportToPdfFile(jasperPrintArrival, FacesContext.getCurrentInstance().getExternalContext().getRealPath("/report.pdf"));
        JRPdfExporter exporter = new JRPdfExporter();
        exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList)); //Set as export input my list with JasperPrint s
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(FacesContext.getCurrentInstance().getExternalContext().getRealPath("/report.pdf"))); //or any other out streaam
        SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
        configuration.setCreatingBatchModeBookmarks(true); //add this so your bookmarks work, you may set other parameters
        exporter.setConfiguration(configuration);
        exporter.exportReport();

}