Jasper Report - How to print pdf, html and excel reports
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
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.JasperManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
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;
public class ReportGeneration {
public static void main(String[] args) {
InputStream inputStream;
JasperReport jasperReport = null;
try {
// First, load JasperDesign from XML and compile it into JasperReport
inputStream = new FileInputStream ("C:/MSC_project/JasperProjects/TestPro2/src/test_jasper_rep1.jrxml");
JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
jasperReport = JasperCompileManager.compileReport(jasperDesign);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Second, create a map of parameters to pass to the report.
Map parameters = new HashMap();
parameters.put("Title", "Basic JasperReport");
parameters.put("MaxSalary", new Double(25000.00));
// Third, get a database connection
// String dbUrl = props.getProperty("jdbc.url");
String dbUrl = "jdbc:mysql://localhost:3306/struts_tiles1";
// String dbDriver = props.getProperty("jdbc.driver");
String dbDriver = "com.mysql.jdbc.Driver";
// String dbUname = props.getProperty("db.username");
String dbUname = "root";
// String dbPwd = props.getProperty("db.password");
String dbPwd = "123456";
Connection conn;
JasperPrint jasperPrint = null;
// Load the JDBC driver
try {
Class.forName(dbDriver);
// Get the connection
conn = DriverManager.getConnection(dbUrl, dbUname, dbPwd);
// Fourth, create JasperPrint using fillReport() ethod
jasperPrint = JasperManager.fillReport(jasperReport,parameters, conn);
// You can use JasperPrint to create PDF
JasperManager.printReportToPdfFile(jasperPrint, "C:/BasicReport.pdf");
JasperExportManager.exportReportToHtmlFile(jasperPrint, "C:/BasicReport.html" );
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "C:/BasicReport.xls" );
exporter.exportReport();
// coding For Excel:
/*ByteArrayOutputStream outputByteArray = new ByteArrayOutputStream();
OutputStream outputfile= new FileOutputStream(new File("C:/BasicReport.xls"));
JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, outputByteArray);
exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
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();
outputfile.write(outputByteArray.toByteArray()); */
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Or to view report in the JasperViewer
JasperViewer.viewReport(jasperPrint);
}
}
To print in to excel sheet we can use step 1 or step 2.But in both instances, chek the jasper report.jar version and add the poi jar version compatible. eg: poi-2.0-final-20040126.jar
step 1
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "C:/BasicReport.xls" );
exporter.exportReport();
step 2
ByteArrayOutputStream outputByteArray = new ByteArrayOutputStream();
OutputStream outputfile= new FileOutputStream(new File("C:/BasicReport.xls"));
JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, outputByteArray);
exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
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();
outputfile.write(outputByteArray.toByteArray());