2017-10-29 86 views
0

在當前我運行我的pdf報告使用JasperReports點擊報告按鈕時PDF報告與另一個瀏覽器選項卡運行,但現在我想運行報告在我的JSP頁面上使用iframe標記。請幫我查看/嵌入我的網頁上的碧玉報告.................................如何在iframe中顯示JasperReports pdf文件?

每個人都可以舉個例子。

HTML代碼

<div class="col-md-12"> 
       <!-- begin panel --> 
       <div class="panel panel-inverse" data-sortable-id="form-stuff-2"> 
        <div class="panel-heading"> 
         <div class="panel-heading-btn"> 
          <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-default" data-click="panel-expand"><i class="fa fa-expand"></i></a> 
         </div> 
         <h4 class="panel-title">Ship Service Report Page </h4> 
        </div> 
         <div class="panel-body"> 
         <form class="form-horizontal"> 

          <div class="form-group"> 
           <label class="col-md-2 control-label">Ship Name</label> 
           <div class="col-md-4"> 
            <select name="" class="form-control" id="shipNam" name="shipNam"> 
             <option value="" selected="selected">Select Ship name</option> 
             <s:iterator value="reportInfos" status="rowstatus"> 
             <option value="<s:property value='shipNo' />"><s:property value='shipName' /></option> 
             </s:iterator> 
            </select> 
           </div> 
            </div> 
          <div class="form-group"> 
           <label class="col-md-2 control-label">From Date</label> 
           <div class="col-md-3"> 
            <input type="text" class="form-control" name="fromDate" id="fromDate" placeholder="Select Date" value="" /> 
           </div> 
          </div> 
          <div class="form-group"> 
           <label class="col-md-2 control-label">To Date</label> 
           <div class="col-md-3"> 
            <input type="text" class="form-control" name="toDate" id="toDate" placeholder="Select Date" value="" /> 
           </div> 
          </div> 

          <div class="form-group col-md-6"> 
           <label class="col-md-4 control-label">Report Format</label> 
           <div class="col-md-8"> 
            <label class="radio-inline"> 
             <input type="radio" name="optionsRadios" id="option1" value="pdf" checked /> 
             PDF 
            </label> 
            <label class="radio-inline"> 
             <input type="radio" name="optionsRadios" id="option2" value="xls" /> 
             Excel 
            </label> 
           </div> 
          </div> 
          <div class="form-group">         
           <div class="col-md-6"> 
            <button type="button" class="btn btn-sm btn-success" onclick="getShipServiceReport('shipNam','toDate' , 'fromDate')">View</button> 
           </div> 
          </div> 
         </form> 
        </div> 
      </div> 
       <!-- end panel --> 
     </div> 

我的js函數/報告調用函數

function getShipServiceReport(ship_no,to_date, fromDate){ 
     var shipNos=document.getElementById(ship_no).value; 
     var toDate=document.getElementById(to_date).value; 
     var fromDate=document.getElementById(fromDate).value; 
     var fileType=$('input[name="optionsRadios"]:checked').val(); 
     var ajaxURL = "getShipServiceReportRT.do?ship_no="+shipNos+"&to_date="+toDate+"&fileType="+fileType+"&fromDatee="+fromDate;   
     window.open(ajaxURL,'_blank'); 
//$("#documentArea.html('<iframe src='"+ajaxURL+"' frameBorder='0' style='height:1200px;width:100%;'></iframe>')"); 

    } 

的Java Action類

public void getShipServiceReport()throws JRException, ServletException, IOException{ 
    Map<String, Object> parameters = new HashMap<String, Object>(); 
    parameters.put("shipNo", ship_no); 
    parameters.put("fromDate", fromDatee); 
    parameters.put("toDate", to_date); 
    System.out.println("Parameter"+ship_no); 
    String filename="shipServiceInformation.jasper"; 
    String reportType=fileType; 
    showReport("",parameters,filename,reportType); 

    } 

public static void showReport(String key, Map<String, Object> parameters, String filename, String reportType) 
     throws JRException, ServletException, IOException { 
    Connection connection = null; 
    HttpServletResponse response = ServletActionContext.getResponse(); 
    String outputFileName = "report_" + key; 

    connection = DBC.openCon(); 

    ServletContext context = ServletActionContext.getServletContext(); 
    String path = context.getRealPath("/"); 

    JasperPrint jasperPrint = JasperFillManager.fillReport(path + "jasper" + "/" + filename, parameters, 
      connection); 
    OutputStream ouputStream = response.getOutputStream(); 
    JRExporter exporter = null; 

    if ("pdf".equalsIgnoreCase(reportType)) { 

     response.setContentType("application/pdf"); 
     response.setHeader("Content-disposition", "inline;filename=" + outputFileName + "." + reportType); 
     exporter = new JRPdfExporter(); 
     exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
     exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); 
     System.out.println("Report Created"); 
    } else if ("rtf".equalsIgnoreCase(reportType)) { 

     response.setContentType("application/rtf"); 
     response.setHeader("Content-disposition", "inline;filename=" + outputFileName + "." + reportType); 
     exporter = new JRRtfExporter(); 
     exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
     exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); 

    } else if ("html".equalsIgnoreCase(reportType)) { 

     exporter = new JRHtmlExporter(); 
     exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
     exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); 

    } else if ("xls".equalsIgnoreCase(reportType)) { 

     response.setContentType("application/xls"); 
     response.setHeader("Content-disposition", "inline;filename=" + outputFileName + "." + reportType); 
     exporter = new JRXlsExporter(); 
     exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
     exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); 

    } else if ("csv".equalsIgnoreCase(reportType)) { 

     response.setContentType("application/xls"); 
     response.setHeader("Content-disposition", "inline;filename=" + outputFileName + "." + reportType); 
     exporter = new JRCsvExporter(); 
     exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
     exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); 
    } 

    try { 
     // JasperViewer.viewReport(jasperPrint, true); // for direct print 
     exporter.exportReport(); 

    } catch (JRException e) { 
     System.out.println(e); 
    } finally { 
     if (ouputStream != null) { 
      try { 
       ouputStream.close(); 
      } catch (Exception ex) { 
       System.out.println(ex); 
      } 
     } 
    } 
} 

回答

0

只需在「window.open(ajaxURL,'_ blank');」 line

$('#documentArea').html("<iframe name='your_frame_name' src='"+ajaxURL+"' height=100% width=100% > </iframe>")