Hi! Usually in my struts applications I use the compiled jasper file
(.jasper), without recompiling the .jrxml at each request. I've
created a class named JasperReportExporter:
package utils.report;
import java.sql.Connection;
import java.util.HashMap;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRExportProgressMonitor;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
public class JasperReportExporter implements Runnable, JRExportProgressMonitor {
//Output format. Default: PDF
private int exportFormat = "pdf";
//Path for the .jasper file
private String jasperPath = null;
//.jasper filename
private String jasperFile = null;
//Path for the output file
private String exportPath = null;
//Output filename
private String exportFile = null;
//Report parameters hashmap
private HashMap reportParameters = null;
//Connection to the database
private Connection connection = null;
public void run() {
boolean canrun = true;
String filename = null;
String outputfile = null;
//Performs checks
if(jasperPath != null){
if(jasperFile != null){
filename = jasperPath + jasperFile;
}
else{
canrun = false;
}
}
else{
if(jasperFile != null){
filename = jasperFile;
}
else{
canrun = false;
}
}
if(exportPath != null){
if(exportFile != null){
outputfile = exportPath + exportFile;
}
else{
canrun = false;
}
}
else{
if(exportFile != null){
outputfile = exportFile;
}
else{
canrun = false;
}
}
if(canrun == true){
if(reportParameters == null){
reportParameters = new HashMap();
}
JasperPrint print = null;
if (connection == null) {
//If I set from outside a null connection, I
want to use an empty datasource
print = JasperFillManager.fillReport(filename,
reportParameters,
new JREmptyDataSource());
} else {
print = JasperFillManager.fillReport(filename,
reportParameters,
connection);
}
JRExporter exporter = null;
//Checks for the required export format and
instantiates the correct exporter
if(exportFormat.equals("pdf") == true){
exporter = new JRPdfExporter();
outputfile += ".pdf";
}
if(exportFormat.equals("html") == true){
exporter = new JRHtmlExporter();
outputfile += ".html";
}
if(exportFormat.equals("xls") == true){
exporter = new JRXlsExporter();
outputfile += ".xls";
}
if(exportFormat.equals("csv") == true){
exporter = new JRCsvExporter();
outputfile += ".csv";
}
//Sets report parameters, run report and create the
output
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outputfile);
exporter.setParameter(JRExporterParameter.JASPER_PRINT,
print);
exporter.setParameter(JRExporterParameter.PROGRESS_MONITOR, this);
exporter.exportReport();
}
else{
System.out.println("JasperReportExporter : export
failed. Please
fill the jasperFile and the exportFilename properties");
}
}
public void afterPageExport() {
}
//GETTERS AND SETTERS FOR PRIVATE PROPERTIES OMITTED
}
Then, from my action classes I instantiate a JasperReportExporter,
then I set all JasperReportExporter properties, finally I invoke the
run method
Hope this helps
Bye, Marco ;)
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]