Monday 1 August 2016

JASPER REPORT IN JAVA USING NETBEANS


Step 1  : Download iReport plugin here


Step 2  : To Install iReport plugin in Netbeans IDE

               Tools-->Plugin-->Downloaded--->Select ireport Plugin-->Install

Step 3  :   Create Jasper report

                 Newproject----->create Empty report(Student.jrxml)

Step 4 a: Connect Database in Netbeans



 4 b) Create new database and select the jdbc connection






















4 c) 





















4 d) Click the image as shown in below



4 e) Write the query here



4 f) Generate the field here when we write the query in query tab.as shown above



4 g)Drag and Drop the Field in Detail column



Step 5: Create java class

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import net.sf.jasperreports.engine.*;
import com.lowagie.text.Document; 
import com.lowagie.text.Paragraph; 
import com.lowagie.text.pdf.PdfWriter; 
import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.view.JasperViewer;

public class Demo {
    public static void main(String[] args) throws IOException
    {
          try
        {
      
//Specify Your Report Path
JasperReport jasperReport=JasperCompileManager.compileReport("path of your .jrxml file");
        
            Class.forName("com.mysql.jdbc.Driver");

            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db name" , "uname", "password");
Statement stmt = null;
ResultSet rset = null;
String queryString = "query here";
stmt = conn.createStatement();
rset = stmt.executeQuery(queryString);
 JRResultSetDataSource jasperReports = new JRResultSetDataSource(rset);

            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null, jasperReports);
//Report saved in specified path
            JasperExportManager.exportReportToPdfFile(jasperPrint,"file download path.pdf");
//Report open in Runtime
          Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " +"file download path.pdf");
JRXlsExporter exporterXLS = new JRXlsExporter();
OutputStream output = new FileOutputStream(new File("file download path.xls"));
 exporterXLS.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
 exporterXLS.setParameter(JRExporterParameter.OUTPUT_STREAM, output);
 exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_COLLAPSE_ROW_SPAN, Boolean.TRUE);
 exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, Boolean.TRUE);
 exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
 exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
 exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_DETECT_CELL_TYPE, Boolean.FALSE);
 exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
 exporterXLS.setParameter(JRXlsAbstractExporterParameter.IS_IGNORE_GRAPHICS, Boolean.TRUE);
 exporterXLS.exportReport();
            //JasperViewer.viewReport(jasperPrint);

        }
 catch (JRException e)
        {
            e.printStackTrace();
        }
        catch (ClassNotFoundException ex)
        {
            ex.printStackTrace();
        }
        catch (SQLException ex)

        {
            ex.printStackTrace();
        }
    }
}


Step 6 : Required Jar Files


No comments:

Post a Comment