1

我想使用 xml 数据源来创建动态 jasper 报告。但是在运行程序时出现 MalformedURLException 异常。

xml 文件与 ireport 一起使用没有任何问题。但不适用于动态碧玉。我尝试了互联网上给出的解决方案。但他们都没有解决我的问题。

这是xml代码:

<employees>
<employee>
<id>1001</id>
<name>John</name>
<email>john@somecorp.com</email>
<salary>2000</salary>
</employee>

<employee>
<id>1002</id>
<name>David</name>
<email>davida@somecorp.com</email>
<salary>1000</salary>
</employee>

<employee>
<id>1003</id>
<name>Susan</name>
<email>susana@somecorp.com</email>
<salary>4000</salary>
</employee>

<employee>
<id>1004</id>
<name>Susan</name>
<email>susanb@somecorp.com</email>
<salary>2000</salary>
</employee>

<employee>
<id>1005</id>
<name>David</name>
<email>davidb@somecorp.com</email>
<salary>3000</salary>
</employee>            

这是一个例外:

线程“主”net.sf.jasperreports.engine.JRException 中的异常:java.net.MalformedURLException

这是我用来生成报告的代码:

public class ReportPrint {

public static void main(String[] args) throws JRException, IOException {

    JasperReport jasperReport = JasperCompileManager.compileReport(ReportPrint.class.getResourceAsStream("data.x‌​ml"));
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,new HashMap(), new JREmptyDataSource());
    JasperExportManager.exportReportToPdfFile(jasperPrint, "sample.pdf");

  }

public ReportPrint(){

    try {
        DynamicReportBuilder dynamicReportBuilder = new DynamicReportBuilder();

        // configure report-level settings
        dynamicReportBuilder.setReportName("Some");

        dynamicReportBuilder.setPageSizeAndOrientation(Page.Page_Letter_Landscape());

        // add id column to report
        ColumnBuilder columnBuilderID = ColumnBuilder.getNew();

        columnBuilderID.setTitle("ID");
        columnBuilderID.setWidth(180);
        columnBuilderID.setFixedWidth(true);
        columnBuilderID.setColumnProperty("ID", Integer.class.getName(), "@id");

        dynamicReportBuilder.addColumn(columnBuilderID.build());

        // add name column to report
        ColumnBuilder columnBuilderName = ColumnBuilder.getNew();

        columnBuilderName.setTitle("Name");
        columnBuilderName.setWidth(180);
        columnBuilderName.setFixedWidth(true);
        columnBuilderName.setColumnProperty("Name", String.class.getName(), "@name");

        dynamicReportBuilder.addColumn(columnBuilderName.build());

        // add email column to report
        ColumnBuilder columnBuilderEmail = ColumnBuilder.getNew();

        columnBuilderEmail.setTitle("Email");
        columnBuilderEmail.setWidth(180);
        columnBuilderEmail.setFixedWidth(true);
        columnBuilderEmail.setColumnProperty("Email", String.class.getName(), "@email");

        dynamicReportBuilder.addColumn(columnBuilderEmail.build());

        // add salary column to report
        ColumnBuilder columnBuilderSalary = ColumnBuilder.getNew();

        columnBuilderSalary.setTitle("Salary");
        columnBuilderSalary.setWidth(180);
        columnBuilderSalary.setFixedWidth(true);
        columnBuilderSalary.setColumnProperty("Salary", Integer.class.getName(), "@salary");

        dynamicReportBuilder.addColumn(columnBuilderSalary.build());

        DynamicReport dynamicReport = dynamicReportBuilder.build();

        // build a datasource representing the XML file
        JRDataSource dataSource = new JRXmlDataSource(new File("data.xml"), "//employee");

        // build JasperPrint instance, filling the report with data from datasource created above
        JasperPrint jasperPrint = DynamicJasperHelper.generateJasperPrint(
            dynamicReport, new ClassicLayoutManager(), dataSource, new HashMap<String, Object>());

        // export to pdf
        String pdfFile = Math.round(Math.random() * 100000) + ".pdf";

        JRExporter exporter = new JRPdfExporter();

        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, pdfFile);

        exporter.exportReport();

    } catch(JRException e) {
        e.printStackTrace();
    }

}

}

4

0 回答 0