java 如何使用java将参数传递给JasperReport以便稍后在SQL查询中使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33966181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass parameters to JasperReport with java to use later in SQL query
提问by webminer07
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: FirstNameand LastName. I am also passing in 2 other parameters that will just get added to Report.
我已经创建了 6 个 Jasper 报告模板,所有静态文本字段都使用 SQL 查询填写。SQL 查询使用我传入的 2 个参数:FirstName和LastName。我还传递了 2 个其他参数,这些参数将被添加到报告中。
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.
这是我到目前为止将带有参数的 HashMap 传递给报告的代码。但是我迷路了,因为我真的找不到任何好的文档或示例。
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!
我知道这可能远非正确,但是如果有人可以为我指出好的文档或示例的正确方向,或者指出我做错了什么,那将有很大帮助!
回答by Madushan Perera
Here is a brief description to use Jasper Report with your Java Application
以下是在 Java 应用程序中使用 Jasper Report 的简要说明
- First you have to set database connection to the Report.
- 首先,您必须设置与报告的数据库连接。
- Then you can design your SQL query for the report.
You can add parameters to the SQL query in case you need to filter data through your query.Just add parameters using New Parameterbutton and you can drag and drop parameters displaying inside the text area to your query.
In your report inspector you can see all the column names of our query under Fieldscategory and all the parameters defined under Parameterscategory.
在您的报告检查器中,您可以在“字段”类别下看到我们查询的所有列名称以及在“参数”类别下定义的所有参数。
- You can design a basic report like below
By dragging and dropping field names and parameters to your Report Designeryou can design your report as you desire. Title Band(Your Title Here) , Column Header Band(Column Names) , Detail Band(Iterative Data here) and Summary Band(Like Grand_Total,Date,Issued By and chart elements can be added to this section) are enough for basic report. Then you can declare ReportGenaratorclass 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);
- 您可以设计如下的基本报告
通过将字段名称和参数拖放到您的报告设计器中,您可以根据需要设计您的报告。Title Band(Your Title Here) , Column Header Band(Column Names) , Detail Band(Iterative Data here) 和Summary Band(像Grand_Total,Date,Issued By 和图表元素可以添加到这个部分)对于基本报告来说就足够了。 然后你可以声明ReportGenerator类来生成你的报告
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(); } } }
您可以通过按下应用程序中的按钮来生成报告。因此您必须在按钮操作事件中包含以下代码
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);