使用DynamicJasper API在单个报表中生成横向和纵向

时间:2012-09-03 08:06:42

标签: java jasper-reports dynamic-jasper

我的应用程序中有不同的模块。每个模块都可以以excel,word和pdf格式导出。报告格式为 landscape portrait 。现在我需要编译所有这些报告并作为单个导出。但我遇到的问题是,在导出时, DynamicJasper 仅以单一格式显示,即 landscape portrait

如何使用 DynamicJasper API 在单个报告中使用这两种格式显示报告?

2 个答案:

答案 0 :(得分:2)

您可以创建主报告并向其添加子报告,这是主报告的示例以及如何添加子报告:

public DynamicReport buildDynamicReport() throws ReportDocumentGenerationException {
  DynamicReportBuilder drb = new DynamicReportBuilder();
  drb.setDetailHeight(detailHeight)     
  .setMargins(properties.getReportMargins().getTopMargin(), properties.getReportMargins().getBottomMargin(), properties.getReportMargins().getLeftMargin(), properties.getReportMargins().getRightMargin())     
  .setDefaultStyles(null, null, null, getSpoolStyle(SpoolRow.PLAIN_ATTRIBUTE))    
  .setPageSizeAndOrientation(new Page(**/*Your document dimensions and orientation*/**)
  .setColumnsPerPage(1);
   for(ReportDocumentInformationPage page: reportInformation.getPaginas()){
            drb.addConcatenatedReport(getPageSubReport(page.getPageNumber()),new ClassicLayoutManager(),"DS"+page.getPageNumber().toString(),DJConstants.DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_JRDATASOURCE,!page.getPageNumber().equals(1));
            params.put("DS"+page.getPageNumber().toString(), getReportJRDataSource(page.getPageNumber())  );
        }
        drb.setUseFullPageWidth(true);
        DynamicReport dr = drb.build();
        return dr;
    }

这是关于如何创建这些子报告的示例:

*private DynamicReport getPageSubReport(int i) throws ReportDocumentGenerationException {
        try{
            DynamicReportBuilder drb = new DynamicReportBuilder();
            drb.setDetailHeight(detailHeight)
            .setReportName("Reporte"+i)
            .setMargins(properties.getReportMargins().getTopMargin(), properties.getReportMargins().getBottomMargin(), properties.getReportMargins().getLeftMargin(), properties.getReportMargins().getRightMargin())
            .setDefaultStyles(null, null, null, getSpoolStyle(SpoolRow.PLAIN_ATTRIBUTE))    
            .setPageSizeAndOrientation(new Page(**/*Your specific Page dimensions and orientation***/)
            .setColumnsPerPage(1);

            AbstractColumn spoolColumn = ColumnBuilder.getNew()     
            .setColumnProperty("value", String.class.getName())     
            .setTitle(null)     
            .setWidth(150)
            .build();       
            spoolColumn.setConditionalStyles(getSpoolConditionalStyle());

            drb.addColumn(spoolColumn);
            drb.setUseFullPageWidth(true);
            drb.addField("attributes", String.class.getName());
            DynamicReport dr = drb.build();
            return dr;
        }catch(ColumnBuilderException cbe){
            cbe.printStackTrace();
            throw new ReportDocumentGenerationException("No se pudo definir correctamente la columna del reporte para la pagina "+i);
        }catch(Exception e){
            e.printStackTrace();
            throw new ReportDocumentGenerationException("No se pudo generar la pagina "+i+" del reporte");
        }
    }*

希望它有所帮助。

答案 1 :(得分:0)

如果您拥有或可以处理JasperPrint对象,则可以设置方向:

final JasperDesign jasperDesign = JRXmlLoader.load(someInputStream);
final JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
final JasperPrint jasperPrint =   jasperPrint = JasperFillManager.fillReport( jasperReport, parameters, dataSourceOrConnection);    
...
jasperPrint.setOrientation(OrientationEnum.LANDSCAPE); // Pick
jasperPrint.setOrientation(OrientationEnum.PORTRAIT);  // One 
...
someJRExporter.exportReport();

您还可以使用JasperPrint对象设置页面宽度和高度。