JasperReport Development Guide and SampleWord格式.docx

上传人:b****5 文档编号:8398949 上传时间:2023-05-11 格式:DOCX 页数:16 大小:442.72KB
下载 相关 举报
JasperReport Development Guide and SampleWord格式.docx_第1页
第1页 / 共16页
JasperReport Development Guide and SampleWord格式.docx_第2页
第2页 / 共16页
JasperReport Development Guide and SampleWord格式.docx_第3页
第3页 / 共16页
JasperReport Development Guide and SampleWord格式.docx_第4页
第4页 / 共16页
JasperReport Development Guide and SampleWord格式.docx_第5页
第5页 / 共16页
JasperReport Development Guide and SampleWord格式.docx_第6页
第6页 / 共16页
JasperReport Development Guide and SampleWord格式.docx_第7页
第7页 / 共16页
JasperReport Development Guide and SampleWord格式.docx_第8页
第8页 / 共16页
JasperReport Development Guide and SampleWord格式.docx_第9页
第9页 / 共16页
JasperReport Development Guide and SampleWord格式.docx_第10页
第10页 / 共16页
JasperReport Development Guide and SampleWord格式.docx_第11页
第11页 / 共16页
JasperReport Development Guide and SampleWord格式.docx_第12页
第12页 / 共16页
JasperReport Development Guide and SampleWord格式.docx_第13页
第13页 / 共16页
JasperReport Development Guide and SampleWord格式.docx_第14页
第14页 / 共16页
JasperReport Development Guide and SampleWord格式.docx_第15页
第15页 / 共16页
JasperReport Development Guide and SampleWord格式.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

JasperReport Development Guide and SampleWord格式.docx

《JasperReport Development Guide and SampleWord格式.docx》由会员分享,可在线阅读,更多相关《JasperReport Development Guide and SampleWord格式.docx(16页珍藏版)》请在冰点文库上搜索。

JasperReport Development Guide and SampleWord格式.docx

importjava.util.Map;

importorg.jfree.chart.ChartFactory;

importorg.jfree.chart.JFreeChart;

importorg.jfree.chart.plot.PiePlot3D;

importorg.jfree.data.general.DefaultPieDataset;

importmon.util.SpringContextUtils;

importcom.achievo.module.training.domain.AdminDistrict;

importcom.achievo.module.training.service.IAdminDistrictService;

importnet.sf.jasperreports.engine.JRDataSource;

importnet.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

importnet.sf.jasperreports.renderers.JFreeChartRenderer;

publicclassReportTestActionextendsReportSupport{

IAdminDistrictServiceadminDistrictService=(IAdminDistrictService)SpringContextUtils.getBean("

adminDistrictService"

);

@Override

publicvoidaddSubReport(JasperReportDataSourceAdapterjrda){

}

//初始化数据

publicJRDataSourceinitJRDataSource(){

//取业务数据

List<

reportRows=adminDistrictService.getAll();

if(null==reportRows){

reportRows=newArrayList<

();

}

JRBeanCollectionDataSourcedataSource=newJRBeanCollectionDataSource(

reportRows);

returndataSource;

publicvoidprepareParam(Mapparam){

reportName="

report_test"

;

ReportTestAction继承于ReportSupport,ReportSupport封装了公用的功能,代码如下:

 

importjava.awt.Graphics2D;

importjava.awt.geom.Rectangle2D;

importjava.io.File;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.util.HashMap;

importorg.jfree.chart.plot.PlotOrientation;

importorg.jfree.data.category.DefaultCategoryDataset;

importorg.jfree.data.xy.XYSeriesCollection;

importmon.util.Struts2Utils;

importcom.achievo.framework.server.web.action.AbstractBaseAction;

publicabstractclassReportSupportextendsAbstractBaseAction{

StringreportName;

Stringpath;

publicstaticfinalStringREPORT_PATH="

com/achievo/module/training/action"

publicStringgetPath(){

returnpath;

publicvoidsetPath(Stringpath){

this.path=path;

publicStringgetReportName(){

returnreportName;

publicvoidsetReportName(StringreportName){

this.reportName=reportName;

publicabstractvoidprepareParam(Mapparam);

publicabstractvoidaddSubReport(JasperReportDataSourceAdapterjrda);

//生成报表文件到前台

publicStringshowPdfReport(){

path=this.getClass().getClassLoader().getResource(

REPORT_PATH).getPath();

/**Parameters*/

Mapparams=newHashMap();

params.put("

sub_dir"

path);

prepareParam(params);

ReportDataSourcerds=newReportDataSource();

//Templatefilepath

rds.setReportTemplatePath(path+"

/"

+reportName

+"

.jrxml"

//Compiledfilepath

rds.setJasperReportPath(path+"

+reportName

.jasper"

//Parameters

rds.setParams(params);

//JRDataSource

rds.setJRDataSource(initJRDataSource());

//OutPutFilename

rds.setOutPutFileName(reportName+"

.pdf"

//Compileandfillvaluetothereport

JasperReportDataSourceAdapterjrda=newJasperReportDataSourceAdapter(

rds);

//Exportreportconstructor

Reportreport=newJasperReportObject();

try{

//添加子报表(如果有)

addSubReport(jrda);

//以PDF方式导出

report.export(Struts2Utils.getRequest(),

Struts2Utils.getResponse(),jrda.getJaspertPrint(),

Struts2Utils.getResponse().getOutputStream(),

Report.REPORT_EXPORT_PDF,rds.getOutPutFileName());

}catch(ReportExceptione){

e.printStackTrace();

}catch(IOExceptione){

returnnull;

publicabstractJRDataSourceinitJRDataSource();

BufferedImageextractImage(JFreeChartchart,intwidth,intheight){

BufferedImageimg=newBufferedImage(width,height,

BufferedImage.TYPE_INT_RGB);

Graphics2Dg2=img.createGraphics();

chart.draw(g2,newRectangle2D.Double(0,0,width,height));

g2.dispose();

returnimg;

JFreeChartcreateChart(XYSeriesCollectiondataSet){

JFreeChartjfreechart=ChartFactory.createXYBarChart(null,"

"

false,

"

dataSet,PlotOrientation.HORIZONTAL,false,false,false);

returnjfreechart;

ReportSupport的实现需要用到下列类:

JasperReportDataSourceAdapter,JasperReportObject,Report,ReportDataSource,ReportException。

map为提供给报表的参数,如日期范围等。

struts.xml配置如下:

<

actionname="

showReport"

class="

com.achievo.module.training.action.ReportTestAction"

method="

showPdfReport"

/>

三、利用iReport开发报表

1.新建数据源

打开iReport,点击下图像插头一样的ReportDatasources,弹出对象框中New,选择"

JavaBeanssetdatasource"

后Next,输入Name为"

demo"

后Save,如下两图:

2.增加类路径

点击“工具”“选项”,选择classpath标签页,点击AddFolder,将项目类路径加入,如下图路径为:

C:

\Users\linli\Documents\workspace\JSReportDemo\build\classes

3.新建报表

点击createanewreport

OpenthisTemplate"

后,输入报表名这里是demoReport后下一步,继续完成。

4.读取bean属性

点击Preview字样右边的按钮设置数据源,在弹出框中选择"

JavaBeanDatasource"

标签页,在Classname中输入类名,点击"

Readattributes"

会列出类中所有属性。

选中所需的属性,点击对话框中间的"

Addselectedfield(s)"

增加到报表字段中。

6.设计报表

将左侧ReportInspector中Fields下刚加入的字段托到报表设计器中,设计完的报表如下图:

7.运行报表

将demoReport.jxml文件复制到项目,启动容器,浏览器下查看报表如下:

四,利用JFreeChart画图

方式一,利用IReport的charttool控件:

将chart拖到报表中,选择类型,如pie:

点击右键,选择ChartData菜单,

需要设置Keyexpression,Valueexpression,Labelexpression,其中$F{disId},$F{name}均来自于数据源,效果如下:

方式二,利用Scriptlet,

创建一个Java类,如JChartReportDemo,继承于JRDefaultScriptlet,重写afterReportInit,代码如下:

importnet.sf.jasperreports.engine.JRDefaultScriptlet;

importnet.sf.jasperreports.engine.JRScriptletException;

publicclassJChartReportDemoextendsJRDefaultScriptlet{

publicvoidafterReportInit()throwsJRScriptletException{

//createpiechartdataset

DefaultPieDatasetdateset=newDefaultPieDataset();

//setdatasetvalue

dateset.setValue("

Chinese"

108);

Math"

110);

English"

74);

ScienceDepartment"

226);

/**//*

*createjfreeChartobject

*thefirstparameterispiecharttitle

*thesecendparameterisdatasetofpiechart

*thethreeparameterisbooleanvalue,createchartnote

*thefourparameterisbooleanvalue,it'

satooltipofmovemouseon

*configurecharttogenerateURLs,It'

sgetaPiePlot3Dobject

*

*/

JFreeChartfreeChart=ChartFactory.createPieChart3D("

ReportPieChart"

dateset,true,true,false);

PiePlot3Dplot3D=(PiePlot3D)freeChart.getPlot();

plot3D.setNoDataMessage("

Nodatatodisplay"

//setvariable"

jfreeChart"

value

this.setVariableValue("

newJFreeChartRenderer(freeChart));

将该Scriptlet加入报表,方法如下:

在Scriptlets中添加刚才编写的JChartReportDemo;

在报表加入一个Image控件,

设置它的属性:

其中ImageExpression设置为$V{jfreeChart},ExpressionClass设为net.sf.jasperreports.engine.JRRenderable。

保存报表,运行效果如下:

方式三,利用参数或JavaBean属性传递数据

在参数中加入如下代码:

@Override

DefaultPieDatasetdateset=newDefaultPieDataset();

param.put("

pfreeChart"

将报表Image控件的ImageExpression设为$P{pfreeChart}。

运行效果如下:

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2