我已经创建了6个Jasper Report模板,所有静态文本字段都使用SQL查询填写 . SQL查询使用我传入的2个参数: FirstName 和 LastName . 我还传递了其他2个参数,这些参数只会添加到Report中 .
这是我到目前为止将带有参数的HashMap传递给Report的代码 . 但是我很遗憾,因为我找不到任何好的文档或示例 .
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);
}
}
}
}
我知道这可能远非正确,但如果有人可以指出我正确的文档或示例方向,或者指出我做错了什么,那将会有很大的帮助!
1 回答
您可以在SQL查询中添加参数,以防需要通过查询过滤数据 . 只需使用 New Parameter 按钮添加参数,您就可以将显示在文本区域内的参数拖放到查询中 .
在报表检查器中,您可以在 Fields 类别下查看查询的所有列名称,并在 Parameters 类别下查看所有参数 .
通过将字段名称和参数拖放到 Report Designer ,您可以根据需要设计报告 . Title Band (您的 Headers 在这里), Column Header Band (列名), Detail Band (此处为迭代数据)和 Summary Band (如同Grand_Total,日期,发布者和图表元素可添加到此部分)足以用于基本报告 .