这里以iReport 4.5版本为例。
该报表必须含有一个参数: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 = 0i <es_header.lengthi++) {
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 = 0j <es_detail.lengthj++) {
JRDesignElement ee = (JRDesignElement) es_detail[i]
if (ee.getY() == e.getY()) {
cDetail.removeElement(ee)
}
}
cHeader.removeElement(e)
}
}
}
return jdesign
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)