Friday, November 13, 2009

Java Report Generation


 iReport 
                   Report generation is process of fetching data from database and displaying those data to Users. It may be with some period of time like weekly, monthly.etc.. Report generation is used to view historical data.
iReport is the free, open source report designer for Jasper Reports, available for all major operating systems under the GNU General Public License. Use iReport to create very complex layouts containing charts, images, sub reports, crosstabs and much more. Access your data through JDBC, Table Models, JavaBeans, XML, Hibernate, CSV, and custom sources. Then publish your reports as PDF, RTF, XML, XLS, CSV, HTML, XHTML, text, DOCX, or Open Office.


Example:- 


   


/* Datewise Report 
 * 
 */
package Package;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.sql.*;
import javax.swing.JDialog;
import javax.swing.ProgressMonitor;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.event.*;


import java.awt.event.*;


import net.sf.jasperreports.engine.JRExporterParameter;
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.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;


class Datevicereport extends JDialog  {
private boolean isExitOnClose = false;


ProgressMonitor conso;


int counter = 0;


Datevicereport() {
setTitle("Datewise Report");

Report();
this.dispose();
}



public void Report() {
try {
/*
* String jdbcString="jdbc:mysql://"; InetAddress address=null;
* address = InetAddress.getLocalHost(); String Ipaddr =
* address.getHostAddress(); String databaseName =
* jdbcString.concat(Ipaddr).concat("/SinglePhase");
*/
String databaseName = "jdbc:mysql://localhost/singlephase";
String userName = "root";
String password = "";
String reportFile = "DateReport.jrxml";
runReport(databaseName, userName, password, reportFile);
} catch (Exception e) {


}
}


public Connection connectDB(String databaseName, String userName,
String password) {
Connection jdbcConnection = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
jdbcConnection = DriverManager.getConnection(databaseName,
userName, password);
} catch (Exception ex) {
String connectMsg = "Could not connect to the database: "
+ ex.getMessage() + " " + ex.getLocalizedMessage();
System.out.println(connectMsg);
}
return jdbcConnection;
}


public void runReport(String databaseName, String userName,
String password, String reportFile) {
try {
JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
JasperReport jasperReport = JasperCompileManager
.compileReport(jasperDesign);
Connection jdbcConnection = connectDB(databaseName, userName,
password);
JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport, null, jdbcConnection);
JasperViewer.viewReport(jasperPrint, isExitOnClose);
OutputStream ouputStream    = new FileOutputStream(new File("C:/catalog.xls"));
ByteArrayOutputStream byteArrayOutputStream    = new ByteArrayOutputStream();
JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT,jasperPrint);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,byteArrayOutputStream);
exporterXLS.exportReport();
ouputStream.write(byteArrayOutputStream.toByteArray()); ouputStream.flush();ouputStream.close();
exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); 
exporterXLS.setParameter(JRXlsExporterParameter.IS_AUTO_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) 
{
String connectMsg = "Could not create the report "
+ ex.getMessage() + " " + ex.getLocalizedMessage();
System.out.println(connectMsg);
}
}


}

Download link:-
    http://jasperforge.org/plugins/project/project_home.php?projectname=ireport


JFreeReport


JFreeReport is a free Java class library for generating reports (GNU LGPL). Includes support for headers, footers, grouping, report functions, print preview, export to PDF and more. Complete source code is included, subject to the GNU LGPL.
Download link:-












0 Comments:

Post a Comment

The Link Exchange - Your ultimate resource for link exchange!

About This Blog

  © Blogger template Webnolia by Ourblogtemplates.com 2009

Back to TOP