页面有 3个表A 中的字段: 字段1,字段2,字段3
其中这3个字段是复选框形式,若客户选择其中某个字段 则在报表中把这个字段显示出来,若不选择不在报表中显示,也就是说 显示的页面的列是动态的,在iReport中 来如何实现? 谢谢!
8 个解决方案
#1
顶一下!
我也正被这个问题困扰呢
郁闷ing............
我也正被这个问题困扰呢
郁闷ing............
#2
顶。。有人知道这问题吗?
#3
我也是这问题呀!
哎!哪位高手速度解决下!
哎!哪位高手速度解决下!
#4
解决没?
#5
这么多年了,还没解决?
#6
package report.service;
import java.io.File;
import models.sc.Report;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRReport;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JRDesignBand;
import net.sf.jasperreports.engine.design.JRDesignExpression;
import net.sf.jasperreports.engine.design.JRDesignField;
import net.sf.jasperreports.engine.design.JRDesignStaticText;
import net.sf.jasperreports.engine.design.JRDesignTextField;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.xml.JRXmlWriter;
/**
* 根据报表模板动态创建报表
*
* @author liudong
* 群号:10721223
* @date 2012-7-13
*
*/
public class ReportTemplate {
private static final String flagTextKey = "customFlagText";
/**
* 根据条件动态创建报表
*
* @param fileDir
* 报表模板文件路径
* @param headerFileds
* 列头字段
* @param detailFields
* 取表内容字段
* @param report
* 报表值对象
* @param reportDir
* 生成报表输出路径
* @return
* @throws JRException
*/
@SuppressWarnings("deprecation")
public JasperReport templateCreator(String templateFileDir, String[] headerFileds, String[] detailFields, Report report, String reportDir) throws JRException {
File file = new File(templateFileDir);
JasperDesign jasperDesign = new JasperDesign();
jasperDesign = JRXmlLoader.load(file);
int restWidth = jasperDesign.getColumnWidth();
int columnNum = headerFileds.length;
int columnWidth = restWidth / columnNum;
JRDesignBand columnHeader = (JRDesignBand) jasperDesign.getColumnHeader();
JRDesignBand detail = (JRDesignBand) jasperDesign.getDetailSection().getBands()[0];
for (int i = 0; i < headerFileds.length; i++) {
JRDesignField jrfile = new JRDesignField();
jrfile.setValueClassName("java.lang.String");
jrfile.setName(headerFileds[i]);
jasperDesign.addField(jrfile);
// 生成列头 JRDesignStaticText
JRDesignStaticText textField = (JRDesignStaticText) (((JRDesignBand) jasperDesign.getColumnHeader()).getElementByKey(flagTextKey)).clone();
textField.setX(14 + columnWidth * (i));
textField.setY(0);
textField.setWidth(columnWidth);
textField.setHeight(20);
textField.setText(headerFileds[i]);
columnHeader.addElement(textField);
}
for (int i = 0; i < detailFields.length; i++) {
JRDesignField jrfilec = new JRDesignField();
jrfilec.setValueClassName("java.lang.String");
jrfilec.setName(detailFields[i]);
jasperDesign.addField(jrfilec);
// 生成表内容]
String field = "";
if (null != detailFields && null != detailFields[i])
field = "$F{" + detailFields[i] + "}";
JRDesignTextField textField1 = (JRDesignTextField) (((JRDesignBand) jasperDesign.getDetailSection().getBands()[0]).getElementByKey("textField-24")).clone();
textField1.setX(14 + columnWidth * (i));
textField1.setY(0);
textField1.setWidth(columnWidth);
textField1.setHeight(20);
JRDesignExpression expression1 = new JRDesignExpression();
expression1.setValueClass(java.lang.String.class);
expression1.setText(field);
textField1.setExpression(expression1);
detail.addElement(textField1);
}
JRXmlWriter.writeReport((JRReport) jasperDesign, reportDir, "UTF-8");
return JasperCompileManager.compileReport(jasperDesign);
}
}
import java.io.File;
import models.sc.Report;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRReport;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JRDesignBand;
import net.sf.jasperreports.engine.design.JRDesignExpression;
import net.sf.jasperreports.engine.design.JRDesignField;
import net.sf.jasperreports.engine.design.JRDesignStaticText;
import net.sf.jasperreports.engine.design.JRDesignTextField;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.xml.JRXmlWriter;
/**
* 根据报表模板动态创建报表
*
* @author liudong
* 群号:10721223
* @date 2012-7-13
*
*/
public class ReportTemplate {
private static final String flagTextKey = "customFlagText";
/**
* 根据条件动态创建报表
*
* @param fileDir
* 报表模板文件路径
* @param headerFileds
* 列头字段
* @param detailFields
* 取表内容字段
* @param report
* 报表值对象
* @param reportDir
* 生成报表输出路径
* @return
* @throws JRException
*/
@SuppressWarnings("deprecation")
public JasperReport templateCreator(String templateFileDir, String[] headerFileds, String[] detailFields, Report report, String reportDir) throws JRException {
File file = new File(templateFileDir);
JasperDesign jasperDesign = new JasperDesign();
jasperDesign = JRXmlLoader.load(file);
int restWidth = jasperDesign.getColumnWidth();
int columnNum = headerFileds.length;
int columnWidth = restWidth / columnNum;
JRDesignBand columnHeader = (JRDesignBand) jasperDesign.getColumnHeader();
JRDesignBand detail = (JRDesignBand) jasperDesign.getDetailSection().getBands()[0];
for (int i = 0; i < headerFileds.length; i++) {
JRDesignField jrfile = new JRDesignField();
jrfile.setValueClassName("java.lang.String");
jrfile.setName(headerFileds[i]);
jasperDesign.addField(jrfile);
// 生成列头 JRDesignStaticText
JRDesignStaticText textField = (JRDesignStaticText) (((JRDesignBand) jasperDesign.getColumnHeader()).getElementByKey(flagTextKey)).clone();
textField.setX(14 + columnWidth * (i));
textField.setY(0);
textField.setWidth(columnWidth);
textField.setHeight(20);
textField.setText(headerFileds[i]);
columnHeader.addElement(textField);
}
for (int i = 0; i < detailFields.length; i++) {
JRDesignField jrfilec = new JRDesignField();
jrfilec.setValueClassName("java.lang.String");
jrfilec.setName(detailFields[i]);
jasperDesign.addField(jrfilec);
// 生成表内容]
String field = "";
if (null != detailFields && null != detailFields[i])
field = "$F{" + detailFields[i] + "}";
JRDesignTextField textField1 = (JRDesignTextField) (((JRDesignBand) jasperDesign.getDetailSection().getBands()[0]).getElementByKey("textField-24")).clone();
textField1.setX(14 + columnWidth * (i));
textField1.setY(0);
textField1.setWidth(columnWidth);
textField1.setHeight(20);
JRDesignExpression expression1 = new JRDesignExpression();
expression1.setValueClass(java.lang.String.class);
expression1.setText(field);
textField1.setExpression(expression1);
detail.addElement(textField1);
}
JRXmlWriter.writeReport((JRReport) jasperDesign, reportDir, "UTF-8");
return JasperCompileManager.compileReport(jasperDesign);
}
}
#7
原理是这样的
1.就是ireport画好模板后,生成了个reporttemplate.jrxml文件,
2.然后用JasperDesign design = JRXmlLoader.load(file) 得到 JasperDesign
3.然后根据需求列个数,动态将列插入
4.然后compileReport,fill data 生成报表
1.就是ireport画好模板后,生成了个reporttemplate.jrxml文件,
2.然后用JasperDesign design = JRXmlLoader.load(file) 得到 JasperDesign
3.然后根据需求列个数,动态将列插入
4.然后compileReport,fill data 生成报表
#8
在画报表的时候怎么定义customFlagText
#1
顶一下!
我也正被这个问题困扰呢
郁闷ing............
我也正被这个问题困扰呢
郁闷ing............
#2
顶。。有人知道这问题吗?
#3
我也是这问题呀!
哎!哪位高手速度解决下!
哎!哪位高手速度解决下!
#4
解决没?
#5
这么多年了,还没解决?
#6
package report.service;
import java.io.File;
import models.sc.Report;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRReport;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JRDesignBand;
import net.sf.jasperreports.engine.design.JRDesignExpression;
import net.sf.jasperreports.engine.design.JRDesignField;
import net.sf.jasperreports.engine.design.JRDesignStaticText;
import net.sf.jasperreports.engine.design.JRDesignTextField;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.xml.JRXmlWriter;
/**
* 根据报表模板动态创建报表
*
* @author liudong
* 群号:10721223
* @date 2012-7-13
*
*/
public class ReportTemplate {
private static final String flagTextKey = "customFlagText";
/**
* 根据条件动态创建报表
*
* @param fileDir
* 报表模板文件路径
* @param headerFileds
* 列头字段
* @param detailFields
* 取表内容字段
* @param report
* 报表值对象
* @param reportDir
* 生成报表输出路径
* @return
* @throws JRException
*/
@SuppressWarnings("deprecation")
public JasperReport templateCreator(String templateFileDir, String[] headerFileds, String[] detailFields, Report report, String reportDir) throws JRException {
File file = new File(templateFileDir);
JasperDesign jasperDesign = new JasperDesign();
jasperDesign = JRXmlLoader.load(file);
int restWidth = jasperDesign.getColumnWidth();
int columnNum = headerFileds.length;
int columnWidth = restWidth / columnNum;
JRDesignBand columnHeader = (JRDesignBand) jasperDesign.getColumnHeader();
JRDesignBand detail = (JRDesignBand) jasperDesign.getDetailSection().getBands()[0];
for (int i = 0; i < headerFileds.length; i++) {
JRDesignField jrfile = new JRDesignField();
jrfile.setValueClassName("java.lang.String");
jrfile.setName(headerFileds[i]);
jasperDesign.addField(jrfile);
// 生成列头 JRDesignStaticText
JRDesignStaticText textField = (JRDesignStaticText) (((JRDesignBand) jasperDesign.getColumnHeader()).getElementByKey(flagTextKey)).clone();
textField.setX(14 + columnWidth * (i));
textField.setY(0);
textField.setWidth(columnWidth);
textField.setHeight(20);
textField.setText(headerFileds[i]);
columnHeader.addElement(textField);
}
for (int i = 0; i < detailFields.length; i++) {
JRDesignField jrfilec = new JRDesignField();
jrfilec.setValueClassName("java.lang.String");
jrfilec.setName(detailFields[i]);
jasperDesign.addField(jrfilec);
// 生成表内容]
String field = "";
if (null != detailFields && null != detailFields[i])
field = "$F{" + detailFields[i] + "}";
JRDesignTextField textField1 = (JRDesignTextField) (((JRDesignBand) jasperDesign.getDetailSection().getBands()[0]).getElementByKey("textField-24")).clone();
textField1.setX(14 + columnWidth * (i));
textField1.setY(0);
textField1.setWidth(columnWidth);
textField1.setHeight(20);
JRDesignExpression expression1 = new JRDesignExpression();
expression1.setValueClass(java.lang.String.class);
expression1.setText(field);
textField1.setExpression(expression1);
detail.addElement(textField1);
}
JRXmlWriter.writeReport((JRReport) jasperDesign, reportDir, "UTF-8");
return JasperCompileManager.compileReport(jasperDesign);
}
}
import java.io.File;
import models.sc.Report;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRReport;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JRDesignBand;
import net.sf.jasperreports.engine.design.JRDesignExpression;
import net.sf.jasperreports.engine.design.JRDesignField;
import net.sf.jasperreports.engine.design.JRDesignStaticText;
import net.sf.jasperreports.engine.design.JRDesignTextField;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.xml.JRXmlWriter;
/**
* 根据报表模板动态创建报表
*
* @author liudong
* 群号:10721223
* @date 2012-7-13
*
*/
public class ReportTemplate {
private static final String flagTextKey = "customFlagText";
/**
* 根据条件动态创建报表
*
* @param fileDir
* 报表模板文件路径
* @param headerFileds
* 列头字段
* @param detailFields
* 取表内容字段
* @param report
* 报表值对象
* @param reportDir
* 生成报表输出路径
* @return
* @throws JRException
*/
@SuppressWarnings("deprecation")
public JasperReport templateCreator(String templateFileDir, String[] headerFileds, String[] detailFields, Report report, String reportDir) throws JRException {
File file = new File(templateFileDir);
JasperDesign jasperDesign = new JasperDesign();
jasperDesign = JRXmlLoader.load(file);
int restWidth = jasperDesign.getColumnWidth();
int columnNum = headerFileds.length;
int columnWidth = restWidth / columnNum;
JRDesignBand columnHeader = (JRDesignBand) jasperDesign.getColumnHeader();
JRDesignBand detail = (JRDesignBand) jasperDesign.getDetailSection().getBands()[0];
for (int i = 0; i < headerFileds.length; i++) {
JRDesignField jrfile = new JRDesignField();
jrfile.setValueClassName("java.lang.String");
jrfile.setName(headerFileds[i]);
jasperDesign.addField(jrfile);
// 生成列头 JRDesignStaticText
JRDesignStaticText textField = (JRDesignStaticText) (((JRDesignBand) jasperDesign.getColumnHeader()).getElementByKey(flagTextKey)).clone();
textField.setX(14 + columnWidth * (i));
textField.setY(0);
textField.setWidth(columnWidth);
textField.setHeight(20);
textField.setText(headerFileds[i]);
columnHeader.addElement(textField);
}
for (int i = 0; i < detailFields.length; i++) {
JRDesignField jrfilec = new JRDesignField();
jrfilec.setValueClassName("java.lang.String");
jrfilec.setName(detailFields[i]);
jasperDesign.addField(jrfilec);
// 生成表内容]
String field = "";
if (null != detailFields && null != detailFields[i])
field = "$F{" + detailFields[i] + "}";
JRDesignTextField textField1 = (JRDesignTextField) (((JRDesignBand) jasperDesign.getDetailSection().getBands()[0]).getElementByKey("textField-24")).clone();
textField1.setX(14 + columnWidth * (i));
textField1.setY(0);
textField1.setWidth(columnWidth);
textField1.setHeight(20);
JRDesignExpression expression1 = new JRDesignExpression();
expression1.setValueClass(java.lang.String.class);
expression1.setText(field);
textField1.setExpression(expression1);
detail.addElement(textField1);
}
JRXmlWriter.writeReport((JRReport) jasperDesign, reportDir, "UTF-8");
return JasperCompileManager.compileReport(jasperDesign);
}
}
#7
原理是这样的
1.就是ireport画好模板后,生成了个reporttemplate.jrxml文件,
2.然后用JasperDesign design = JRXmlLoader.load(file) 得到 JasperDesign
3.然后根据需求列个数,动态将列插入
4.然后compileReport,fill data 生成报表
1.就是ireport画好模板后,生成了个reporttemplate.jrxml文件,
2.然后用JasperDesign design = JRXmlLoader.load(file) 得到 JasperDesign
3.然后根据需求列个数,动态将列插入
4.然后compileReport,fill data 生成报表
#8
在画报表的时候怎么定义customFlagText