日期:2014-05-20  浏览次数:20970 次

ireport及jasperreport动态报表
最近刚刚学习jasperreport报表使用,做了几个例子,但在实现动态添加元素(列名,循环数据)上出现了问题,没办法解决.
但是出现这种异常,感觉自己没什么错,查了很久的资料还找不到答案,各位帮下忙.
net.sf.jasperreports.engine.design.JRValidationException: Report design not valid : 
1. Field not found : Field1
这是设计

保存设计,名domeDynamic.jrxml. 
代码  
  int randomNum = new Random().nextInt(20);
  String jrxmlPath = this.getServletContext().getRealPath("/jasper");
  jrxmlPath = jrxmlPath + "\\domeDynamic.jrxml";
 
  JasperDesign jasperDesign= JRXmlLoader.load(jrxmlPath);//因为还是添加元素进去,所以还只是jrxml文件

  JRDesignBand columnHeaderBand = (JRDesignBand )jasperDesign.getColumnHeader();//获取ColumnHeader拦
  JRDesignBand detailBand = (JRDesignBand )jasperDesign.getDetail();//获取Detail拦
  JRDesignTextField detailField= (JRDesignTextField )detailBand.getElementByKey("textField-2"); 
  JRDesignStaticText columnHeaderField = (JRDesignStaticText)columnHeaderBand.getElementByKey("staticText-3");
  Map map = new HashMap();
  List list = new ArrayList(); 数据集 list.add(map);
  map.put("Field1","1");
  map.put("Field2","2");
  //根据随即生成的数进行动态添加列
  for(int i=0,y=3;i<=randomNum;i++,y++){
String detailFieldKey = "Field-"+y;
//String columnHeaderFieldKey = "staticText-"+y+1;
map.put(detailFieldKey,y);
//map.put(columnHeaderFieldKey,"列"+y);

detailField= (JRDesignTextField)detailField.clone();
detailField.setX(detailField.getX()+detailField.getWidth());
JRDesignExpression expression = new JRDesignExpression();
expression.setText("$F{"+detailFieldKey+"}");
System.out.println("$F{"+detailFieldKey+"}");
expression.setValueClass(java.lang.String.class);
detailField.setExpression(expression);
detailBand.addElement(detailField);

columnHeaderField = (JRDesignStaticText)columnHeaderField.clone();
columnHeaderField.setX(columnHeaderField.getX()+columnHeaderField.getWidth());
columnHeaderField.setText("列"+y);
columnHeaderBand.addElement(columnHeaderField);
}
list.add(map);
System.out.println(list);
JRDataSource jRDataSource = new JRMapCollectionDataSource(list); //用这个应该可以
  JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);//编译jrxml文件,异常也出在这一行.
  JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null,jRDataSource); //填充打印
  OutputStream outputStream = response.getOutputStream();
  response.setContentType("application/pdf");
  response.setCharacterEncoding("GB2312");
  response.setHeader("Content-Disposition","attachment; filename="+URLEncoder.encode("PDF报表","GB2312")+".pdf");
  JRPdfExporter exporter = new JRPdfExporter();
  exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
  exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,outputStream);
  exporter.exportReport();
  outputStream.close();
  out.clear();
  out = pageContext.pushBody();
异常:
  net.sf.jasperreports.engine.design.JRValidationException: Report design not valid : 
1. Field not found : Field1
2. Field not found : Field2
3. Field not found : Field-3
4. Field not found : Field-4
5. Field not found : Field-5
6. Field not found : Field-6
7. Field not found : Field-7
8. Field not found : Field-8
at net.sf.jasperreports.engine.design.JRAbstractCompiler.verifyDesign(JRAbstractCompiler.java:260)