欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

iReport+jasperReport之scriptlet

系統 1850 0

?????????? 轉載自 http://www.blogjava.net/bulktree/archive/2008/12/17/246786.html

???????????? 提起scriptlet就不能不聯想到它的強大功能,jasperReport也是支持scriptlet的哦,先分析一下JasperReport的API吧!
在填充報表時scriplet是一個非常有力的工具,JRAbstractScriptlet.java位于net.sf.jasperreports.engine包下是一個抽象


iReport+jasperReport之scriptlet
beforeReportInit()?,afterReportInit()?,beforePageInit(),afterPageInit(),?beforeColumnInit(),?afterColumnInit()?,beforeGroupInit(String?groupName),afterGroupInit(String?groupName)?
看看這些名字就知道你能完成那些功能,這幾個方法是要求我們實現的,jasperReport給我們提供了一個實現類JRDefaultScriptlet.java,默認的空實現了上面幾個方法,它只是很便利的為我們提供了所需的八個方法的空實現。我們寫自己的scriptlet時需要繼承JRDefaultScriptlet.java這個類實現自己的相應的功能即可。
?先來看一個簡單的例子:
先看看模板文件的處理: iReport+jasperReport之scriptlet
新建時填寫的這個類是下面我們要介紹的繼承自JRDefaultScriptlet.java類,也就是在模板文件中我們要加上如下代碼

scriptletClass="org.bulktree.ireport.scriptlet.ScriptletReportDemo"

完整的模板文件如下:scriptletDemo.jrxml

<? xml?version="1.0"?encoding="UTF-8"?? ?>
<!-- ?Created?with?iReport?-?A?designer?for?JasperReports? -->
<! DOCTYPE?jasperReport?PUBLIC?"http://JasperReports//DTD?Report?Design//EN"?"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd" >
< jasperReport
?????????
name ="scriptletDemo"
?????????columnCount
="1"
?????????printOrder
="Vertical"
?????????orientation
="Portrait"
?????????pageWidth
="595"
?????????pageHeight
="842"
?????????columnWidth
="535"
?????????columnSpacing
="0"
?????????leftMargin
="30"
?????????rightMargin
="30"
?????????topMargin
="20"
?????????bottomMargin
="20"
?????????whenNoDataType
="NoPages"
?????????scriptletClass
="org.bulktree.ireport.scriptlet.ScriptletReportDemo"
?????????isTitleNewPage
="false"
?????????isSummaryNewPage
="false" >
????
< property? name ="ireport.scriptlethandling" ?value ="2" ? />
????
< property? name ="ireport.encoding" ?value ="UTF-8" ? />
????
< import? value ="java.util.*" ? />
????
< import? value ="net.sf.jasperreports.engine.*" ? />
????
< import? value ="net.sf.jasperreports.engine.data.*" ? />

????
< parameter? name ="ReportTitle" ?isForPrompting ="true" ?class ="java.lang.String" />

????????
< background >
????????????
< band? height ="0" ??isSplitAllowed ="true" ? >
????????????
</ band >
????????
</ background >
????????
< title >
????????????
< band? height ="20" ??isSplitAllowed ="true" ? >
????????????????
< textField? isStretchWithOverflow ="false" ?isBlankWhenNull ="false" ?evaluationTime ="Now" ?hyperlinkType ="None" ??hyperlinkTarget ="Self" ? >
????????????????????
< reportElement
????????????????????????
mode ="Opaque"
????????????????????????x
="193"
????????????????????????y
="0"
????????????????????????width
="134"
????????????????????????height
="18"
????????????????????????backcolor
="#FFCC33"
????????????????????????key
="textField" />
????????????????????
< box ></ box >
????????????????????
< textElement? textAlignment ="Center" ?verticalAlignment ="Middle" >
????????????????????????
< font? pdfFontName ="Helvetica-Bold" ?size ="12" ?isBold ="true" />
????????????????????
</ textElement >
????????????????
< textFieldExpression??? class ="java.lang.String" > <![CDATA[ $P{ReportTitle} ]]> </ textFieldExpression >
????????????????
</ textField >
????????????
</ band >
????????
</ title >
????????
< pageHeader >
????????????
< band? height ="0" ??isSplitAllowed ="true" ? >
????????????
</ band >
????????
</ pageHeader >
????????
< columnHeader >
????????????
< band? height ="0" ??isSplitAllowed ="true" ? >
????????????
</ band >
????????
</ columnHeader >
????????
< detail >
????????????
< band? height ="264" ??isSplitAllowed ="true" ? >
????????????????
< textField? isStretchWithOverflow ="false" ?isBlankWhenNull ="false" ?evaluationTime ="Now" ?hyperlinkType ="None" ??hyperlinkTarget ="Self" ? >
????????????????????
< reportElement
????????????????????????
x ="85"
????????????????????????y
="20"
????????????????????????width
="329"
????????????????????????height
="61"
????????????????????????forecolor
="#FF0099"
????????????????????????key
="textField-1" />
????????????????????
< box ></ box >
????????????????????
< textElement? textAlignment ="Center" ?verticalAlignment ="Middle" >
????????????????????????
< font? pdfFontName ="Helvetica-Bold" ?size ="20" ?isBold ="true" />
????????????????????
</ textElement >
????????????????
< textFieldExpression??? class ="java.lang.String" > <![CDATA[ $P{REPORT_SCRIPTLET}.showInfor() ]]> </ textFieldExpression >
????????????????
</ textField >
????????????
</ band >
????????
</ detail >
????????
< columnFooter >
????????????
< band? height ="0" ??isSplitAllowed ="true" ? >
????????????
</ band >
????????
</ columnFooter >
????????
< pageFooter >
????????????
< band? height ="0" ??isSplitAllowed ="true" ? >
????????????
</ band >
????????
</ pageFooter >
????????
< summary >
????????????
< band? height ="50" ??isSplitAllowed ="true" ? >
????????????
</ band >
????????
</ summary >
</ jasperReport >

下來看看怎么實現我們自己的scriplet

ScriptletReportDemo.java

package ?org.bulktree.ireport.scriptlet;

import ?net.sf.jasperreports.engine.JRDefaultScriptlet;
import ?net.sf.jasperreports.engine.JRScriptletException;

/**
?*?
?*?
@author ?bulktree?Email:?laoshulin@gmail.com
?*?@Nov?26,?2008
?
*/

public ? class ?ScriptletReportDemo? extends ?JRDefaultScriptlet? {

????@Override
????
public ? void ?afterColumnInit()? throws ?JRScriptletException? {
????????System.out.println(
" **************************************afterColumnInit()************************************** " );
????}


????@Override
????
public ? void ?afterDetailEval()? throws ?JRScriptletException? {
????????System.out.println(
" **************************************afterDetailEval()************************************** " );
????}


????@Override
????
public ? void ?afterGroupInit(String?groupName)? throws ?JRScriptletException? {
????????System.out.println(
" **************************************afterDetailEval()************************************** " );
????}


????@Override
????
public ? void ?afterPageInit()? throws ?JRScriptletException? {
????????System.out.println(
" **************************************afterPageInit()************************************** " );
????}


????@Override
????
public ? void ?afterReportInit()? throws ?JRScriptletException? {
????????System.out.println(
" **************************************afterReportInit()?begin************************************** " );
????????
????????String?str?
= ?(String)? this .getParameterValue( " ReportTitle " );
????????System.out.println(
" report?title=====>>>> " + str);
????????
????????str?
+= ?str.subSequence( 0 ,?str.length() - 2 );
????????
this .setParameterValue( " ReportTitle " ,?str);
????????System.out.println(
" **************************************afterReportInit()?end************************************** " );
????}


????@Override
????
public ? void ?beforeColumnInit()? throws ?JRScriptletException? {
????????System.out.println(
" **************************************beforeColumnInit()************************************** " );
????}


????@Override
????
public ? void ?beforeDetailEval()? throws ?JRScriptletException? {
????????System.out.println(
" **************************************beforeDetailEval()************************************** " );
????}


????@Override
????
public ? void ?beforeGroupInit(String?groupName)? throws ?JRScriptletException? {
????????System.out.println(
" **************************************beforeGroupInit()************************************** " );
????}


????@Override
????
public ? void ?beforePageInit()? throws ?JRScriptletException? {
????????System.out.println(
" **************************************beforePageInit()************************************** " );
????}


????@Override
????
public ? void ?beforeReportInit()? throws ?JRScriptletException? {
????????System.out.println(
" **************************************beforeReportInit()************************************** " );
????}


????
public ?String?showInfor()? throws ?JRScriptletException? {
????????
return ? " the?is?scriptlet?scriptlet?scriptlet?the,sscriptlet?report?the?is?ascriptlet?report?this?is?a?scriptlet?report?this?is?a?scriptlet?report " ;
????}

????
}

這段代碼最后一個方法是我們自己的加的,用來在報表上顯示一段文本。我們知道對于一個Field、Parameter、Variable,JasperReport分別采用$F{FieldName}、$P{Parametername}、$V{VariableName}來引用,而如果要引用ScriptletReportDemo.java類的showInfor()返回字符串顯示在報表上,看看這個就知道了 iReport+jasperReport之scriptlet

在afterReportInit方法中我們把parameter字段取出來前后添上五個*號再set進去
????下來寫一個test類測試一下:

package ?org.bulktree.ireport.scriptlet;

import ?java.io.File;
import ?java.io.FileInputStream;
import ?java.io.InputStream;
import ?java.util.HashMap;

import ?net.sf.jasperreports.engine.JREmptyDataSource;
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.view.JasperViewer;

/**
?*?
?*?
@author ?bulktree?Email:?laoshulin@gmail.com
?*?@Nov?27,?2008
?
*/

public ? class ?ScriptletTestMain? {

????
public ? static ? void ?main(String[]?args)? {
????????String?path?
= ? " D:/workspace/scriptletDemo.jrxml " ;

????????File?file?
= ? new ?File(path);
????????InputStream?in;
????????
try ? {

????????????HashMap?parameters?
= ? new ?HashMap();
????????????parameters.put(
" ReportTitle " ,? " LAOSHULIN " );
????????????in?
= ? new ?FileInputStream(file);
????????????JasperReport?jasperReport?
= ?JasperCompileManager.compileReport(in);
????????????JasperPrint?jasperPrint?
= ?JasperFillManager.fillReport(jasperReport,
????????????????????parameters,?
new ?JREmptyDataSource());
????????????JasperViewer?viewer?
= ? new ?JasperViewer(jasperPrint);
????????????viewer.setVisible(
true );

????????}
? catch ?(Exception?e)? {
????????????
// ?TODO?Auto-generated?catch?block
????????????e.printStackTrace();
????????}


????}

}


效果不錯吧:

iReport+jasperReport之scriptlet

getParameterValue、setParameterValue方法可以操作Parameter,Field/Variable該怎么set呢?
????看看?JRAbstractScriptlet.java類的這個方法:

public ? void ?setData(
????????Map?parsm,
????????Map?fldsm,
????????Map?varsm,
????????JRFillGroup[]?grps
????????)
????
{
????????parametersMap?
= ?parsm;
????????fieldsMap?
= ?fldsm;
????????variablesMap?
= ?varsm;
????????groups?
= ?grps;
????}

????????似乎有點意思,我們可以通過這個方法把我們期望的數據組裝成Map然后set進去,可是要只針對個別字段怎么處理呢,調用此方法似乎不太合常理,仔細查看API卻沒有實際能調用的API吧!這個似乎不太合乎,仔細看看確實沒有調用的,至少目前我還是沒有發現,怎么辦 自己寫吧!
????設置Field方法:

public ? void ?setFieldValue(String?fieldName,?Object?value)? throws ?JRScriptletException
????
{
????????JRFillField?field?
= ?(JRFillField) this .fieldsMap.get(fieldName);
????????
if ?(field? == ? null )
????????
{
????????????
throw ? new ?JRScriptletException( " FieldName?not?found?:? " ? + ?fieldName);
????????}

????????
????????field.setValue(value);
????}

????設置Variable方法:

public ? void ?setVariableValue(String?variableName,?Object?value)? throws ?JRScriptletException
????
{
????????JRFillVariable?variable?
= ?(JRFillVariable) this .variablesMap.get(variableName);
????????
if ?(variable? == ? null )
????????
{
????????????
throw ? new ?JRScriptletException( " Variable?not?found?:? " ? + ?variableName);
????????}

????????
????????
if ?(value? != ? null ? && ? ! variable.getValueClass().isInstance(value)?)
????????
{
????????????
throw ? new ?JRScriptletException( " Incompatible?value?assigned?to?variable? " ? + ?variableName? + ? " .?Expected? " ? + ?variable.getValueClassName()? + ? " . " );
????????}

????????
????????variable.setValue(value);
????}

????OK!這樣我們就可以針對報表上的每一個字段處理了,測試通過 代碼就不貼了哦,寫上篇的時候忘記這兩個方法是我自己加的,查看API時才發現所以來了個續

iReport+jasperReport之scriptlet


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产一级做a爰片久久毛片 欧美一区欧美二区 | 国精品人妻无码一区二区三区性色 | 91蝌蚪九色 | 久久久黄色 | 在线二区人妖系列 | 亚洲三级视频 | bt日韩| 奇米奇米777 | 欧美v在线 | 欧美视频观看 | 91精品国产日韩91久久久久久 | 亚洲精品免费在线观看 | 成人免费大片a毛片 | 久久久精品免费观看 | 91麻豆精品一区二区三区 | 国产精品美女久久久 | 蜜桃91麻豆 | 欧美一级做| 国产精品欧美一区二区三区不卡 | 色接久久 | 丝袜美腿视频一区二区三区 | 精品无人乱码高清 | 精久久久| 人人99| 亚洲国产日韩a在线亚洲 | 美乳在线观看 | 老司机精品视频个人在观看 | 亚洲视频在线看 | 日本喷潮 | 国产精品日本一区二区不卡视频 | 国产va免费精品观看精品 | 国产精品久久久久久亚洲伦理 | 亚洲视频2020| 一级毛片 在线播放 | 国产区视频在线观看 | 亚洲国产精品久久婷婷 | 美女下面被cao出水 玖玖玖影院 | 青青草娱乐视频 | 黄色网址你懂的 | 九九99国产精品视频 | 亚洲综合图片人成综合网 |