一、简单介绍
这里使用的工具是ireport-5.6.0来制作报表模板,内容主要有报表中如何遍历数据,报表与报表之间如何传值。
二、报表设计图
主报表
说明:主报表包括两部分:头部(Title)、循环体(Detail1);这里将Detail1的内容放到了二级报表中,如下所示:
三级报表
三、后台代码
1、在控制类里面创建工具类(ProductTraceController内部类)
(1)、第一层循环的工具类
public class Product_iReport{
private String productno;
private String partsno;
private String partsname;
private List twoiReport;
public String getProductno() {
return productno;
}
public void setProductno(String productno) {
this.productno = productno;
}
public String getPartsno() {
return partsno;
}
public void setPartsno(String partsno) {
this.partsno = partsno;
}
public String getPartsname() {
return partsname;
}
public void setPartsname(String partsname) {
this.partsname = partsname;
}
public List getTwoiReport() {
return twoiReport;
}
public void setTwoiReport(List twoiReport) {
this.twoiReport = twoiReport;
}
}
(2)、第二层循环的工具类(ProductTraceController内部类)
public class Process_iReport{
private String processno;
private String processname;
private List measureList;
public String getProcessno() {
return processno;
}
public void setProcessno(String processno) {
this.processno = processno;
}
public String getProcessname() {
return processname;
}
public void setProcessname(String processname) {
this.processname = processname;
}
public List getMeasureList() {
return measureList;
}
public void setMeasureList(List measureList) {
this.measureList = measureList;
}
}
(3)、第三层循环的工具类(ProductTraceController内部类)
public class SubreportClass{
private String itemNo; //序号
private String itemName; //项目名称
private String testValue; //实测值
private String normalValue; //名义值
private String uslValue; //上公差
private String lslValue; //下公差
private String deviation; //偏差
private String evaluate; //评定
private String toolsNo; //器具编号
private String toolsName; //器具名称
private String measureMan; //测量人
private String measureTime; //测量时间
public String getItemNo() {
return itemNo;
}
public void setItemNo(String itemNo) {
this.itemNo = itemNo;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getTestValue() {
return testValue;
}
public void setTestValue(String testValue) {
this.testValue = testValue;
}
public String getNormalValue() {
return normalValue;
}
public void setNormalValue(String normalValue) {
this.normalValue = normalValue;
}
public String getUslValue() {
return uslValue;
}
public void setUslValue(String uslValue) {
this.uslValue = uslValue;
}
public String getLslValue() {
return lslValue;
}
public void setLslValue(String lslValue) {
this.lslValue = lslValue;
}
public String getDeviation() {
return deviation;
}
public void setDeviation(String deviation) {
this.deviation = deviation;
}
public String getEvaluate() {
return evaluate;
}
public void setEvaluate(String evaluate) {
this.evaluate = evaluate;
}
public String getToolsNo() {
return toolsNo;
}
public void setToolsNo(String toolsNo) {
this.toolsNo = toolsNo;
}
public String getToolsName() {
return toolsName;
}
public void setToolsName(String toolsName) {
this.toolsName = toolsName;
}
public String getMeasureMan() {
return measureMan;
}
public void setMeasureMan(String measureMan) {
this.measureMan = measureMan;
}
public String getMeasureTime() {
return measureTime;
}
public void setMeasureTime(String measureTime) {
this.measureTime = measureTime;
}
}
2、获取获取全部树形表格中的值—产品报告模板,对应接口如下:
@RequestMapping(value = {"ReportProductList-{grano}@{ProductSerielno}@{workPieceNum}@{TmpSelectedParentId}@{ProductName}"}, method = RequestMethod.GET)
@ResponseBody
public void ReportProductList(@PathVariable(value = "grano") String grano,@PathVariable(value = "ProductSerielno") String
ProductSerielno,@PathVariable(value = "workPieceNum") String workPieceNum,@PathVariable(value = "TmpSelectedParentId") String TmpSelectedParentId,
@PathVariable(value = "ProductName") String ProductName,
HttpServletResponse response,HttpServletRequest request) throws JRException, IOException
{
String path = System.getProperty("emrpwebapp.root") + "\\jasperreports\\";
// 避免路径中出现转译字符
try {
path = java.net.URLDecoder.decode(path, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Map parameters = new HashMap();
parameters.put("ReportLogo", path + "image\\ReportLogo.jpg");
parameters.put("Title", "西安");
parameters.put("SubTitle", "德翼测量软件有限公司");
parameters.put("SUBREPORT_DIR", path);
/***标签**/
parameters.put("ProductNoTag", "工件编号");
parameters.put("ProgramNoTag", "产品代号");
parameters.put("ProductNameTag","产品名称");
parameters.put("PartsNumTag", "零部件代号");
parameters.put("PartsNameTag", "零部件名称");
parameters.put("Empty", "");
/**通用值**/
parameters.put("ProgramNo", grano);
parameters.put("ProductName", ProductName);
/**遍历二级报表-表头标签***/
Map subreportMap = new HashMap();
subreportMap.put("SUBREPORT_DIR", path);
subreportMap.put("ProcessNoTag", "工序编号:");
subreportMap.put("ProcessNameTag", "工序名称:");
/**循环遍历表数据***/
subreportMap.put("ItemNoTag", "编号");//编号
subreportMap.put("ItemNameTag","项目名称");
subreportMap.put("TestValueTag", "实测值");
subreportMap.put("NormalValueTag", "名义值");
subreportMap.put("UslValueTag", "上公差");
subreportMap.put("LslValueTag", "下公差");
subreportMap.put("DeviationTag", "偏差");
subreportMap.put("EvaluateTag", "评定");
subreportMap.put("ToolsNoTag", "器具编号");
subreportMap.put("ToolsNameTag", "器具名称");
subreportMap.put("MeasureManTag", "测量人");
subreportMap.put("MeasureTimeTag", "测量时间");
parameters.put("TwoSubreport", subreportMap);
JSONArray jsonPrdLists= ResultQuery(grano,ProductSerielno,workPieceNum,TmpSelectedParentId);
List lst=new ArrayList();//一级主表
for(int a=0;a prList=iReport_ListValue(pid,jo.getString("workPieceNum"));
pi.setTwoiReport(prList);
lst.add(pi);
}
FileInputStream jasperStream = new FileInputStream(path + "/rpt_ReportProductList.jasper");
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,
new JRBeanCollectionDataSource(lst));
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "inline; filename=ReportProductList.pdf");
final OutputStream outStream = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);
}
3、上面三层循环报表中进行值传递的方法是:
①、首先将下层报表的值放到map集合中,java代码如下:
Map subreportMap = new HashMap();
subreportMap.put("SUBREPORT_DIR", path);
subreportMap.put("ProcessNoTag", "工序编号:");
subreportMap.put("ProcessNameTag", "工序名称:");
/**循环遍历表数据***/
subreportMap.put("ItemNoTag", "编号");//编号
subreportMap.put("ItemNameTag","项目名称");
subreportMap.put("TestValueTag", "实测值");
subreportMap.put("NormalValueTag", "名义值");
subreportMap.put("UslValueTag", "上公差");
subreportMap.put("LslValueTag", "下公差");
subreportMap.put("DeviationTag", "偏差");
subreportMap.put("EvaluateTag", "评定");
subreportMap.put("ToolsNoTag", "器具编号");
subreportMap.put("ToolsNameTag", "器具名称");
subreportMap.put("MeasureManTag", "测量人");
subreportMap.put("MeasureTimeTag", "测量时间");
/**parameters是一个Map集合,存放主报表表头参数,这里将二级报表的表头参数
首先放到一个Map集合里,然后将二级报表表头参数的map集合subreportMap放到
主报表的parameters集合中**/
parameters.put("TwoSubreport", subreportMap);
②,给二级传递参数不但要在java代码中进行处理,在ireport中也要进行处理(如图)
a、如①所标注的地方为要传递给二级报表的参数
b、详见下图:
里面的配置为:
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{twoiReport})
③、二级报表如图所示:
④、到这里后台和ireport的准备工作全部完成,来看web显示部分:
4、《补充》只有两层嵌套报表的接口对应如下:
/获取树形表格中单个的值—零部件报告模板/
@RequestMapping(value = { "ReportProcessList-{rowid}@{workPieceNum}@{ProductName}"}, method = RequestMethod.GET)
@ResponseBody
public void ReportProcessList(@PathVariable(value = "rowid") String rowid,@PathVariable(value = "workPieceNum") String workPieceNum,@PathVariable(value = "ProductName") String ProductName,
HttpServletResponse response,HttpServletRequest request) throws JRException, IOException
{
Productview pv=prdViewServ.findById(rowid);
String path = System.getProperty("emrpwebapp.root") + "\\jasperreports\\";
// 避免路径中出现转译字符
try {
path = java.net.URLDecoder.decode(path, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Map parameters = new HashMap();
parameters.put("ReportLogo", path + "image\\ReportLogo.jpg");
parameters.put("Title", "西安");
parameters.put("SubTitle", "德翼测量软件有限公司");
parameters.put("SUBREPORT_DIR", path);
/***标签**/
parameters.put("ProductNoTag", "工件编号");
parameters.put("ProgramNoTag", "产品代号");
parameters.put("ProductNameTag","产品名称");
parameters.put("PartsNumTag", "零部件代号");
parameters.put("PartsNameTag", "零部件名称");
/****值****/
parameters.put("ProductNo", workPieceNum);
parameters.put("ProgramNo", pv.getGrano());
parameters.put("ProductName", ProductName);
parameters.put("Partsnum", pv.getProductnum());
parameters.put("PartstName", pv.getProductname());
parameters.put("Empty", "");
/**遍历表头标签***/
parameters.put("ProcessNoTag", "工序编号:");
parameters.put("ProcessNameTag", "工序名称:");
/**循环遍历表数据***/
parameters.put("ItemNoTag", "编号");//编号
parameters.put("ItemNameTag","项目名称");
parameters.put("TestValueTag", "实测值");
parameters.put("NormalValueTag", "名义值");
parameters.put("UslValueTag", "上公差");
parameters.put("LslValueTag", "下公差");
parameters.put("DeviationTag", "偏差");
parameters.put("EvaluateTag", "评定");
parameters.put("ToolsNoTag", "器具编号");
parameters.put("ToolsNameTag", "器具名称");
parameters.put("MeasureManTag", "测量人");
parameters.put("MeasureTimeTag", "测量时间");
List lst=iReport_ListValue(rowid,workPieceNum);//主表
FileInputStream jasperStream = new FileInputStream(path + "/rpt_ReportProcessList.jasper");
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,
new JRBeanCollectionDataSource(lst));
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "inline; filename=rpt_ReportProcessList.pdf");
final OutputStream outStream = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);
}