[Report] - Jasper Report & Oracle BI Publisher

February 04, 2021 |

 

=========================================================================
I. Jasper Report

1. Require

IReport tool: https://community.jaspersoft.com/project/ireport-designer
JDK 1.7: https://www.oracle.com/java/technologies/javase/javase7-archive-downloads.html

If your PC has already installed many JDK, we can addressed the JDK on IReport Config:
1. Go to C:\Program Files (x86)\Jaspersoft\iReport-5.6.0\etc\ireport.conf
2. Edit this file:
    jdkhome="C:/Program Files/Java/jdk1.7.0_80"


2. Ant compile Jaser Report
Ref: http://jasperreports.sourceforge.net/sample.reference/antcompile/index.html

<target name="compile1"> 
  <mkdir dir="./build/reports"/> 
  <jrc 
    srcdir="./reports"
    destdir="./build/reports"
    tempdir="./build/reports"
    keepjava="true"
    xmlvalidation="true">
   <classpath refid="runClasspath"/>
   <include name="**/*.jrxml"/>
  </jrc>
</target> 
<target name="compile2">
  <mkdir dir="./build/reports"/> 
  <jrc 
    destdir="./build/reports"
    tempdir="./build/reports"
    keepjava="true"
    xmlvalidation="true">
   <src>
    <fileset dir="./reports">
     <include name="**/*.jrxml"/>
    </fileset>
   </src>
   <classpath refid="runClasspath"/>
  </jrc> 
</target> 

3. Jasper Report Java Code

Export Excel Report

/**
 * Export to Excel files.
 * @param response
 * @throws IOException
 * @throws JRException
 */
 public void exportReportToExcel(HttpServletRequest request, HttpServletResponse response) throws IOException, JRException {
  JasperPrint jasperPrint = print();
  String fileName = getExportFileName(types[1]);
  
  response.setContentType("application/vnd.ms-excel; charset=" + Constant.ENCODING);
  response.setHeader("Content-Disposition","attachment;filename=" + fileName);
  
  final OutputStream outputStream = response.getOutputStream();
  
  JRXlsExporter exporter = new JRXlsExporter();
  exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
  exporter.exportReport();
 }

Export HTML Report

/**
 * Export to HTML files.
 * @param response
 * @throws IOException
 * @throws JRException
 */
 public void exportReportToHTML(HttpServletRequest request, HttpServletResponse response) throws IOException, JRException {
  JasperPrint jasperPrint = print();
  
  response.setContentType("text/html");
  response.setHeader("Content-disposition", "inline");
  
  PrintWriter out = response.getWriter();
  JRHtmlExporter exporter = new JRHtmlExporter();
  request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_LIST_SESSION_ATTRIBUTE, jasperPrint);
  
  exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
  exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "/resources/ireport/html_files/");
  
  exporter.exportReport();
 }

Note: With HTML to view on the web browser, you should have "px" file from IReport. You can found this file in "html_files/px" after compiling your report in IReport Tool.

Export PDF Report

 /**
 * Export to PDF files.
 * @param response
 * @throws IOException
 * @throws JRException
 */
 public void exportReportToPDF(HttpServletResponse response) throws IOException, JRException {
  String fileName = getExportFileName(types[0]);
  JasperPrint jasperPrint = print();

  response.setContentType("application/x-pdf");
  response.setHeader("Content-disposition", "inline; filename=" + fileName);

  final OutputStream outputStream = response.getOutputStream();
  JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
 }

4. Jasper Report - Javascript

Download Report by AJAX

 function downloadFile(urlToSend) { 
     var req = new XMLHttpRequest(); 
     req.open("GET", urlToSend, true); 
     req.responseType = "blob"; 
     req.onload = function (event) { 
         var blob = req.response; 
         var fileName = req.getResponseHeader("Header attribute") //if you have the fileName header available 
         var link=document.createElement('a'); 
         link.href=window.URL.createObjectURL(blob); 
         link.download=fileName; 
         link.click(); 
     }; 
 
     req.send(); 
 }


=======================================================================






is updating....

Read more…