iReport+jasperReport之JDBC數(shù)據(jù)源
iReport畫(huà)出來(lái)的報(bào)表可以滿足我們大部分的需要,所以采用iReport 編輯jrxml文件是我們的首選啦!當(dāng)然掌握jrxml文件的結(jié)構(gòu)也是必須的。
????????采用JDBC數(shù)據(jù)源必須有數(shù)據(jù)庫(kù)支持啊!我們新建一個(gè)test表,其中有pid、name、sex、password、department、age字段,為了方便期間我們定義這些字段全部為String類(lèi)型。
????????在iReport中新建一報(bào)表,報(bào)表有title、pageHeader、columnHeader、detail、columnFooter、pageFooter、lastPageFooter、summary等部分,被成為Report section
title:顧名思義是指報(bào)表的標(biāo)題哦,它會(huì)顯示在報(bào)表的最上面,如果有多頁(yè)只會(huì)出現(xiàn)在第一頁(yè)的最上面。pageHeader:每頁(yè)的標(biāo)題,如果有多頁(yè)每頁(yè)的開(kāi)始都會(huì)顯示此部分內(nèi)容。
columnHeader:顯示報(bào)表的列頭不經(jīng)常使用。
detail:這個(gè)就不用說(shuō)了吧!
有header就會(huì)出現(xiàn)footer啦!lastPageFooter只會(huì)出現(xiàn)在最后一頁(yè)。summay會(huì)出現(xiàn)在每一頁(yè)數(shù)據(jù)上,主要是為了展示一些統(tǒng)計(jì)信息,比如當(dāng)前的時(shí)間,頁(yè)數(shù)信息啦!
新建一parameter
此參數(shù)可作為報(bào)表的標(biāo)題使用,我們?cè)诔绦蛑袆?dòng)態(tài)的傳遞給報(bào)表。
????document structure---》parameter里找到reportTitle參數(shù)拖至title區(qū)域,右擊編輯reportTitle域的屬性,在font選項(xiàng)里找到Markup設(shè)置為HTML,TextField選項(xiàng)里設(shè)置Text Field Expreesion為
"
<
font?
color
='blue'?
size
='5'>"+$P{reportTitle}+"</font>"
下來(lái)就是設(shè)置報(bào)表的Field字段了,不需要我們一個(gè)個(gè)的新建那些字段啦!
選擇Data--->Report Query在Report Query選項(xiàng)里選擇Query Language為SQL,寫(xiě)入SqL語(yǔ)句
select
?
*
?
from
?test?
order
?
by
?pid?
DESC
這時(shí)所有的field會(huì)出現(xiàn)在下面(SQL是正確的)
關(guān)閉對(duì)話框在document structure---》field中就會(huì)出現(xiàn)我們需要的field,之后拖到相應(yīng)的位置,關(guān)于怎么美化報(bào)表這個(gè)本人也不是很懂哦!
如果需要顯示一下當(dāng)前的頁(yè)數(shù)信息也可以自己托動(dòng)Variables里的PAGE_NUMBER完成頁(yè)數(shù)的顯示。完整的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
="Person"
?????????columnCount
="1"
?????????printOrder
="Vertical"
?????????orientation
="Portrait"
?????????pageWidth
="595"
?????????pageHeight
="842"
?????????columnWidth
="535"
?????????columnSpacing
="0"
?????????leftMargin
="30"
?????????rightMargin
="30"
?????????topMargin
="20"
?????????bottomMargin
="20"
?????????whenNoDataType
="NoPages"
?????????isTitleNewPage
="false"
?????????isSummaryNewPage
="false"
>
????
<
property?
name
="ireport.scriptlethandling"
?value
="0"
?
/>
????
<
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"
/>
????
<
queryString
>
<![CDATA[
select?*?from?test?order?by?pid?DESC
]]>
</
queryString
>
????
<
field?
name
="pid"
?class
="java.lang.String"
/>
????
<
field?
name
="name"
?class
="java.lang.String"
/>
????
<
field?
name
="sex"
?class
="java.lang.String"
/>
????
<
field?
name
="password"
?class
="java.lang.String"
/>
????
<
field?
name
="department"
?class
="java.lang.String"
/>
????
<
field?
name
="age"
?class
="java.lang.String"
/>
????????
<
background
>
????????????
<
band?
height
="6"
??isSplitAllowed
="true"
?
>
????????????
</
band
>
????????
</
background
>
????????
<
title
>
????????????
<
band?
height
="29"
??isSplitAllowed
="true"
?
>
????????????????
<
textField?
isStretchWithOverflow
="false"
?isBlankWhenNull
="false"
?evaluationTime
="Now"
?hyperlinkType
="None"
??hyperlinkTarget
="Self"
?
>
????????????????????
<
reportElement
????????????????????????
x
="138"
????????????????????????y
="0"
????????????????????????width
="258"
????????????????????????height
="29"
????????????????????????key
="textField"
/>
????????????????????
<
box
></
box
>
????????????????????
<
textElement?
textAlignment
="Center"
?verticalAlignment
="Middle"
?markup
="html"
>
????????????????????????
<
font?
pdfFontName
="Helvetica-Bold"
?isBold
="true"
/>
????????????????????
</
textElement
>
????????????????
<
textFieldExpression???
class
="java.lang.String"
>
<![CDATA[
"<font?color='blue'?size='5'>"+$P{reportTitle}+"</font>"
]]>
</
textFieldExpression
>
????????????????
</
textField
>
????????????
</
band
>
????????
</
title
>
????????
<
pageHeader
>
????????????
<
band?
height
="0"
??isSplitAllowed
="true"
?
>
????????????
</
band
>
????????
</
pageHeader
>
????????
<
columnHeader
>
????????????
<
band?
height
="0"
??isSplitAllowed
="true"
?
>
????????????
</
band
>
????????
</
columnHeader
>
????????
<
detail
>
????????????
<
band?
height
="22"
??isSplitAllowed
="true"
?
>
????????????????
<
textField?
isStretchWithOverflow
="false"
?isBlankWhenNull
="false"
?evaluationTime
="Now"
?hyperlinkType
="None"
??hyperlinkTarget
="Self"
?
>
????????????????????
<
reportElement
????????????????????????
x
="0"
????????????????????????y
="0"
????????????????????????width
="100"
????????????????????????height
="18"
????????????????????????key
="textField"
/>
????????????????????
<
box
></
box
>
????????????????????
<
textElement?
verticalAlignment
="Top"
>
????????????????????????
<
font
/>
????????????????????
</
textElement
>
????????????????
<
textFieldExpression???
class
="java.lang.String"
>
<![CDATA[
$F{pid}
]]>
</
textFieldExpression
>
????????????????
</
textField
>
????????????????
<
textField?
isStretchWithOverflow
="false"
?isBlankWhenNull
="false"
?evaluationTime
="Now"
?hyperlinkType
="None"
??hyperlinkTarget
="Self"
?
>
????????????????????
<
reportElement
????????????????????????
x
="100"
????????????????????????y
="0"
????????????????????????width
="93"
????????????????????????height
="18"
????????????????????????key
="textField"
/>
????????????????????
<
box
></
box
>
????????????????????
<
textElement?
verticalAlignment
="Top"
>
????????????????????????
<
font
/>
????????????????????
</
textElement
>
????????????????
<
textFieldExpression???
class
="java.lang.String"
>
<![CDATA[
$F{name}
]]>
</
textFieldExpression
>
????????????????
</
textField
>
????????????????
<
textField?
isStretchWithOverflow
="false"
?isBlankWhenNull
="false"
?evaluationTime
="Now"
?hyperlinkType
="None"
??hyperlinkTarget
="Self"
?
>
????????????????????
<
reportElement
????????????????????????
x
="193"
????????????????????????y
="0"
????????????????????????width
="58"
????????????????????????height
="18"
????????????????????????key
="textField"
/>
????????????????????
<
box
></
box
>
????????????????????
<
textElement?
verticalAlignment
="Top"
>
????????????????????????
<
font
/>
????????????????????
</
textElement
>
????????????????
<
textFieldExpression???
class
="java.lang.String"
>
<![CDATA[
$F{sex}
]]>
</
textFieldExpression
>
????????????????
</
textField
>
????????????????
<
textField?
isStretchWithOverflow
="false"
?isBlankWhenNull
="false"
?evaluationTime
="Now"
?hyperlinkType
="None"
??hyperlinkTarget
="Self"
?
>
????????????????????
<
reportElement
????????????????????????
x
="251"
????????????????????????y
="0"
????????????????????????width
="100"
????????????????????????height
="18"
????????????????????????key
="textField"
/>
????????????????????
<
box
></
box
>
????????????????????
<
textElement?
verticalAlignment
="Top"
>
????????????????????????
<
font
/>
????????????????????
</
textElement
>
????????????????
<
textFieldExpression???
class
="java.lang.String"
>
<![CDATA[
$F{password}
]]>
</
textFieldExpression
>
????????????????
</
textField
>
????????????????
<
textField?
isStretchWithOverflow
="false"
?isBlankWhenNull
="false"
?evaluationTime
="Now"
?hyperlinkType
="None"
??hyperlinkTarget
="Self"
?
>
????????????????????
<
reportElement
????????????????????????
x
="351"
????????????????????????y
="0"
????????????????????????width
="100"
????????????????????????height
="18"
????????????????????????key
="textField"
/>
????????????????????
<
box
></
box
>
????????????????????
<
textElement?
verticalAlignment
="Top"
>
????????????????????????
<
font
/>
????????????????????
</
textElement
>
????????????????
<
textFieldExpression???
class
="java.lang.String"
>
<![CDATA[
$F{department}
]]>
</
textFieldExpression
>
????????????????
</
textField
>
????????????????
<
textField?
isStretchWithOverflow
="false"
?pattern
=""
?isBlankWhenNull
="false"
?evaluationTime
="Now"
?hyperlinkType
="None"
??hyperlinkTarget
="Self"
?
>
????????????????????
<
reportElement
????????????????????????
x
="451"
????????????????????????y
="0"
????????????????????????width
="84"
????????????????????????height
="18"
????????????????????????key
="textField"
/>
????????????????????
<
box
></
box
>
????????????????????
<
textElement?
verticalAlignment
="Top"
>
????????????????????????
<
font
/>
????????????????????
</
textElement
>
????????????????
<
textFieldExpression???
class
="java.lang.String"
>
<![CDATA[
$F{age}
]]>
</
textFieldExpression
>
????????????????
</
textField
>
????????????????
<
line?
direction
="TopDown"
>
????????????????????
<
reportElement
????????????????????????
x
="0"
????????????????????????y
="17"
????????????????????????width
="535"
????????????????????????height
="1"
????????????????????????key
="line-1"
/>
????????????????????
<
graphicElement?
stretchType
="NoStretch"
/>
????????????????
</
line
>
????????????
</
band
>
????????
</
detail
>
????????
<
columnFooter
>
????????????
<
band?
height
="0"
??isSplitAllowed
="true"
?
>
????????????
</
band
>
????????
</
columnFooter
>
????????
<
pageFooter
>
????????????
<
band?
height
="24"
??isSplitAllowed
="true"
?
>
????????????????
<
textField?
isStretchWithOverflow
="false"
?isBlankWhenNull
="false"
?evaluationTime
="Now"
?hyperlinkType
="None"
??hyperlinkTarget
="Self"
?
>
????????????????????
<
reportElement
????????????????????????
x
="387"
????????????????????????y
="5"
????????????????????????width
="22"
????????????????????????height
="18"
????????????????????????key
="textField"
/>
????????????????????
<
box
></
box
>
????????????????????
<
textElement?
textAlignment
="Center"
?verticalAlignment
="Middle"
>
????????????????????????
<
font
/>
????????????????????
</
textElement
>
????????????????
<
textFieldExpression???
class
="java.lang.Integer"
>
<![CDATA[
$V{PAGE_NUMBER}
]]>
</
textFieldExpression
>
????????????????
</
textField
>
????????????????
<
staticText
>
????????????????????
<
reportElement
????????????????????????
x
="373"
????????????????????????y
="5"
????????????????????????width
="14"
????????????????????????height
="17"
????????????????????????key
="staticText-2"
/>
????????????????????
<
box
></
box
>
????????????????????
<
textElement?
textAlignment
="Center"
?verticalAlignment
="Middle"
>
????????????????????????
<
font?
pdfFontName
="Helvetica-Bold"
?size
="12"
?isBold
="true"
/>
????????????????????
</
textElement
>
????????????????
<
text
>
<![CDATA[
第
]]>
</
text
>
????????????????
</
staticText
>
????????????????
<
staticText
>
????????????????????
<
reportElement
????????????????????????
x
="409"
????????????????????????y
="5"
????????????????????????width
="14"
????????????????????????height
="17"
????????????????????????key
="staticText-3"
/>
????????????????????
<
box
></
box
>
????????????????????
<
textElement?
textAlignment
="Center"
?verticalAlignment
="Middle"
>
????????????????????????
<
font?
pdfFontName
="Helvetica-Bold"
?size
="12"
?isBold
="true"
/>
????????????????????
</
textElement
>
????????????????
<
text
>
<![CDATA[
頁(yè)
]]>
</
text
>
????????????????
</
staticText
>
????????????
</
band
>
????????
</
pageFooter
>
????????
<
summary
>
????????????
<
band?
height
="0"
??isSplitAllowed
="true"
?
>
????????????
</
band
>
????????
</
summary
>
</
jasperReport
>
????我們可以直接使用ireport查看預(yù)覽效果,但是大部分我們是在應(yīng)用程序中使用的哦!我們看看我們?cè)趺醋屵@個(gè)jrxml模板文件工作呢,???
File?file?
=
?
new
?File(
"
Person.jrxml
"
);
????????????InputStream?in?
=
?
new
?FileInputStream(file);?
//
?編譯報(bào)表?jasperReport?=
????????????jasperReport?
=
?JasperCompileManager.compileReport(in);
編譯文件是很耗時(shí)的工作,通常這個(gè)由iReport去做我們使用jasper文件即可,
jasperReport?
=
?(JasperReport)?JRLoader
????????????????????.loadObject(
"
D:\\workspace\\Person.jasper
"
);
產(chǎn)生了JasperReport對(duì)象下來(lái)就是要填充數(shù)據(jù)了,采用JDBC方式我們需要一個(gè)打開(kāi)的connection(數(shù)據(jù)庫(kù)連接),
還有報(bào)表需要的parameter:
public
?HashMap?getMap()?
{
????????HashMap?map?
=
?
new
?HashMap();
????????map.put(
"
reportTitle
"
,?
"
laoshulin
"
);
????????
return
?map;
????}
?
jasperPrint?
=
?JasperFillManager
????????????????????.fillReport(jasperReport,?getMap,?getConn());
針對(duì)jasperPrint對(duì)象JasperReport有很多的API可以提供各種方式的預(yù)覽或是生成文件
我只說(shuō)說(shuō)JasperViewer吧!其它的看看api或是google一下就知道了
JasperViewer?jasperViewer?
=
?
new
?JasperViewer(jasperPrint);
JasperViewer 繼承自JFrame類(lèi),
jasperViewer.setVisible(
true
);
這樣就可以預(yù)覽報(bào)表了,
JRViewer這個(gè)類(lèi)繼承Jpanel,我們可以在web中使用它,后面介紹客戶端打印時(shí)再詳細(xì)介紹。
????JDBC數(shù)據(jù)源很簡(jiǎn)單主要是SQL的功底,但是它是最基礎(chǔ)的,網(wǎng)上一大堆這方面的介紹哦,可以參考別人的多看看哦,我寫(xiě)的這些都有點(diǎn)語(yǔ)無(wú)倫次了,主要是我自己不怎么寫(xiě)東西,多以代碼的形式留給自己了,現(xiàn)在寫(xiě)出來(lái)和大家交流,當(dāng)時(shí)做這個(gè)的時(shí)候找了好多的文章沒(méi)有一篇寫(xiě)的深刻的,大多都是copy的。
?
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRDataSource;
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.data.JRMapCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;
/**
?*
?* <p>C/S架構(gòu) JasperReport 的簡(jiǎn)單使用<p>
?*
?* 創(chuàng)建日期 2013-4-22<br>
?* @author? longgangbai<br>
?* @version $Revision$ 2013-4-22
?* @since?? 3.0.0
?*/
public class Main {
??? public static JRDataSource createReportDataSource() {
??????? JRDataSource dataSource;
??????? Collection<Map<String, ?>> reportRows = initializeMapArray();
??????? dataSource = new JRMapCollectionDataSource(reportRows);
??????? return dataSource;
??? }
??? public static Collection<Map<String,?>> initializeMapArray() {
??????? Collection<Map<String, ?>> reportRows = new ArrayList<Map<String,?>>();
??????? HashMap<String, String> row1Map = new HashMap<String, String>();
??????? HashMap<String, String> row2Map = new HashMap<String, String>();
??????? HashMap<String, String> row3Map = new HashMap<String, String>();
??????? HashMap<String, String> row4Map = new HashMap<String, String>();
??????? row1Map.put("tail_num", "N263Y");
??????? row1Map.put("aircraft_serial", "Chinese,我是中國(guó)人");
??????? row1Map.put("aircraft_model", "39 ROSCOE TRNR RACER");
??????? row1Map.put("engine_model", "R1830 SERIES");
??????? row2Map.put("tail_num", "N4087X");
??????? row2Map.put("aircraft_serial", "BA100-163");
??????? row2Map.put("aircraft_model", "BRADLEY AEROBAT");
??????? row2Map.put("engine_model", "R2800 SERIES");
??????? row3Map.put("tail_num", "N43JE");
??????? row3Map.put("aircraft_serial", "HAYABUSA 1");
??????? row3Map.put("aircraft_model", "NAKAJIMA KI-43 IIIA");
??????? row3Map.put("engine_model", "R1830 SERIES");
??????? row4Map.put("tail_num", "N912S");
??????? row4Map.put("aircraft_serial", "9973CC");
??????? row4Map.put("aircraft_model", "PA18-150");
??????? row4Map.put("engine_model", "R-1820 SER");
??????? reportRows.add(row1Map);
??????? reportRows.add(row2Map);
??????? reportRows.add(row3Map);
??????? reportRows.add( row4Map);
??????? return reportRows;
??? }
????? public static void main(String[] args) throws JRException {
????????? InputStream? inputStream=Main.class.getResourceAsStream("/resources/data/AircraftReport.jrxml");
????????? System.out.println(inputStream);
????????? JRDataSource? jrdatasource =createReportDataSource();
????????? //填充報(bào)表數(shù)據(jù)生成JasperPrint對(duì)象
????????? JasperReport jrt = JasperCompileManager.compileReport(inputStream);//編譯報(bào)表格式
????????? //填充報(bào)表數(shù)據(jù)生成JasperPrint對(duì)象
????????? JasperPrint jasperPrint = JasperFillManager.fillReport(jrt, new HashMap<String, Object>(), jrdatasource);
????????? JasperViewer.viewReport(jasperPrint,false);
????????? JasperViewer view = new JasperViewer(jasperPrint, false);
????????? view.pack();
????????? view.setVisible(true);
??? }
}