您现在的位置:首页 > IT频道 > 正文

Jasperreport基本介绍(Jasperreport与Ireport介绍)

时间:2022-01-01 19:08:22    来源:脚本之家    

java代码设计模板,数据源为对象数组。

用java实现模板设计,我们就直接粘贴代码,先要将jasperreport相关的jar包导入进来:

我的代码结构如图所示:

WebappDataSource.java

/*

* JasperReports - Free Java Reporting Library.

* Copyright (C) 2001 - 2009 Jaspersoft Corporation. All rights reserved.

* http://www.jaspersoft.com

*

* Unless you have purchased a commercial license agreement from Jaspersoft,

* the following license terms apply:

*

* This program is part of JasperReports.

*

* JasperReports is free software: you can redistribute it and/or modify

* it under the terms of the GNU Lesser General Public License as published by

* the Free Software Foundation, either version 3 of the License, or

* (at your option) any later version.

*

* JasperReports is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

* GNU Lesser General Public License for more details.

* You should have received a copy of the GNU Lesser General Public License

* along with JasperReports. If not, see <http://www.gnu.org/licenses/>.

*/

package datasource;

import net.sf.jasperreports.engine.JRDataSource;

import net.sf.jasperreports.engine.JRException;

import net.sf.jasperreports.engine.JRField;

/**

* @author Teodor Danciu (teodord@users.sourceforge.net)

* @version $Id: WebappDataSource.java 3031 2009-08-27 11:14:57Z teodord $

*/

public class WebappDataSource implements JRDataSource

{

/**

*

*/

private Object[][] data =

{

{"Berne", new Integer(22), "Bill", "250 - 20th Ave."},

{"Berne", new Integer(9), "James Schneider", "277 Seventh Av."},

{"Boston", new Integer(32), "Michael Ott", "339 College Av."},

{"Boston", new Integer(23), "Julia Heiniger", "358 College Av."},

{"Chicago", new Integer(39), "Mary Karsen", "202 College Av."},

{"Chicago", new Integer(35), "George Karsen", "412 College Av."},

{"Chicago", new Integer(11), "Julia White", "412 Upland Pl."},

{"Dallas", new Integer(47), "Janet Fuller", "445 Upland Pl."},

{"Dallas", new Integer(43), "Susanne Smith", "2 Upland Pl."},

{"Dallas", new Integer(40), "Susanne Miller", "440 - 20th Ave."},

{"Dallas", new Integer(36), "John Steel", "276 Upland Pl."},

{"Dallas", new Integer(37), "Michael Clancy", "19 Seventh Av."},

{"Dallas", new Integer(19), "Susanne Heiniger", "86 - 20th Ave."},

{"Dallas", new Integer(10), "Anne Fuller", "135 Upland Pl."},

{"Dallas", new Integer(4), "Sylvia Ringer", "365 College Av."},

{"Dallas", new Integer(0), "Laura Steel", "429 Seventh Av."},

{"Lyon", new Integer(38), "Andrew Heiniger", "347 College Av."},

{"Lyon", new Integer(28), "Susanne White", "74 - 20th Ave."},

{"Lyon", new Integer(17), "Laura Ott", "443 Seventh Av."},

{"Lyon", new Integer(2), "Anne Miller", "20 Upland Pl."},

{"New York", new Integer(46), "Andrew May", "172 Seventh Av."},

{"New York", new Integer(44), "Sylvia Ott", "361 College Av."},

{"New York", new Integer(41), "Bill King", "546 College Av."},

{"Oslo", new Integer(45), "Janet May", "396 Seventh Av."},

{"Oslo", new Integer(42), "Robert Ott", "503 Seventh Av."},

{"Paris", new Integer(25), "Sylvia Steel", "269 College Av."},

{"Paris", new Integer(18), "Sylvia Fuller", "158 - 20th Ave."},

{"Paris", new Integer(5), "Laura Miller", "294 Seventh Av."},

{"San Francisco", new Integer(48), "Robert White", "549 Seventh Av."},

{"San Francisco", new Integer(7), "James Peterson", "231 Upland Pl."}

};

private int index = -1;

/**

*

*/

public WebappDataSource()

{

}

/**

*

*/

public boolean next() throws JRException

{

index++;

return (index < data.length);

}

/**

*

*/

public Object getFieldValue(JRField field) throws JRException

{

Object value = null;

String fieldName = field.getName();

if ("city".equals(fieldName))

{

value = data[index][0];

}

else if ("ID".equals(fieldName))

{

value = data[index][1];

}

else if ("name".equals(fieldName))

{

value = data[index][2];

}

else if ("address".equals(fieldName))

{

value = data[index][3];

}

return value;

}

}

next()  实现了 JRDataSource 中的方法.判断是否还有下一个. 

getFieldValue(JRField field) 实现了JRDataSource 中的方法. field 是对应报表中的要填充的字段的名称. 

模板设计代码,java实现模板时,要贯彻一个思想就设计思路要与XML模板中组成结构一致,这也是我的心得体会之一。

StaticText.java

package test;

import java.io.File;

import java.util.HashMap;

import net.sf.jasperreports.engine.JRAlignment;

import net.sf.jasperreports.engine.JRElement;

import net.sf.jasperreports.engine.JRException;

import net.sf.jasperreports.engine.JasperCompileManager;

import net.sf.jasperreports.engine.JasperFillManager;

import net.sf.jasperreports.engine.JasperPrint;

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.JRDesignParameter;

import net.sf.jasperreports.engine.design.JRDesignReportFont;

import net.sf.jasperreports.engine.design.JRDesignStyle;

import net.sf.jasperreports.engine.design.JRDesignTextField;

import net.sf.jasperreports.engine.design.JasperDesign;

import net.sf.jasperreports.engine.util.JRLoader;

import net.sf.jasperreports.view.JasperDesignViewer;

import net.sf.jasperreports.view.JasperViewer;

import datasource.WebappDataSource;

public class StaticText {

JRDesignReportFont font1 = new JRDesignReportFont();

// TODO Auto-generated method stub

/**

* Method name: main <BR>

* Description: main <BR>

* Remark: <BR>

* @param args  void<BR>

*/

public static void main(String[] args) {

JRDesignReportFont font1 = new JRDesignReportFont();

font1.setFontSize(12);

try {

// JRDesignStaticText text1=ReportUtil.getStaticText(

// 110,0,400,22,null,JRTextElement.TEXT_ALIGN_CENTER,font1,"city");

// JRDesignStaticText text2=ReportUtil.getStaticText(

// 110,0,400,22,null,JRTextElement.TEXT_ALIGN_CENTER,font1,"name");

// JRDesignStaticText text3=ReportUtil.getStaticText(

// 110,0,400,22,null,JRTextElement.TEXT_ALIGN_CENTER,font1,"address");

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);

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);

JRDesignParameter parameter = new JRDesignParameter();

parameter.setName("ReportTitle");

parameter.setValueClass(java.lang.String.class);

jasperDesign.addParameter(parameter);

// Fields

JRDesignField field = new JRDesignField();

field.setName("city");

field.setValueClass(java.lang.String.class);

jasperDesign.addField(field);

field = new JRDesignField();

field.setName("ID");

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("address");

field.setValueClass(java.lang.String.class);

jasperDesign.addField(field);

JRDesignBand band = null;

JRDesignTextField textField = null;

JRDesignExpression expression = null;

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.String.class);

expression.setText("$F{city}");

textField.setExpression(expression);

band.addElement(textField);

textField = new JRDesignTextField();

textField.setStretchWithOverflow(true);

textField.setX(120);

textField.setY(4);

textField.setWidth(100);

textField.setHeight(15);

textField.setPositionType(JRElement.POSITION_TYPE_FLOAT);

textField.setStyle(normalStyle);

expression = new JRDesignExpression();

expression.setValueClass(java.lang.Integer.class);

expression.setText("$F{ID}");

textField.setExpression(expression);

band.addElement(textField);

textField = new JRDesignTextField();

textField.setStretchWithOverflow(true);

textField.setX(220);

textField.setY(4);

textField.setWidth(100);

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);

textField = new JRDesignTextField();

textField.setStretchWithOverflow(true);

textField.setX(400);

textField.setY(4);

textField.setWidth(100);

textField.setHeight(15);

textField.setPositionType(JRElement.POSITION_TYPE_FLOAT);

textField.setStyle(normalStyle);

expression = new JRDesignExpression();

expression.setValueClass(java.lang.String.class);

expression.setText("$F{address}");

textField.setExpression(expression);

band.addElement(textField);

jasperDesign.setDetail(band);

HashMap<String, String> titleParameters = new HashMap<String, String>();

titleParameters.put("ReportTitle", "helloworld");

File reportFile= new File("E:/billin.jasper");

JasperCompileManager.compileReportToFile(jasperDesign,reportFile.getPath());

JasperReport jasperReport=(JasperReport)JRLoader.loadObject(reportFile.getPath());

JasperPrint jasperPrint=JasperFillManager.fillReport( jasperReport,titleParameters,new WebappDataSource());

System.out.println("--------"+jasperPrint.getName());

JasperViewer viewer = new JasperViewer(jasperPrint);

viewer.setTitle("");

viewer.setVisible(true);

//             JasperDesignViewer viewer = new JasperDesignViewer(jasperReport);

//             viewer.setVisible(true);

} catch (JRException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

下面就可以运行,查看报表结果:

关于jasperreport的sample

1)运行sample之前需要安装ant服务器

2)进入到某个sample目录中,输入ant,之后再输入ant view

3)有些sample需要运行sample自带的数据,则在hsqldb目录中,输入ant runServer命令启动数据库,再到sample目录中,输入ant,之后再输入ant view。

上一页 1 2 3下一页阅读全文

关键词: Jasperreport Ireport

凡本网注明“XXX(非中国微山网)提供”的作品,均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和其真实性负责。

特别关注