JSP 页面中的 Jasper 报表

发布于 2024-09-14 08:15:35 字数 154 浏览 4 评论 0原文

如何在JSP页面中显示jasper报告?我使用iReport1.3.3工具来创建
报告。我正在努力在 JSP 页面中显示 jasper 报告。

是否可以将 ArrayList 传递给 jasper 报告?

我需要以 PDF 和 EXcel 格式显示报告。

How to display jasper reports in JSP page? I am using iReport1.3.3 tool to create
reports. I am struggling to display jasper report in JSP page.

Is it possible to pass ArrayList to jasper reports?

I need to display the report in PDF and EXcel format.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜雨飘雪 2024-09-21 08:15:35

我编写了一个 struts (1.1) 应用程序来呈现 PDF 和 CSV。我会在操作处理程序中执行此操作:

public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    response.setContentType("application/pdf");
    OutputStream out = response.getOutputStream();
    try {
        // generate the PDF
    } finally {
        out.close();
    }
    return null;
 }

更新:将集合提供给 JasperReports

package reports;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import java.util.Arrays;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.JRException;

public class CollectionDataSource implements JRDataSource {
    private Iterator iterator = null;
    private Object current = null;

    public CollectionDataSource(Collection col) {
        if (col != null) {
            iterator = col.iterator();
        }
    }

    public CollectionDataSource(Object array[]) {
        this(Arrays.asList(array == null ? new Object[0] : array));
    }

    public boolean next() throws JRException {
        if (iterator == null || !iterator.hasNext()) {
            return false;
        } else {
            current = iterator.next();
            return true;
        }
    }

    public Object getFieldValue(JRField field) throws JRException {
        if ("this".equals(field.getName())) {
            return current;
        } else if (current == null) {
            return null;
        } else {
            Class<?> clazz = current.getClass();
            char chars[] = field.getName().toCharArray();
            chars[0] = Character.toUpperCase(chars[0]);
            String name = new String(chars);
            Method method = null;
            try {
                method = clazz.getMethod("get" + name);
            } catch (NoSuchMethodException e) {
                if (field.getValueClass() == Boolean.class) {
                    try {
                        method = clazz.getMethod("is" + name);
                    } catch (NoSuchMethodException e1) {
                    }
                }
            }
            if (method == null) {
                throw new JRException("No getter for field " + name);
            }
            try {
                return method.invoke(current);
            } catch (Exception e) {
                throw new JRException("Exception in getter of " + name, e);
            }
        }
    }
}

I have written an struts (1.1) application that renders PDFs and CSVs. I would do this in an action handler:

public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    response.setContentType("application/pdf");
    OutputStream out = response.getOutputStream();
    try {
        // generate the PDF
    } finally {
        out.close();
    }
    return null;
 }

UPDATE: feeding collections to JasperReports

package reports;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import java.util.Arrays;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.JRException;

public class CollectionDataSource implements JRDataSource {
    private Iterator iterator = null;
    private Object current = null;

    public CollectionDataSource(Collection col) {
        if (col != null) {
            iterator = col.iterator();
        }
    }

    public CollectionDataSource(Object array[]) {
        this(Arrays.asList(array == null ? new Object[0] : array));
    }

    public boolean next() throws JRException {
        if (iterator == null || !iterator.hasNext()) {
            return false;
        } else {
            current = iterator.next();
            return true;
        }
    }

    public Object getFieldValue(JRField field) throws JRException {
        if ("this".equals(field.getName())) {
            return current;
        } else if (current == null) {
            return null;
        } else {
            Class<?> clazz = current.getClass();
            char chars[] = field.getName().toCharArray();
            chars[0] = Character.toUpperCase(chars[0]);
            String name = new String(chars);
            Method method = null;
            try {
                method = clazz.getMethod("get" + name);
            } catch (NoSuchMethodException e) {
                if (field.getValueClass() == Boolean.class) {
                    try {
                        method = clazz.getMethod("is" + name);
                    } catch (NoSuchMethodException e1) {
                    }
                }
            }
            if (method == null) {
                throw new JRException("No getter for field " + name);
            }
            try {
                return method.invoke(current);
            } catch (Exception e) {
                throw new JRException("Exception in getter of " + name, e);
            }
        }
    }
}
没有你我更好 2024-09-21 08:15:35

似乎有一个 DefaultJasperViewer.jsp,在 http://jasperforge.org/plugins/espforum/view.php?group_id=112&forumid=102&topicid=35938

我认为编写一个 taglib 会更好。看一下这里:http://seamframework.org/Community/JasperReportsSeam
这与 JSF 和 Seam 相关,但可能会给一些启发。

There seems to be a DefaultJasperViewer.jsp, it is mentioned on http://jasperforge.org/plugins/espforum/view.php?group_id=112&forumid=102&topicid=35938

I think it would be nicer to write a taglib. Take a look here: http://seamframework.org/Community/JasperReportsSeam
This is related to JSF and Seam, but might give some inspiration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文