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互转可以自己找,我的另一个文章中有:
package ec.export.checksheet; import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRPrintPage;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer; import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader; import ec.export.ECReportHelper;
import ec.utils.Dom4jXml; public class AddCheckSheet { public Dom4jXml dj = new Dom4jXml(); public JasperPrint FormTotalPage(JasperPrint jp,org.w3c.dom.Document document,Map mSelectCheckSheet){
List l=new ArrayList();
try {
l=CheckSheetReadXml(document);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} //jasper template put to this class dir path
String jasperpatha=getClass().getResource("")+"checksheeta.jasper";
jasperpatha=jasperpatha.replaceAll("file:/", "");
String jasperpathb=getClass().getResource("")+"checksheetb.jasper";
jasperpathb=jasperpathb.replaceAll("file:/", "");
for(int i=0;i<l.size();i++){ if(mSelectCheckSheet.containsKey(((Map)l.get(i)).get("cs_Model")))
{
// ========for checksheeta.jasper=======
JasperPrint jasperprint = new JasperPrint();
try {
jasperprint = JasperFillManager.fillReport(jasperpatha, (Map)l.get(i), new JREmptyDataSource());
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println(" before jp: "+jp.getPages().size());
multipageLinking(jp,jasperprint);
//System.out.println(" after jp: "+jp.getPages().size()); // =======for checksheetb.jasper=======
jasperprint = new JasperPrint();
try {
jasperprint = JasperFillManager.fillReport(jasperpathb, (Map)l.get(i), new JREmptyDataSource());
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println(" before jp: "+jp.getPages().size());
multipageLinking(jp,jasperprint);
//System.out.println(" after jp: "+jp.getPages().size());
}
}
return jp;
} 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;
} public static void main(String[] args) throws Exception {
Dom4jXml dj = new Dom4jXml();
AddCheckSheet acs=new AddCheckSheet();
SAXReader saxReader = new SAXReader();
Document document;
try {
document = saxReader.read(new File("D:/Task/draw_cs3/20141218/wcfx36_more.xml"));
JasperPrint jp=null;
// acs.CheckSheetReadXml(dj.parse(document));
Map mSelectCheckSheet=new HashMap();
jp=acs.FormTotalPage(jp,dj.parse(document),mSelectCheckSheet);
JasperViewer.viewReport(jp,false);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
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;
}
完结
本着共享的精神,减少别人重复弯路,减少重复别人的弯路
jasperreport 追加新报表(2)的更多相关文章
- ireport 追加新报表
ireport 追加新报表 /* To change this template, choose Tools | Templates * and open the template in the e ...
- 使用FileStream向txt格式的文本文件 "追加" 新内容并读取
原文:使用FileStream向txt格式的文本文件 "追加" 新内容并读取 //得到文件路径. static string filePath = AppDomain.Curren ...
- python openpyxl模块实现excel的读取,新表创建及原数据表追加新数据
当实际工作需要把excel表的数据读取出来,或者把一些统计数据写入excel表中时,一个设计丰富,文档便于寻找的模块就会显得特别的有吸引力,本文对openpyxl模块的一些常见用法做一些记录,方便工作 ...
- jasperReport Studio java报表设计(详细)
一.环境搭建 在spring-mvc.xml加入 <!-- jasperReports--><import resource="classpath*:spring-mvc- ...
- JasperReport报表开发(一)--原理介绍
1. JasperReport介绍 JasperReport 是一个开源的Java报表引擎,它不像其他的报表工具,例如Crystal报表是基于Java的,没有自己的表达式语法.Jasper Repor ...
- 报表开发神器!DevExpress Reporting v19.1:WinForms平台新功能
行业领先的.NET界面控件DevExpress Reporting全新发布了v19.1版本,本文主要为大家介绍WinForms.ASP.Net Core平台.Visual Studio报表设计器中发布 ...
- js创建与追加元素
用javascript创建元素 : var NewNode = document.creatElement('div'); 结合appendChild与insertBefore插入到DOM树中 ins ...
- ReportingService报表入门
今天我们来学习一下ReportingService报表的制作. ReportingService是微软提供的一个制作报表的工具,它集成在Microsoft Visual Studio中. 启动Visu ...
- DevExpress的XtraReport和微软RDLC报表的使用和对比
我们开发程序的时候,经常会碰到一些报表,如果是Winform的报表,一般可以采用DevExpress控件组的XtraReport,或者微软的RDLC报表,当然还有一些其他的,在此不再赘述.由于本人在W ...
随机推荐
- thymeleaf 常用标签
1.th:field th:field="*{user.sex}" 此标签会自动填充数据,比如用户的性别 user.sex 如果不为空,则会自动勾选上 2.th:each=&qu ...
- 05004_Linux的其他命令和权限命令
1.其他命令 (1)显示当前所在位置 命令:pwd (2)搜索命令 a.命令:grep 要搜索的字符串 要搜索的文件 示例:搜索/etc/sudu.conf文件中包含字符串to的行 b.搜索/etc/ ...
- Web长连接推送
http://www.workerman.net/web-sender http://wahahachuang5.iteye.com/blog/2311313
- 作为一个程序员怎么通过android开发赚钱
上面是一个程序员通过Android开发每天的收入,信则有! 自己学安卓差不多,有一年了.我本来是从事javaweb开发的,可能学习安卓上手会快点.其实安卓没有那难 .首先开发安卓程序,要有一个,开 ...
- [Javascript] Improve Composition with the Compose Combinator
To make our composition more readable and easier to name we are going to ceate a compose function we ...
- windows环境利用apache 配置虚拟主机
windows环境利用apache 配置虚拟主机 1.改动http.host #LoadModule vhost_alias_module modules/mod_vhost_alias.so #In ...
- Project Euler:Problem 93 Arithmetic expressions
By using each of the digits from the set, {1, 2, 3, 4}, exactly once, and making use of the four ari ...
- Dubbo分布式服务框架入门(附project)
要想了解Dubbo是什么,我们不防先了解它有什么用. 使用场景:比方我想开发一个网上商城项目.这个网上商城呢,比較复杂.分为pc端web管理后台.微信端销售公众号,那么我们分成四个项目,pc端站点,微 ...
- html+css实现选项卡功能
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JNI 资源释放
JNI 编程实现了 native code 和 Java 程序的交互,因此 JNI 代码编程既遵循 native code 编程语言的编程规则,同时也遵守 JNI 编程的文档规范.在内存管理方面,na ...