var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-20738293-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script')"/>
jutleo
欢迎走进有风的地方~~
posts - 63,  comments - 279,  trackbacks - 0
jasperReport的这个包net.sf.jasperreports.engine.design 是这样描述的:Contains design time implementations for the library's main interfaces as well as report compiling tools.因此我们完全可以使用它的API构建自己的报表,还是和前几篇使用同一个数据库表。
        构建JasperDesign对象:
设置一些对应的属性:
// JasperDesign
        JasperDesign jasperDesign = new JasperDesign();
        jasperDesign.setName(
"NoXmlDesignReport"
);
        jasperDesign.setPageWidth(
595
);
        jasperDesign.setPageHeight(
842
);
        jasperDesign.setColumnWidth(
515
);
        jasperDesign.setColumnSpacing(
0
);
        jasperDesign.setLeftMargin(
40
);
        jasperDesign.setRightMargin(
40
);
        jasperDesign.setTopMargin(
50
);
        jasperDesign.setBottomMargin(
50);
字体:
// Fonts
        JRDesignStyle normalStyle = new JRDesignStyle();
        normalStyle.setName(
"Arial_Normal");
        normalStyle.setDefault(
true);
        normalStyle.setFontName(
"Arial");
        normalStyle.setFontSize(
12);
        normalStyle.setPdfFontName(
"Helvetica");
        normalStyle.setPdfEncoding(
"Cp1252");
        normalStyle.setPdfEmbedded(
false);
        jasperDesign.addStyle(normalStyle);

        JRDesignStyle boldStyle 
= new JRDesignStyle();
        boldStyle.setName(
"Arial_Bold");
        boldStyle.setFontName(
"Arial");
        boldStyle.setFontSize(
12);
        boldStyle.setBold(
true);
        boldStyle.setPdfFontName(
"Helvetica-Bold");
        boldStyle.setPdfEncoding(
"Cp1252");
        boldStyle.setPdfEmbedded(
false);
        jasperDesign.addStyle(boldStyle);

        JRDesignStyle italicStyle 
= new JRDesignStyle();
        italicStyle.setName(
"Arial_Italic");
        italicStyle.setFontName(
"Arial");
        italicStyle.setFontSize(
12);
        italicStyle.setItalic(
true);
        italicStyle.setPdfFontName(
"Helvetica-Oblique");
        italicStyle.setPdfEncoding(
"Cp1252");
        italicStyle.setPdfEmbedded(
false);
        jasperDesign.addStyle(italicStyle);
定义报表的参数,并添加到报表设计器上
// Parameters
        JRDesignParameter parameter = new JRDesignParameter();
        parameter.setName(
"ReportTitle");
        parameter.setValueClass(java.lang.String.
class);
        jasperDesign.addParameter(parameter);
定义报表的字段,这些字段是最终显示到报表上的域
// Fields
        JRDesignField field = new JRDesignField();
        field.setName(
"userid");
        field.setValueClass(java.lang.Integer.
class);
        jasperDesign.addField(field);

        field 
= new JRDesignField();
        field.setName(
"name");
        field.setValueClass(java.lang.String.
class);
        jasperDesign.addField(field);

        field 
= new JRDesignField();
        field.setName(
"sex");
        field.setValueClass(java.lang.String.
class);
        jasperDesign.addField(field);

        field 
= new JRDesignField();
        field.setName(
"age");
        field.setValueClass(java.lang.String.
class);
        jasperDesign.addField(field);

        field 
= new JRDesignField();
        field.setName(
"password");
        field.setValueClass(java.lang.String.
class);
        jasperDesign.addField(field);

        field 
= new JRDesignField();
        field.setName(
"department");
        field.setValueClass(java.lang.String.
class);
        jasperDesign.addField(field);

        JRDesignBand band 
= null;
        JRDesignTextField textField 
= null;
        JRDesignExpression expression 
= null;

开始画报表主体的title部分,不需要的部分可以不用代码标识出来,title部分放置了报表标题字段,只会出现在第一页的上方

// title
        band = new JRDesignBand();
        band.setHeight(
50);
        textField 
= new JRDesignTextField();
        textField.setBlankWhenNull(
true);
        textField.setX(
0);
        textField.setY(
10);
        textField.setWidth(
500);
        textField.setHeight(
30);
        textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER);
        textField.setStyle(normalStyle);
        textField.setFontSize(
22);
        expression 
= new JRDesignExpression();
        expression.setValueClass(java.lang.String.
class);
        expression.setText(
"$P{ReportTitle}");
        textField.setExpression(expression);
        band.addElement(textField);
        jasperDesign.setTitle(band);
detail部分:排列域的位置,并设置它的值,没一个域的值会最为一个 JRDesignExpression出现
// Detail
        
// pid
        band = new JRDesignBand();
        band.setHeight(
20);

        textField 
= new JRDesignTextField();
        textField.setX(
5);
        textField.setY(
4);
        textField.setWidth(
100);
        textField.setHeight(
15);
        textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);
        textField.setStyle(normalStyle);
        expression 
= new JRDesignExpression();
        expression.setValueClass(java.lang.Integer.
class);
        expression.setText(
"$F{userid}");
        textField.setExpression(expression);
        band.addElement(textField);
        
// name
        textField = new JRDesignTextField();
        textField.setStretchWithOverflow(
true);
        textField.setX(
120);
        textField.setY(
4);
        textField.setWidth(
80);
        textField.setHeight(
15);
        textField.setPositionType(JRElement.POSITION_TYPE_FLOAT);
        textField.setStyle(normalStyle);
        expression 
= new JRDesignExpression();
        expression.setValueClass(java.lang.String.
class);
        expression.setText(
"$F{name}");
        textField.setExpression(expression);
        band.addElement(textField);

        
// sex
        textField = new JRDesignTextField();
        textField.setStretchWithOverflow(
true);
        textField.setX(
200);
        textField.setY(
4);
        textField.setWidth(
30);
        textField.setHeight(
15);
        textField.setPositionType(JRElement.POSITION_TYPE_FLOAT);
        textField.setStyle(normalStyle);
        expression 
= new JRDesignExpression();
        expression.setValueClass(java.lang.String.
class);
        expression.setText(
"$F{sex}");
        textField.setExpression(expression);
        band.addElement(textField);
        
// age
        textField = new JRDesignTextField();
        textField.setStretchWithOverflow(
true);
        textField.setX(
240);
        textField.setY(
4);
        textField.setWidth(
30);
        textField.setHeight(
15);
        textField.setPositionType(JRElement.POSITION_TYPE_FLOAT);
        textField.setStyle(normalStyle);
        expression 
= new JRDesignExpression();
        expression.setValueClass(java.lang.String.
class);
        expression.setText(
"$F{age}");
        textField.setExpression(expression);
        band.addElement(textField);
        
// password
        textField = new JRDesignTextField();
        textField.setStretchWithOverflow(
true);
        textField.setX(
270);
        textField.setY(
4);
        textField.setWidth(
80);
        textField.setHeight(
15);
        textField.setPositionType(JRElement.POSITION_TYPE_FLOAT);
        textField.setStyle(normalStyle);
        expression 
= new JRDesignExpression();
        expression.setValueClass(java.lang.String.
class);
        expression.setText(
"$F{password}");
        textField.setExpression(expression);
        band.addElement(textField);
        
// department
        textField = new JRDesignTextField();
        textField.setStretchWithOverflow(
true);
        textField.setX(
350);
        textField.setY(
4);
        textField.setWidth(
80);
        textField.setHeight(
15);
        textField.setPositionType(JRElement.POSITION_TYPE_FLOAT);
        textField.setStyle(normalStyle);
        expression 
= new JRDesignExpression();
        expression.setValueClass(java.lang.String.
class);
        expression.setText(
"$F{department}");
        textField.setExpression(expression);
        band.addElement(textField);
JasperReport对象可以使用下面这句产生:
JasperCompileManager.compileReport(jasperDesign);
至此一个完整的报表就可以显示出来了。
posted on 2008-12-16 13:27 凌晨风 阅读(2339) 评论(2)  编辑  收藏 所属分类: iReport + JasperReport 系列

FeedBack:
# re: iReport+jasperReport之NoXML
2009-02-17 14:50 | entering
请问如何动态的增在“Summary”中增加“图形报表” 呢?
  回复  更多评论
  
# re: iReport+jasperReport之NoXML
2009-02-17 15:03 | 凌晨风
@entering
图形报表一般在jasper中是以image 变量的形式出现的,这个和普通字段的设计没有区别,主要就是构造你的图形scriplet,再者jasper对jChart图表支持的很到位,你可以多看看它的文档尤其iReport生成的jrxml文件结构  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

<2009年2月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567

常用链接

留言簿(11)

我参与的团队

随笔分类

随笔档案

文章分类

文章档案

新闻分类

新闻档案

收藏夹

围脖

最新随笔

搜索

  •  

最新评论

阅读排行榜

评论排行榜