Sample Jasper Report (Design with IReport & Code Sample)

Alhamdulillah, tutorial “Sample Jasper Report (Design with IReport & Code Sample)” ini dibuat juga. Tutorial ini saya tujukan bagi pengguna JAVA menengah yang hendak membuat template dengan jasper & export ke format Excel & PDF.

Tutorial ini akan mensimulasikan bagaimana :

  • Bagaimana membuat Template Report Jasper menggunakan Tools IReport.
  • Export Report ke Format Excel & PDF.

Saya menggunakan :

  • Jdk-7u10-windows-x64 (windows 7 64 bit)
  • IDE netbeans 7.1.1.
  • IReport 5.0.1.
  • Jasperreports-4.7.1.
  • MySQL (xampp-win32-1.7.3).

Hal yang perlu diperhatikan :

  • Harus sudah memahamai JAVA Basic.
  • Harus sudah memahami koneksi JAVA & Database (contoh disini menggunakan MySQL).

Untuk Sample Source Code :

[code language=”java”]

try {
// create & open connection
String url = "jdbc:mysql://localhost/db_jasper";
String username = "root";
String password = "";
try (Connection connection = (Connection) DriverManager.getConnection(url, username, password)) {
JasperDesign jasperDesign = JRXmlLoader.load("file\report1.jrxml";);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, connection);

// export to pdf
OutputStream outputStream = new FileOutputStream(new File("file\export_result_in_pdf.pdf";));
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);

// export to xls
outputStream = new FileOutputStream(new File("file\export_result_in_excel.xls";));
JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, outputStream);
exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporterXLS.exportReport();
} catch (Exception ex) {
Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
}
[/code]

Saya mengizinkan untuk semua downloader tutorial ini untuk menyebarluaskannya, tetapi yang pasti dengan tetap mencantumkan nama pembuat-nya.

Salam dari penulis

Muhammad arif nasution

Download tutorial beserta source code disini atau slideshare

Leave a Reply

Your email address will not be published. Required fields are marked *

Afiseaza emoticoanele Locco.Ro