2012-03-22 10 views
2

ireportを使用してjasperレポートを設計しました。jspからそのレポートを呼び出したいので、SQLクエリを使用してレポートを作成しました。ジャスパーをjspで表示する方法

+0

可能な重複:http://stackoverflow.com/questions/3475186/jasper-reports-in-jsp-page&http://stackoverflow.com/questions/3407704/jasperreports-how-to-dispaly-report-in-jsp-page –

答えて

0

jspファイルからボタンまたはイメージをクリックしてサーブレットを呼び出す必要があります。サーブレットで、jrxml(ジャスパーレポート形式)をコンパイルします。そして、サーブレットをダウンロードするコードを書く必要があります。コードの下

は、特定のフォルダに碧玉レポートやエクスポートをコンパイルするために使用されます。コードの下

HashMap<String, Object> response = new HashMap<String, Object>(); 
     String fileName = "C:\\temp\\report.jrxml"; 
     String pdfFile = "C:\\temp\\"; 
     JasperReport mainReport; 
     JasperDesign mainReportDesign; 

     try { 
      mainReportDesign = JRXmlLoader.load(fileName); 
      mainReport = JasperCompileManager.compileReport(mainReportDesign); 
      if(reportFormat.equalsIgnoreCase("PDF")) 
       pdfFile = pdfFile+"report.pdf"; 
      else if(reportFormat.equalsIgnoreCase("CSV")) 
       pdfFile = pdfFile+"report.csv"; 
      JasperPrint jasperPrint = JasperFillManager.fillReport(mainReport, params, new JREmptyDataSource()); 


      if(reportFormat.equalsIgnoreCase("PDF")) 
       JasperExportManager.exportReportToPdfFile(jasperPrint, pdfFile); 
      else if(reportFormat.equalsIgnoreCase("CSV")) { 
       JRExporter exporter = new JRCsvExporter(); 
       exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, pdfFile); 
       exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
       exporter.exportReport(); 
      } 

     } catch (JRException e) { 
      e.printStackTrace(); 
     } 

そのフォルダからそのエクスポートファイルをダウンロードするために使用されます。これはあなたのサーブレットクラスになります。

 File f  = new File("C:\\temp\\report.pdf"); 
     int     length = 0; 
     ServletOutputStream op  = resp.getOutputStream(); 
     ServletContext  context = getServletConfig().getServletContext(); 
     String    mimetype = context.getMimeType(f.getName()); 

     resp.setContentType((mimetype != null) ? mimetype : "application/octet-stream"); 
     resp.setContentLength((int)f.length()); 
     resp.setHeader("Content-Disposition", "attachment; filename="+reportName); 

     byte[] bbuf = new byte[1024]; 
     DataInputStream in = new DataInputStream(new FileInputStream(f)); 

     while ((in != null) && ((length = in.read(bbuf)) != -1)) 
     { 
      op.write(bbuf,0,length); 
     } 

     in.close(); 
     op.flush(); 
     op.close(); 
関連する問題