zoukankan      html  css  js  c++  java
  • JasperReport导出

    设计JasperReport模板 利用iReport设计工具设计

     

    比如导出到html中 利用iFrame 标签引入

     try{
         GroupOpenReportForm grouopopenreportForm=(GroupOpenReportForm)form;
         GroupOpenReportVo  groupopenreportVo=new GroupOpenReportVo();
        PageDataVO pageDataVO = new PageDataVO();
        super.copyPageData2VO(grouopopenreportForm.getPageData(),pageDataVO);
       String countType=grouopopenreportForm.getCountType();
       StatisUnion statisunion=StatisAgent.getBean();
       List list=new ArrayList();
      
      if("day".equals(countType)){
         
       groupopenreportVo.setStartTime(DateUtil.parseDate(grouopopenreportForm.getStartTime())); 
          groupopenreportVo.setEndTime(DateUtil.parseDate(grouopopenreportForm.getEndTime())); 
       Map resultMap=statisunion.getGroupOpenReportByDate(groupopenreportVo, pageDataVO);
        int total = (Integer) resultMap.get("total");
        grouopopenreportForm.getPageData().setTotalRow(total);
        list=(List)resultMap.get("resultList");
        this.getNewList(list);
       }else{
       
       groupopenreportVo.setStrStartTime(getStrDate(grouopopenreportForm.getStartTime())); 
          groupopenreportVo.setStrEndTime(getStrDate(grouopopenreportForm.getEndTime())); 
       Map resultMap=statisunion.getGroupOpenReportByMonth(groupopenreportVo, pageDataVO);
        int total = (Integer) resultMap.get("total");
        grouopopenreportForm.getPageData().setTotalRow(total);
        list=(List)resultMap.get("resultList");
      }
       response.setContentType("text/html;charset=UTF-8");
       //报表参数列表
              HashMap paramsMap = new HashMap();
        Locale local = new Locale(ADCi18n.currentLang);
       // 设置报表的多语言
           paramsMap.put(JRParameter.REPORT_LOCALE, local);
           String fileName = null;
              //报表临时文件路径
              String dir = null;
              //报表临时文件
              String destFile = null;
          String reportTemplatePath=getReportTemplatePath();
          dir = reportTemplatePath.substring(0, reportTemplatePath.indexOf("/WEB-INF")) + "/reportTemp/";
          //装载报表模板对象
                JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportTemplatePath + "groupOpenReport.jasper");
                //报表中的参数列表
               
                JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);
               
              //填充数据
                JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, paramsMap, dataSource);
                //报表HTML格式文件名称
                fileName = System.currentTimeMillis() + ".html";
                destFile = dir + fileName;
                //创建临时文件目录
                getSavePath(dir);

                //导出文件
                JasperExportManager.exportReportToHtmlFile(jasperPrint, destFile);
                String s = request.getServerName() + ":" + request.getServerPort() + request.getContextPath()
                        + "/reportTemp";
                //response.sendRedirect("http://" + s + "/" + fileName);
                request.setAttribute("reportPath", "http://" + s + "/" + fileName);
                request.setAttribute("resultReport", list);
             this.writeSearchLog(list, request);
      }catch(Exception e){
          this.writeSearchLog(null, request);
       throw e;
      }
      
      
      return mapping.findForward("groupOpenCount");

    在jsp页面显示时可以 找到临时页面并且引入进来

    <%@ page contentType="text/html; charset=UTF-8"%>
    <%@ include file="../../jsputil.ini.jsp" %>
    <%@ taglib uri="/WEB-INF/page-pagination.tld" prefix="page"%>
    <script language="javascript" src="../js/bms300.js"></script>
    <html  xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script type="text/javascript" src="../js/jquery.js"></script>
    <script type="text/javascript" src="../js/common.js"></script>
    <script language="javascript" src="../js/bms300.js"></script>
    <script language="javascript" >
      function search()
      {
                    var flag = true;
         flag = (beginTime("startTime","endTime",
          '<adc:i18n key="adc.product.app.query.end.large.than.start"/>!'));
        if ( flag ==true)
        {
        document.forms[0].action="groupOpenReport.do?method=groupOpenReport";
         document.groupOpenReportForm.submit();
        }
      }
      function export1()
      {
        document.forms[0].action="groupOpenReport.do?method=groupOpenReportXls";
        document.forms[0].submit();
      }
    </script>

    </head>
    <body>
    <div class="title"><adc:i18n key="op.pushmail.current_position"/>:<adc:i18n key="adc.log.ecaccess.000005"/> &gt; <adc:i18n key="adc.group.open.report.user.statis"/> &gt; <adc:i18n key="adc.group.open.report.group.open.account"/></div>
    <html:form  action="groupOpenReport.do?method=groupOpenReport" method="post">
        <div class="tab">
       <table>
         <tr>
           <td class="on"><a href="#"><span><adc:i18n key="adc.group.open.report.group.open.account"/></span></a></td>
         </tr>
       </table>
     </div>
        <div class="tabBlock">
          <div class="editBlock">
         <table>
           <tr>
             <td colspan="4" class="subtitle"><%/*查询条件*/%><adc:i18n key="adc.business.000180"/></td>
           </tr>
           <tbody>
             <tr>
               <th><adc:i18n key="adc.group.open.report.statistics.type"/>:</th>
               <td colspan="3">
                 <SELECT  name="countType">
                   <OPTION value="day"  <c:if test="${groupOpenReportForm.countType eq 'day'}">selected</c:if> ><adc:i18n key="adc.group.open.report.daily.statis"/></OPTION>
                   <OPTION value="month" <c:if test="${groupOpenReportForm.countType eq 'month'}">selected</c:if> ><adc:i18n key="adc.group.open.report.month.statis"/></OPTION>
                 </SELECT>
                  </td>
                </tr>
                <tr> 
               <th><adc:i18n key="adc.pushmail.report.statistics.date"/>:</th>
               <td colspan="3">
                 <input id="t1" name="startTime" type="text" onClick="dateSelect('startTime')" value="${groupOpenReportForm.startTime}" class="Wdate" readonly="readonly"  onclick="dateSelect('startTime')" />
             
              <input id="t2" name="endTime" type="text" onClick="dateSelect('endTime')" value="${groupOpenReportForm.endTime}" class="Wdate" readonly="readonly" onclick="dateSelect('endTime')"/>
             
               </td>
             </tr>
             <tr>
               <td colspan="4" class="toolbar">
                 <input name="serch" type="button" class="inputButton2" onclick="search();" value="<adc:i18n key="adc.log.ecaccess.000005"/>"/>
                       <input name="exp" type="button" class="inputButton2" onclick="export1();" value="export"/>
               </td>
             </tr>
           </tbody>
         </table> 
       </div> 
        </div>
       <c:if test="${resultReport != null}">
       <div class="report_div">
          <iframe marginheight=0 rows="100%,*" marginwidth=0 hspace=0 vspace=0 scrolling="no" id="Type" onload="iframeResizable('Type')" frameborder="0" allowtransparency="yes" width="100%"
                src="${reportPath}">
        </iframe>
       </div>
       <div class="newpageui">
         <c:if test="${!empty resultReport}">
         <table class="newpage">
              <tr>
                <td id="pright">
                  <page:pagination name="groupOpenReportForm" property="pageData" />
                </td>
              </tr>
            </table>
            </c:if>
            <c:if test="${resultReport != null && empty resultReport}">
            <table class="newpage">
              <tr>  
                <td>
                  <div class="nodata" style="text-align:center;color:#ff0000;">
                    <adc:i18n key="adc.common.paging.noresult"/>
                  </div>
                </td>
              </tr>
            </table>
            </c:if>
          </div>
          </c:if>
    </html:form>
    </body>
    </html>

    导出pdf之类都可以采用上面的方法

    导出为excel时可以这样

     

     

     public ActionForward groupOpenReportXls(ActionMapping mapping,
       ActionForm form, HttpServletRequest request,
       HttpServletResponse response) throws Exception
     {
      
      try{
          GroupOpenReportForm grouopopenreportForm=(GroupOpenReportForm)form;
          GroupOpenReportVo  groupopenreportVo=new GroupOpenReportVo();
         PageDataVO pageDataVO = new PageDataVO();
         super.copyPageData2VO(grouopopenreportForm.getPageData(),pageDataVO);
        String countType=grouopopenreportForm.getCountType();
        StatisUnion statisunion=StatisAgent.getBean();
        List list=new ArrayList();
       
       if("day".equals(countType)){
          
        groupopenreportVo.setStartTime(DateUtil.parseDate(grouopopenreportForm.getStartTime())); 
           groupopenreportVo.setEndTime(DateUtil.parseDate(grouopopenreportForm.getEndTime())); 
         Map resultMap=statisunion.getGroupOpenReportByDate(groupopenreportVo, pageDataVO);
         int total = (Integer) resultMap.get("total");
         grouopopenreportForm.getPageData().setTotalRow(total);
         list=(List)resultMap.get("resultList");
         this.getNewList(list);
        }else{
        
        groupopenreportVo.setStrStartTime(getStrDate(grouopopenreportForm.getStartTime())); 
           groupopenreportVo.setStrEndTime(getStrDate(grouopopenreportForm.getEndTime())); 
        Map resultMap=statisunion.getGroupOpenReportByMonth(groupopenreportVo, pageDataVO);
         int total = (Integer) resultMap.get("total");
         grouopopenreportForm.getPageData().setTotalRow(total);
         list=(List)resultMap.get("resultList");
       }
            response.setContentType("text/html;charset=UTF-8");
        //报表参数列表
               HashMap paramsMap = new HashMap();
         Locale local = new Locale(ADCi18n.currentLang);
        // 设置报表的多语言
              paramsMap.put(JRParameter.REPORT_LOCALE, local);
              String fileName = null;
               //报表临时文件路径
               String dir = null;
               //报表临时文件
               String destFile = null;
           String reportTemplatePath=getReportTemplatePath();
           dir = reportTemplatePath.substring(0, reportTemplatePath.indexOf("/WEB-INF")) + "/reportTemp/";
           //装载报表模板对象
                 JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportTemplatePath + "groupOpenReport.jasper");
                 //报表中的参数列表
                
                 JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);
                
               //填充数据
                 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, paramsMap, dataSource);
                 ByteArrayOutputStream oStream = new ByteArrayOutputStream();
                
                 JRXlsExporter exporter = new JRXlsExporter();
                
                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);
                 exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE); // 删除记录最下面的空行
                 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);// 删除多余的ColumnHeader
                 exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);// 显示边框
                 exporter.exportReport();
                
                 byte[] bytes = oStream.toByteArray();
               
                if(bytes != null && bytes.length > 0) {
                 response.reset();
                 response.setContentType("application/vnd.ms-excel");
                 response.setContentLength(bytes.length);
                 ServletOutputStream ouputStream = response.getOutputStream();
                 ouputStream.write(bytes,0,bytes.length);
                 ouputStream.flush();
                 ouputStream.close();

                }else{
               
                }

       }catch(Exception e){
        throw e;
       }
       
       
       return null;
     }

  • 相关阅读:
    C++ 17
    C++ 11
    mysql统计某一个数据库中有几张表
    poj2636---Electrical Outlets(插线板)
    poj2608---几个字母映射到同一个数字
    poj2583---Series Determination
    poj2578---三个数中找出第一个大于168的
    poj2521---lose money
    poj2538---字符匹配(映射)
    poj2509---抽k根烟就换一支,求能抽烟的总数
  • 原文地址:https://www.cnblogs.com/liaomin416100569/p/9331973.html
Copyright © 2011-2022 走看看