我正在寻找一种使用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;
}


}

任何的想法 ?

最佳答案

问题似乎在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");

关于java - Vaadin与JasperReports?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23766146/

10-16 15:10