使用JasperReports在报告中添加超链接

时间:2012-09-21 06:43:47

标签: java jsp jasper-reports scriptlet

我使用 iReport 设计了带有超链接功能的 jrxml 但它不起作用(报告正在显示,但超链接无效)。

我正在使用此 jsp 页面显示报告:

<%
    //JasperPrint jasperPrint = (JasperPrint)session.getAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE);

    String filepath1 = request.getParameter("filepath"); 
    System.out.println("filepath:" + filepath1);
    String reportFile = filepath1;
    JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
    JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
    java.util.Map<String, Object> params = new java.util.HashMap<String, Object>();
    DateFormat formatter;
    Date date, endDate;   
    formatter = new SimpleDateFormat("dd-mm-yyyy");
    date = formatter.parse("11-08-2012");
    endDate = formatter.parse("13-08-2012");
    HashMap<String, Object> values = new HashMap<String, Object>();

        values.put("camp", "tps1"); 
    JdbcTemplate myJdbcTemplate = null;
    net.sf.jasperreports.engine.JasperPrint jasperPrint = null;
    try {
        myJdbcTemplate = DaoFactory.getJdbcTemplate();
        if (myJdbcTemplate == null) {
        }
        //new apps.dbase.DBaseLink().OpenLink()
        jasperPrint = net.sf.jasperreports.engine.JasperFillManager.fillReport(jasperReport, values, myJdbcTemplate.getDataSource().getConnection());
        session.setAttribute("jasperprint", jasperPrint);
        java.io.PrintWriter printWriter = response.getWriter();
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (request.getParameter("reload") != null || jasperPrint == null) {

        values = new HashMap<String, Object>();
        values.put("camp", "tps1");       
        jasperPrint = net.sf.jasperreports.engine.JasperFillManager.fillReport(jasperReport, values, myJdbcTemplate.getDataSource().getConnection());
        session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
    }

    JRHtmlExporter exporter = new JRHtmlExporter();

    int pageIndex = 0;
    int lastPageIndex = 0;
    if (jasperPrint.getPages() != null) {
        lastPageIndex = jasperPrint.getPages().size() - 1;
    }

    String pageStr = request.getParameter("page");
    System.out.println("page::" + pageStr);
    try {
        pageIndex = Integer.parseInt(pageStr);
    } catch (Exception e) {
    }

    if (pageIndex < 0) {
        pageIndex = 0;
    }
    System.out.println("pageindex::" + pageIndex);
    System.out.println("lastpage::" + lastPageIndex);

    if (pageIndex > lastPageIndex) {
        pageIndex = lastPageIndex;
    }
     JasperExportManager.exportReportToHtmlFile(jasperPrint,"/home/tulsi/Desktop/hi.html");
    StringBuffer sbuffer = new StringBuffer();
    PrintWriter out1 = response.getWriter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, sbuffer);
    //exporter.setParameter(net.sf.jasperreports.engine.JRExporterParameter.OUTPUT_WRITER, printWriter);
    exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../servlets/image?image=");
    exporter.setParameter(JRExporterParameter.PAGE_INDEX, Integer.valueOf(pageIndex));
    exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "");
    //exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
    exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "");

    exporter.exportReport();


%>

<html>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <head>
        <script>
            $("a#first").click(function() {
                var i1 = "0" ;
                var f =  $("input#filepath").val();

                $.ajax({
                    url: "viewer.jsp",
                   data:"page="+i1+"&filepath="+f ,
                    success: function(data){
                        $("#please").html(data);
                    }
                });
            });
            $("a#prev").click(function() {
                var i1 = $("input#pageIndex").val();
                var f =  $("input#filepath").val();
                i1 = parseInt(i1) - 1 ;

                $.ajax({
                    url: "viewer.jsp",
                   data:"page="+i1+"&filepath="+f ,
                    success: function(data){
                        $("#please").html(data);
                    }
                });

            });
            $("a#next").click(function() {

                var i1 = $("input#pageIndex").val();
                var f =  $("input#filepath").val();
                i1 = parseInt(i1) + 1 ;

                $.ajax({
                    url: "viewer.jsp",
                    data:"page="+i1+"&filepath="+f ,
                    success: function(data){
                        $("#please").html(data);
                    }
                });
            });
            $("a#last").click(function() {
                var i1 =  $("input#lastIndex").val();
                var f =  $("input#filepath").val();

                $.ajax({
                    url: "viewer.jsp",
                   data:"page="+i1+"&filepath="+f ,
                    success: function(data){
                        $("#please").html(data);
                    }
                });
            });
        </script>
    </head>
    <body text="#000000" link="#000000" alink="#000000" vlink="#000000">
        <div id="please" >
            <input type="hidden" id="pageIndex" value="<%=pageIndex%>" >
            <input type="hidden" id="lastIndex" value="<%=lastPageIndex%>" >
            <input type="hidden" id="filepath" value="<%=reportFile%>" >
            <%
                System.out.print("in ::" + pageIndex);
                if (pageIndex > 0) {
                    System.out.print("in if ");
            %>

            <a href="#"  id="first"  ><img alt="first" src="images/first.png" border="0"></a>
            <a href="#" id="prev"  ><img alt="pre" src="images/prev.png" border="0"></a></td>
                <%
                } else {
                    System.out.print("in else ");
                %>
        <img alt="first" src="images/first.png" border="0">
        <img alt="prev"  src="images/prev.png" border="0">
        <%
            }
            if (pageIndex < lastPageIndex) {
        %>
        <a  href="#" id="next" ><img alt="next" src="images/next.png" border="0"></a></td>
    <a href="#"  id="last"  ><img alt="last" src="images/last.png" border="0"></a></td>
        <%        } else {
        %>
<img alt="next" src="images/next.png" border="0">
<img alt="last" src="images/last.png" border="0">
<%    }
%>
<table  style=" width:200; border-style: solid; border: 1px; border-color: blue; "  >
    <tr><a href="DownloadAll?type=csv&filepath=<%= reportFile%>"  ><button> CSV</button></a></tr>
  <tr><a href="DownloadAll?type=pdf&filepath=<%= reportFile%>"  > <button>PDF</button></a></tr>
  <tr><a href="DownloadAll?type=xls&filepath=<%= reportFile%>"  > <button>XLS</button></a></tr>
  <tr><a href="DownloadAll?type=docx&filepath=<%= reportFile%>"  ><button>DOCX</button></a></tr>
</table>
<div>
    <%=sbuffer.toString()%>
</div>
</div>
</body>
</html>

我认为问题出在 jasperPrint 对象中。

请指导我如何添加超链接功能。

1 个答案:

答案 0 :(得分:0)

经过这么多努力,我得到了这个链接的答案,这非常有用Drill-down with JSP pages