excel报表工具FineReport之模板参数与参数表单

2025-10-24 06:50:35

上一节中我们实现了参数在上、工具栏在中、报表在下的布局方式,可以看到此时的参数界面我们没有采用FineReport内置的参数面板,而是自己写了一个参数表单。此时每个参数我们都需要定义其相应的input元素,若模板中参数比较多的时候,就需要写大段的代码。我们如何读取模板中的所有参数并自动生成form表单呢?

工具/原料

FineReport

1. 实现原理

由二次开发文档可知,我们可以通过getParameters()来获取模板的所有参数,对每个参数我们将其名称赋值给input元素的name属性,那么只要遍历每个参数,就能自动生成对应的input元素了。

2.实现步骤

2.1 模板准备

我们直接使用doc\Primary\Parameter\Parameter.cpt模板,但该模板只有一个数据集参数:地区,为了体现效果,我们再增加一个报表参数:NEW,如下图

excel报表工具FineReport之模板参数与参数表单

因为参数界面与工具栏都是自定义的,因此,我们先去掉报表的内置参数界面与工具栏。

点击参数界面,在参数设计界面点击右上角编辑按钮,如下图,弹出显示参数窗口与点击查询前不显示报表内容选项,选择取消显示参数窗体。

excel报表工具FineReport之模板参数与参数表单

2.2 自定义页面

定义自定义页面Autopara.jsp,代码如下

<%@ page language="java" contentType="text/html; charset=gb2312"    

    pageEncoding="UTF-8"%>    

<%@ page import="com.fr.base.*"%>    

<%@ page import="com.fr.io.TemplateWorkBookIO"%>    

<%@page import="com.fr.main.TemplateWorkBook"%>    

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">    

<html>    

    <head>    

        <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />    

        <title>读取模板参数列表自动生成参数表单</title>    

    </head>    

    <body>    

        <form    

            action="/WebReport/ReportServer?reportlet=doc/Primary/Parameter/Parameter.cpt&__showtoolbar__=false"    

            method="post" target="reportFrame" name="FRform">    

            <%    

                try {    

                    TemplateWorkBook workbook = null;   

                    // 读取当前环境下的模板并读取模板中的所有参数    

                   workbook = TemplateWorkBookIO.readTemplateWorkBook(        

                                FRContext.getCurrentEnv(), "\\doc\\Primary\\Parameter\\Parameter.cpt");     

                    Parameter[] paras = workbook.getParameters();   

                    // 遍历参数并生成对应的input元素    

                    for (int i = 0; i < paras.length; i++) {    

                        out.println("<script>document.write(\""    

                                + paras[i].getName() + ":<input type='text' name='"    

                                + paras[i].getName() + "' value='"    

                                + paras[i].getValue().toString()    

                                + "'/>\");</script>");    

                    }    

                } catch (Exception e) {    

                    e.printStackTrace();    

                }    

            %>    

            <input type=submit name="submit" value="查询">    

        </form>    

        <div id="toolbar">    

            <button type="button"    

                onclick="document.getElementById('reportFrame').contentWindow.contentPane.gotoFirstPage()">    

                首页    

            </button>    

            <button type="button"    

                onclick="document.getElementById('reportFrame').contentWindow.contentPane.gotoPreviousPage()">    

                上一页    

            </button>    

            <button type="button"    

                onclick="document.getElementById('reportFrame').contentWindow.contentPane.gotoNextPage()">    

                下一页    

            </button>    

            <button type="button"    

                onclick="document.getElementById('reportFrame').contentWindow.contentPane.gotoLastPage()">    

                末页    

            </button>    

        </div>    

        <iframe id="reportFrame" name="reportFrame" frameborder="1"    

            src="/WebReport/ReportServer?reportlet=doc/Primary/Parameter/Parameter.cpt&__showtoolbar__=false"    

            width=100% height=80%></iframe>    

    </body>    

<html>   

3. 预览效果

将该Autopara.jsp保存至Web服务器的报表工程如\WebReport\下面,启动服务器,在浏览器中输入如

http://localhost:8080/WebReport/Autopara.jsp,就可以看到自动生成表单的效果了,如下图:

注意:由于需要通过java来获取模板的参数,因此不能使用普通的html页面;另外FineReport内置的jetty服务器无法解析jsp页面,请使用其他Web服务器,如Tomcat服务器。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢