日期:2014-05-17 浏览次数:21217 次
Jasperreport的文档表明:
??? 1> 开发静态报表建议采纳模板方式
??? 2> 开发动态报表采用直接编辑JasperDesign对象。
但是似乎jasperreport对动态报表的操作缺乏高效率的工具类,因而开发
起来特别的费劲。
经过多方研究,得到了2种解决方案:
1> 结合Velocity技术动态生成jrxml模板文件;
2> 采用第三方工具DynamicJasper:DynamicJasper是sourceforge上的一个
开源项目,把它与jasperreport集成,可以相对轻松的解决大多数动态报表问题,
并且为进一步开发用户自定义报表提供了有效途径。
我采用的是第二种方案。
?
?jasperreports是一个用来开发报表的组件,通常和ireport来配合使用,可以快速的构建出漂亮的报表。但是ireport画出的报表模板过于死板,就是不支持动态的列。现在要自己实现一个报表模板,先参考ireport工具画出的模板,然后改改,用java代码自己实现一个。
?????? 项目组目前就是这么做的,自己去写一个模板,一个横排的,一个竖排的,应为字段比较多,一般采用竖排的。我们项目组做动态报表没有用DynamicJasper这个开源的动态报表的组件,原因是我觉得他用起来也不太方便。还有就是时间的问题。
?
做了两个模板,现在备份下来。首先是横排的模板
package cn.hzmc.dynamicreport.example7;
import java.awt.Color;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRAlignment;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRReport;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.base.JRBaseLine;
import net.sf.jasperreports.engine.base.JRBasePrintText;
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.JRDesignLine;
import net.sf.jasperreports.engine.design.JRDesignQuery;
import net.sf.jasperreports.engine.design.JRDesignStaticText;
import net.sf.jasperreports.engine.design.JRDesignStyle;
import net.sf.jasperreports.engine.design.JRDesignTextField;
import net.sf.jasperreports.engine.design.JasperDesign;
import com.capaa.pipe.config.ReadConfig;
class Sample6 {
?@SuppressWarnings("deprecation")
?Sample6() throws Exception {
??
??Connection connection = getConnection();
??
??JasperDesign jasperDesign = new JasperDesign();
??JRDesignStyle normalStyle = setReportSytle(jasperDesign);
??
??JRDesignQuery query = new JRDesignQuery();
??query.setText("select userid,name,age,sex,password,department from person");
??jasperDesign.setQuery(query);??
??//addReportFiled(jasperDesign);
??
??// Title
??JRDesignBand band = new JRDesignBand();
??band.setHeight(20);
??JRDesignStaticText staticText = new JRDesignStaticText();
??
??staticText.setX(200);
??staticText.setY(0);
??staticText.setWidth(200);
??staticText.setHeight(15);
??staticText.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);? //设置文本的对齐方式
??staticText.setStyle(normalStyle);
??staticText.setText("杭州美创科技有限公司审计报表 ");
??band.addElement(staticText);??
??JRDesignLine line = new JRDesignLine();??
??line.setX(0);
??line.setY(19);
??line.setWidth(500);
??line.setHeight(1);
??line.setForecolor(new Color(0x99,0xFF,0xFF));
??band.addElement(line);
??jasperDesign.setTitle(band);
??// Page header
??band = new JRDesignBand();
??//band.setHeight(20);
??band.setHeight(0);
??jasperDesign.setPageHeader(band);
??// Column header
??band = new JRDesignBand();
??band.setHeight(20);
??
?? // 开始添加列字段
??String[] headers = {"USERID","NAME","AGE","SEX","PASSWORD","DEPARTMENT"};
??String[] alias = {"USERID","NAME","AGE","SEX","PASSWORD","DEPARTMENT"};
?? int X = 80;
?? int columnHeaderfontSize = 10;
?? int fontSize = 8;
?? int textHeight = 20;
?? int textWidth = 80;
?? int detailHeight = 20;
?? JRDesignBand detail = new JRDesignBand();
?? detail.setHeight(detailHeight);
??for(int i =