jasperreport 追加新报表(2)
用ireport做好模版后,如果要新加一个打印页,如果是新手,直接修改模版应该是理想情况,
可是什么数据源 feild,parameter,var,subreport ,还有路径,
真的可以让一个人疯掉,(关键这些东西组织在一起跑不了,主要是要动态的出现这些新页,就是说要动态控制这些新页的数量以及要不要出现),
还好,想到如果直接用java在外部控制,就可以易如反掌了,毕竟java的东西自己熟,
幸运的看到一篇关于 ireport追加新页的文章
(是英文的,有时候不得不承认啊,谷歌是比度娘给力,关键是公司网络的各种墙,连个CSDN 、ITEYE都不给上,逼我学英文...)
写怎么添加时候绕来绕去,读完理解最关键的一个方法或思路:
主要是往已有的 JasperPrint 对象中addPage()。
private JasperPrint multipageLinking(JasperPrint page1, JasperPrint page2) { List pages = page2.getPages(); for (int count = 0; count <pages.size(); count++) { page1.addPage((JRPrintPage) pages.get(count)); } return page1; }
如果你有更多的页要添加,或者有更复杂的逻辑要执行,你可以在外部来调用这个方法
最后制作得到 JasperPrint 对象 jp 然后用下面方法做打印显示
JasperViewer.viewReport(jp,false);
最后给出完整的一个例子,xml文件没提供,DOM4J所需的包有三个自己下载,
dom4j-1.5.1.jar
jaxen-1.1.1.jar
saxpath-1.0-FCS.jar
(findjar.com是个好地方),关于org.w3c.dom.Document 与org.dom4j.Document互转可以自己找,我的另一个文章中有:
1 package ec.export.checksheet; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.util.ArrayList; 7 import java.util.HashMap; 8 import java.util.List; 9 import java.util.Map; 10 11 import net.sf.jasperreports.engine.JREmptyDataSource; 12 import net.sf.jasperreports.engine.JRException; 13 import net.sf.jasperreports.engine.JRPrintPage; 14 import net.sf.jasperreports.engine.JasperFillManager; 15 import net.sf.jasperreports.engine.JasperPrint; 16 import net.sf.jasperreports.engine.JasperReport; 17 import net.sf.jasperreports.engine.util.JRLoader; 18 import net.sf.jasperreports.view.JasperViewer; 19 20 import org.dom4j.Document; 21 import org.dom4j.DocumentException; 22 import org.dom4j.io.SAXReader; 23 24 import ec.export.ECReportHelper; 25 import ec.utils.Dom4jXml; 26 27 public class AddCheckSheet { 28 29 public Dom4jXml dj = new Dom4jXml(); 30 31 public JasperPrint FormTotalPage(JasperPrint jp,org.w3c.dom.Document document,Map mSelectCheckSheet){ 32 List l=new ArrayList(); 33 try { 34 l=CheckSheetReadXml(document); 35 } catch (Exception e1) { 36 // TODO Auto-generated catch block 37 e1.printStackTrace(); 38 } 39 40 //jasper template put to this class dir path 41 String jasperpatha=getClass().getResource("")+"checksheeta.jasper"; 42 jasperpatha=jasperpatha.replaceAll("file:/", ""); 43 String jasperpathb=getClass().getResource("")+"checksheetb.jasper"; 44 jasperpathb=jasperpathb.replaceAll("file:/", ""); 45 for(int i=0;i<l.size();i++){ 46 47 if(mSelectCheckSheet.containsKey(((Map)l.get(i)).get("cs_Model"))) 48 { 49 // ========for checksheeta.jasper======= 50 JasperPrint jasperprint = new JasperPrint(); 51 try { 52 jasperprint = JasperFillManager.fillReport(jasperpatha, (Map)l.get(i), new JREmptyDataSource()); 53 } catch (Exception e) { 54 e.printStackTrace(); 55 } 56 //System.out.println(" before jp: "+jp.getPages().size()); 57 multipageLinking(jp,jasperprint); 58 //System.out.println(" after jp: "+jp.getPages().size()); 59 60 // =======for checksheetb.jasper======= 61 jasperprint = new JasperPrint(); 62 try { 63 jasperprint = JasperFillManager.fillReport(jasperpathb, (Map)l.get(i), new JREmptyDataSource()); 64 } catch (Exception e) { 65 e.printStackTrace(); 66 } 67 //System.out.println(" before jp: "+jp.getPages().size()); 68 multipageLinking(jp,jasperprint); 69 //System.out.println(" after jp: "+jp.getPages().size()); 70 } 71 } 72 return jp; 73 } 74 75 private JasperPrint multipageLinking(JasperPrint page1, JasperPrint page2) { 76 List pages = page2.getPages(); 77 for (int count = 0; count <pages.size(); count++) { 78 page1.addPage((JRPrintPage) pages.get(count)); 79 } 80 return page1; 81 } 82 83 public static void main(String[] args) throws Exception { 84 Dom4jXml dj = new Dom4jXml(); 85 AddCheckSheet acs=new AddCheckSheet(); 86 SAXReader saxReader = new SAXReader(); 87 Document document; 88 try { 89 document = saxReader.read(new File("D:/Task/draw_cs3/20141218/wcfx36_more.xml")); 90 JasperPrint jp=null; 91 // acs.CheckSheetReadXml(dj.parse(document)); 92 Map mSelectCheckSheet=new HashMap(); 93 jp=acs.FormTotalPage(jp,dj.parse(document),mSelectCheckSheet); 94 JasperViewer.viewReport(jp,false); 95 } catch (DocumentException e) { 96 // TODO Auto-generated catch block 97 e.printStackTrace(); 98 } 99 } 100 }
public List CheckSheetReadXml(org.w3c.dom.Document doc) throws Exception{ Document document=dj.parse(doc); List pl=new ArrayList(); //count product num List list = document.selectNodes("/report/job/product"); int num = list.size(); for(int i=1;i<=num;i++) { Map mproduct=new HashMap(); //0 select_date String xpathstr="/report/job/@select_date"; mproduct.put("cs_Date", dj.getContentString(document,xpathstr)); //model 1 AT3ARF1R AT5CRF4R xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Screw Compressor Water Cooled Package Chiller‘]/row/field[name=‘Model #‘]/value"; String s1=dj.getContentString(document,xpathstr); xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Screw Compressor Water Cooled Package Chiller‘]/row/field[name=‘Component code‘]/value"; String tmep=dj.getContentString(document,xpathstr); tmep=tmep.substring(2); String tempbefore=tmep.substring(3); String tempafter=tmep.substring(0,3); tmep=tempbefore+tempafter; mproduct.put("cs_Model", s1+tmep); xpathstr="/report/job/product["+i+"]/qty"; mproduct.put("cs_Qty", dj.getContentString(document,xpathstr)); //evap 2 xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Evaporator_Condenser‘]/col1/field[contains(name,‘Entering fluid temp‘)]/value"; mproduct.put("cs_Evap_Entering_fluid_temp", dj.getContentString(document,xpathstr)); xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Evaporator_Condenser‘]/col1/field[contains(name,‘Fluid flow rate‘)]/value"; mproduct.put("cs_Evap_Fluid_flow_rate", dj.getContentString(document,xpathstr)); xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Evaporator_Condenser‘]/col1/field[contains(name,‘Fouling factor‘)]/value"; mproduct.put("cs_Evap_Fouling_factor", dj.getContentString(document,xpathstr)); xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Evaporator_Condenser‘]/col1/field[contains(name,‘Leaving fluid temp‘)]/value"; mproduct.put("cs_Evap_Leaving_fluid_temp", dj.getContentString(document,xpathstr)); xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Evaporator_Condenser‘]/col1/field[contains(name,‘Fluid‘)]/value"; mproduct.put("cs_Evap_Fluid", dj.getContentString(document,xpathstr)); xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Evaporator_Condenser‘]/col1/field[contains(name,‘Number of passes‘)]/value"; mproduct.put("cs_Evap_Number_of_passes", dj.getContentString(document,xpathstr)); //cond 3 xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Evaporator_Condenser‘]/col2/field[contains(name,‘Entering fluid temp‘)]/value"; mproduct.put("cs_Cond_Entering_fluid_temp", dj.getContentString(document,xpathstr)); xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Evaporator_Condenser‘]/col2/field[contains(name,‘Fluid flow rate‘)]/value"; mproduct.put("cs_Cond_Fluid_flow_rate", dj.getContentString(document,xpathstr)); xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Evaporator_Condenser‘]/col2/field[contains(name,‘Fouling factor‘)]/value"; mproduct.put("cs_Cond_Fouling_factor", dj.getContentString(document,xpathstr)); xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Evaporator_Condenser‘]/col2/field[contains(name,‘Leaving fluid temp‘)]/value"; mproduct.put("cs_Cond_Leaving_fluid_temp", dj.getContentString(document,xpathstr)); xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Evaporator_Condenser‘]/col2/field[contains(name,‘Fluid‘)]/value"; mproduct.put("cs_Cond_Fluid", dj.getContentString(document,xpathstr)); xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Evaporator_Condenser‘]/col2/field[contains(name,‘Number of passes‘)]/value"; mproduct.put("cs_Cond_Number_of_passes", dj.getContentString(document,xpathstr)); //4 Cooling Conditions of service //<name>Capacity (Tons)</name> xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Cooling Conditions of service‘]/col1/field[contains(name,‘Capacity‘)]/value"; mproduct.put("cs_Capacity", dj.getContentString(document,xpathstr)); //<name>Energy efficiency (kW/Ton)</name> xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Cooling Conditions of service‘]/col2/field[contains(name,‘Energy efficiency‘)]/value"; mproduct.put("cs_Energy_efficiency", dj.getContentString(document,xpathstr)); //5<name>Unit power supply</name> xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Electrical characteristics‘]/col1/field[contains(name,‘Unit power supply‘)]/value"; mproduct.put("cs_Unit_power_supply", dj.getContentString(document,xpathstr)); //6<name>Refrigerant</name>//<value>R134a</value> xpathstr="/report/job/product["+i+"]/performanceData/group[name=‘Physical Specifications‘]/col2/field[contains(name,‘Refrigerant‘)]/value"; mproduct.put("cs_Refrigerant", dj.getContentString(document,xpathstr)); //getContent(document,xpathstr); pl.add(mproduct); } return pl; }
完结
本着共享的精神,减少别人重复弯路,减少重复别人的弯路
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。