ireport动态增加行怎么办
展开全部
在iReport中,本身并没有提供动态列的功能,但我们可以通过Java代码进行处理,来实现。
这里以iReport 4.5版本为例。
步骤1:设计报表
该报表必须含有一个参数:dynamiccolumn (名字可以随便取,类型必须为java.util.Collection)
步骤2:通过Java代码进行处理,并生成报表
public static void main(String[] args) {
try {
//步骤1:获取JasperDesign
JasperDesign jdesign = JRXmlLoader
.load("jrxmls//dynamiccolumn.jrxml");
Map<String, Object> params = new HashMap<String, Object>();
params.put("dynamiccolumn",
java.util.Arrays.asList(new String[] { "No","Name","Gender"}));
//步骤2:根据dynamiccolumn参数,对不需要显示的列进行删除
dynamiccolumn(jdesign,params);
// 步骤3:报表修改完成后,以修改后报表进行编译,并输出
JasperReport jreport = JasperCompileManager.compileReport(jdesign);
Connection conn = null;
try {
conn = DriverManager.getConnection(
"jdbc:mysql://10.24.16.31:3306/ireportrun", "root",
"isoftstone*1");
} catch (SQLException e) {
e.printStackTrace();
}
JasperPrint jprint = JasperFillManager.fillReport(jreport, params,
conn);
JasperExportManager.exportReportToHtmlFile(jprint,
"reports//dynamiccolumn.html");
} catch (JRException e) {
e.printStackTrace();
}
}
/**
* @Title: dynamiccolumn
* @Description: TODO(对design进行处理,去掉不应该显示的列)
* @param @param jdesign JasperDesign
* @param @param params 需要显示的列
* @return JasperDesign 返回类型
*/
public static JasperDesign dynamiccolumn(JasperDesign jdesign, Map params) {
/*
* 该方法目前仅进行了简单的处理,如需更多业务,且自行添加 比如:1. 修改元素的位置 2.自动调整Title的宽度
* 3.自行调整整个报表的宽度
*/
Collection dynamiccolumns = (Collection) params.get("dynamiccolumn");
if (dynamiccolumns != null) {
JRDesignBand cHeader = (JRDesignBand) jdesign.getColumnHeader();
JRBand cDetailBand = jdesign.getDetailSection().getBands()[0];
JRDesignBand cDetail = null;
if (cDetailBand != null && cDetailBand instanceof JRDesignBand) {
cDetail = (JRDesignBand) cDetailBand;
}
JRElement[] es_header = cHeader.getElements();
JRElement[] es_detail = cDetail.getElements();
for (int i = 0; i < es_header.length; i++) {
JRDesignElement e = (JRDesignElement) es_header[i];
String v = "";
if (e instanceof JRStaticText) {
JRStaticText text = (JRStaticText) e;
v = text.getText();
}
if (!dynamiccolumns.contains(v)) {
for (int j = 0; j < es_detail.length; j++) {
JRDesignElement ee = (JRDesignElement) es_detail[i];
if (ee.getY() == e.getY()) {
cDetail.removeElement(ee);
}
}
cHeader.removeElement(e);
}
}
}
return jdesign;
}
这里以iReport 4.5版本为例。
步骤1:设计报表
该报表必须含有一个参数:dynamiccolumn (名字可以随便取,类型必须为java.util.Collection)
步骤2:通过Java代码进行处理,并生成报表
public static void main(String[] args) {
try {
//步骤1:获取JasperDesign
JasperDesign jdesign = JRXmlLoader
.load("jrxmls//dynamiccolumn.jrxml");
Map<String, Object> params = new HashMap<String, Object>();
params.put("dynamiccolumn",
java.util.Arrays.asList(new String[] { "No","Name","Gender"}));
//步骤2:根据dynamiccolumn参数,对不需要显示的列进行删除
dynamiccolumn(jdesign,params);
// 步骤3:报表修改完成后,以修改后报表进行编译,并输出
JasperReport jreport = JasperCompileManager.compileReport(jdesign);
Connection conn = null;
try {
conn = DriverManager.getConnection(
"jdbc:mysql://10.24.16.31:3306/ireportrun", "root",
"isoftstone*1");
} catch (SQLException e) {
e.printStackTrace();
}
JasperPrint jprint = JasperFillManager.fillReport(jreport, params,
conn);
JasperExportManager.exportReportToHtmlFile(jprint,
"reports//dynamiccolumn.html");
} catch (JRException e) {
e.printStackTrace();
}
}
/**
* @Title: dynamiccolumn
* @Description: TODO(对design进行处理,去掉不应该显示的列)
* @param @param jdesign JasperDesign
* @param @param params 需要显示的列
* @return JasperDesign 返回类型
*/
public static JasperDesign dynamiccolumn(JasperDesign jdesign, Map params) {
/*
* 该方法目前仅进行了简单的处理,如需更多业务,且自行添加 比如:1. 修改元素的位置 2.自动调整Title的宽度
* 3.自行调整整个报表的宽度
*/
Collection dynamiccolumns = (Collection) params.get("dynamiccolumn");
if (dynamiccolumns != null) {
JRDesignBand cHeader = (JRDesignBand) jdesign.getColumnHeader();
JRBand cDetailBand = jdesign.getDetailSection().getBands()[0];
JRDesignBand cDetail = null;
if (cDetailBand != null && cDetailBand instanceof JRDesignBand) {
cDetail = (JRDesignBand) cDetailBand;
}
JRElement[] es_header = cHeader.getElements();
JRElement[] es_detail = cDetail.getElements();
for (int i = 0; i < es_header.length; i++) {
JRDesignElement e = (JRDesignElement) es_header[i];
String v = "";
if (e instanceof JRStaticText) {
JRStaticText text = (JRStaticText) e;
v = text.getText();
}
if (!dynamiccolumns.contains(v)) {
for (int j = 0; j < es_detail.length; j++) {
JRDesignElement ee = (JRDesignElement) es_detail[i];
if (ee.getY() == e.getY()) {
cDetail.removeElement(ee);
}
}
cHeader.removeElement(e);
}
}
}
return jdesign;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |