Last active
December 29, 2015 21:09
-
-
Save imamhidayat92/7728574 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Author: Imam Hidayat (mokhamad.imam[at]students.paramadina.ac.id) | |
Universitas Paramadina | |
Source: https://gist.github.com/imamhidayat92/7728574 | |
This helper class can be used to simplify report generation procedure using Jasper Report 5.5.0. | |
*/ | |
import java.sql.*; | |
import java.util.HashMap; | |
import javax.swing.JFrame; | |
import javax.swing.JOptionPane; | |
import net.sf.jasperreports.engine.JRResultSetDataSource; | |
import net.sf.jasperreports.engine.JasperCompileManager; | |
import net.sf.jasperreports.engine.JasperExportManager; | |
import net.sf.jasperreports.engine.JasperFillManager; | |
import net.sf.jasperreports.engine.JasperPrint; | |
import net.sf.jasperreports.engine.JasperReport; | |
import net.sf.jasperreports.swing.JRViewer; | |
public class JasperReportHelper { | |
public static void exportReportToPdfFile(ResultSet resultSet, String templateLocation, String exportedFileLocation) { | |
try { | |
JasperReport reportTemplate = JasperCompileManager.compileReport(templateLocation); | |
JRResultSetDataSource reportEntries = new JRResultSetDataSource(resultSet); | |
JasperPrint print = JasperFillManager.fillReport(reportTemplate, null, reportEntries); | |
JasperExportManager.exportReportToPdfFile(print, exportedFileLocation); | |
} | |
catch (Exception e) { | |
JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); | |
} | |
} | |
public static void showReport(ResultSet resultSet, String templateLocation, String reportWindowTitle) { | |
try { | |
JasperReport reportTemplate = JasperCompileManager.compileReport(templateLocation); | |
JRResultSetDataSource reportEntries = new JRResultSetDataSource(resultSet); | |
JasperPrint print = JasperFillManager.fillReport(reportTemplate, null, reportEntries); | |
JFrame frame = new JFrame(reportWindowTitle); | |
frame.getContentPane().add(new JRViewer(print)); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
catch (Exception e) { | |
JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); | |
} | |
} | |
public static void showReport(ResultSet resultSet, String templateLocation, HashMap map, String reportWindowTitle) { | |
try { | |
JasperReport reportTemplate = JasperCompileManager.compileReport(templateLocation); | |
JRResultSetDataSource reportEntries = new JRResultSetDataSource(resultSet); | |
JasperPrint print = JasperFillManager.fillReport(reportTemplate, map, reportEntries); | |
JFrame frame = new JFrame(reportWindowTitle); | |
frame.getContentPane().add(new JRViewer(print)); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
catch (Exception e) { | |
JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment