web*_*r07 3 java parameters jasper-reports
我已经创建了 6 个 Jasper 报告模板,所有静态文本字段都使用 SQL 查询来填写。SQL 查询使用我传入的 2 个参数:FirstName和LastName。我还传递了另外两个将被添加到报告中的参数。
这是我到目前为止将带有参数的 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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道这可能远非正确,但是如果有人可以为我指出好的文档或示例的正确方向,或者指出我做错了什么,那将有很大帮助!
以下是在 Java 应用程序中使用 Jasper Report 的简要说明
在您的报告检查器中,您可以在“字段”类别下看到我们查询的所有列名称以及在“参数”类别下定义的所有参数。


然后你可以声明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();
}
} }
Run Code Online (Sandbox Code Playgroud)您可以通过按下应用程序中的按钮来生成报告。因此您必须在按钮操作事件中包含以下代码
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);
Run Code Online (Sandbox Code Playgroud)