Reputation: 313
I already have 6 Jasper Report templates created, with all of the static text fields to be filled out using a SQL query. The SQL query uses 2 parameters that I am passing in: FirstName and LastName. I am also passing in 2 other parameters that will just get added to Report.
This is the code i have so far to pass the HashMap with the parameters to the Report. But I am lost as there isnt really any good documentation or examples that I can find.
package print;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.*;
import java.util.*;
public class PrintCertificate
{
public PrintCertificate(String output, String certType, String firstName, String lastName, String confirmDate, String pastorName)
{
if(certType=="rci_eng")
{
String fileName = "/RCI_Eng.jasper";
output = "C:/Users/User/Desktop/Test/";
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("FirstName",firstName);
map.put("LastName",lastName);
map.put("PastorName", pastorName);
map.put("DateOfConfirmation", confirmDate);
try
{
JasperPrint print = JasperFillManager.fillReport(fileName, map);
JRDocxExporter exporter = new JRDocxExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "test.docx");
exporter.exportReport(print);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}
}
I know this is probably far from correct, but if someone can point me in the right direction of good documentation or examples, or point out what I've done wrong, it would be of great help!
Upvotes: 3
Views: 25280
Reputation: 2598
Here is a brief description to use Jasper Report with your Java Application
In your report inspector you can see all the column names of our query under Fields category and all the parameters defined under Parameters category.
Then you can declare ReportGenarator class to generate your report
public class ReportGenarator {
public static String OUT_PUT = "your_output_file_path/myreport.docx";
public static String REPORT = "your_report_path/myreport.jrxml";
public void genarateReport(String reportPath,
Map<String, Object> map, Connection con) {
try {
JasperReport jr = JasperCompileManager.compileReport(
ClassLoader.getSystemResourceAsStream(reportPath));
JasperPrint jp = JasperFillManager.fillReport(jr, map, con);
JRDocxExporter export = new JRDocxExporter();
export.setExporterInput(new SimpleExporterInput(jp));
export.setExporterOutput(new SimpleOutputStreamExporterOutput(new File(OUT_PUT)));
SimpleDocxReportConfiguration config = new SimpleDocxReportConfiguration();
export.setConfiguration(config);
export.exportReport();
} catch (JRException ex) {
ex.printStackTrace();
}
} }
You can generate your report by Pressing a button in your application.So that you have to include below code inside your button
action event
Map<String, Object> map = new HashMap<>();
map.put("headding", "REPORT FROM DATABASE CONNECTION");//parameter name should be like it was named inside your report.
new ReportGenarator().genarateReport(
ReportGenarator.REPORT, map, your_DB_connction_reference_here);
Upvotes: 4