Vaadin与JasperReports?

时间:2014-05-20 17:16:50

标签: java jasper-reports vaadin7

我正在寻找一种解决方案,使用JasperReports为我的应用程序创建报告。我发现了一些例子,但仍然无法使其发挥作用。我正在使用Vaadin7

我正在尝试这个

public class Report {

public Report(){
    createShowReport();
}

private void createShowReport(){
    final Map map = new HashMap();

    StreamResource.StreamSource source = new StreamResource.StreamSource() {
        public InputStream getStream() {
            byte[] b = null;
            try {
                b = JasperRunManager.runReportToPdf(getClass().getClassLoader().getResourceAsStream("br/ind/ibg/reports/report3.jasper"), map, new JREmptyDataSource());
            } catch (JRException ex) {
               ex.printStackTrace();
            }

            return new ByteArrayInputStream(b);
        }
    };


    StreamResource resource = new StreamResource(source, "report3.pdf");
    resource.setMIMEType("application/pdf");

    VerticalLayout v = new VerticalLayout();
    Embedded e = new Embedded("", resource);
    e.setSizeFull();
    e.setType(Embedded.TYPE_BROWSER);
    v.addComponent(e);

    Window w = getWindow();
    w.setContent(v);
    UI.getCurrent().addWindow(w);
}


private Window getWindow(){
    Window w = new Window();
    w.setSizeFull();
    w.center();
    return w;
}

}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

问题似乎出在JasperPrint printer = JasperFillManager.fillReport(file, parametros,dados);行。

确保找到您的报告(file不为空)。

为了显示报告,我通常做的是将结果pdf放在流中,然后用streamResource创建mimeType='application\pdf'并使用window.open(resource)来显示它。

示例:

StreamResource.StreamSource source = new StreamResource.StreamSource() {

                public InputStream getStream() {
                    byte[] b = null;
                    try {
                        b = JasperRunManager.runReportToPdf(getClass().getClassLoader().getResourceAsStream("reports/report3.jasper"), map, con);
                    } catch (JRException ex) {
                       ex.printStackTrace();
                    }

                    return new ByteArrayInputStream(b);
                }
            };

            StreamResource resource = new StreamResource(source, "report3.pdf", getApplication());
            resource.setMIMEType("application/pdf");

            getApplication().getMainWindow().open(resource, "_new");