iReport+jasperReport之JDBC數據源
iReport畫出來的報表可以滿足我們大部分的需要,所以采用iReport 編輯jrxml文件是我們的首選啦!當然掌握jrxml文件的結構也是必須的。
????????采用JDBC數據源必須有數據庫支持啊!我們新建一個test表,其中有pid、name、sex、password、department、age字段,為了方便期間我們定義這些字段全部為String類型。
????????在iReport中新建一報表,報表有title、pageHeader、columnHeader、detail、columnFooter、pageFooter、lastPageFooter、summary等部分,被成為Report section
title:顧名思義是指報表的標題哦,它會顯示在報表的最上面,如果有多頁只會出現在第一頁的最上面。pageHeader:每頁的標題,如果有多頁每頁的開始都會顯示此部分內容。
columnHeader:顯示報表的列頭不經常使用。
detail:這個就不用說了吧!
有header就會出現footer啦!lastPageFooter只會出現在最后一頁。summay會出現在每一頁數據上,主要是為了展示一些統計信息,比如當前的時間,頁數信息啦!
新建一parameter
此參數可作為報表的標題使用,我們在程序中動態的傳遞給報表。
????document structure---》parameter里找到reportTitle參數拖至title區域,右擊編輯reportTitle域的屬性,在font選項里找到Markup設置為HTML,TextField選項里設置Text Field Expreesion為
"
<
font?
color
='blue'?
size
='5'>"+$P{reportTitle}+"</font>"
下來就是設置報表的Field字段了,不需要我們一個個的新建那些字段啦!
選擇Data--->Report Query在Report Query選項里選擇Query Language為SQL,寫入SqL語句
select
?
*
?
from
?test?
order
?
by
?pid?
DESC
這時所有的field會出現在下面(SQL是正確的)
關閉對話框在document structure---》field中就會出現我們需要的field,之后拖到相應的位置,關于怎么美化報表這個本人也不是很懂哦!
如果需要顯示一下當前的頁數信息也可以自己托動Variables里的PAGE_NUMBER完成頁數的顯示。完整的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[
頁
]]>
</
text
>
????????????????
</
staticText
>
????????????
</
band
>
????????
</
pageFooter
>
????????
<
summary
>
????????????
<
band?
height
="0"
??isSplitAllowed
="true"
?
>
????????????
</
band
>
????????
</
summary
>
</
jasperReport
>
????我們可以直接使用ireport查看預覽效果,但是大部分我們是在應用程序中使用的哦!我們看看我們怎么讓這個jrxml模板文件工作呢,???
File?file?
=
?
new
?File(
"
Person.jrxml
"
);
????????????InputStream?in?
=
?
new
?FileInputStream(file);?
//
?編譯報表?jasperReport?=
????????????jasperReport?
=
?JasperCompileManager.compileReport(in);
編譯文件是很耗時的工作,通常這個由iReport去做我們使用jasper文件即可,
jasperReport?
=
?(JasperReport)?JRLoader
????????????????????.loadObject(
"
D:\\workspace\\Person.jasper
"
);
產生了JasperReport對象下來就是要填充數據了,采用JDBC方式我們需要一個打開的connection(數據庫連接),
還有報表需要的parameter:
public
?HashMap?getMap()?
{
????????HashMap?map?
=
?
new
?HashMap();
????????map.put(
"
reportTitle
"
,?
"
laoshulin
"
);
????????
return
?map;
????}
?
jasperPrint?
=
?JasperFillManager
????????????????????.fillReport(jasperReport,?getMap,?getConn());
針對jasperPrint對象JasperReport有很多的API可以提供各種方式的預覽或是生成文件
我只說說JasperViewer吧!其它的看看api或是google一下就知道了
JasperViewer?jasperViewer?
=
?
new
?JasperViewer(jasperPrint);
JasperViewer 繼承自JFrame類,
jasperViewer.setVisible(
true
);
這樣就可以預覽報表了,
JRViewer這個類繼承Jpanel,我們可以在web中使用它,后面介紹客戶端打印時再詳細介紹。
????JDBC數據源很簡單主要是SQL的功底,但是它是最基礎的,網上一大堆這方面的介紹哦,可以參考別人的多看看哦,我寫的這些都有點語無倫次了,主要是我自己不怎么寫東西,多以代碼的形式留給自己了,現在寫出來和大家交流,當時做這個的時候找了好多的文章沒有一篇寫的深刻的,大多都是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架構 JasperReport 的簡單使用<p>
?*
?* 創建日期 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,我是中國人");
??????? 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();
????????? //填充報表數據生成JasperPrint對象
????????? JasperReport jrt = JasperCompileManager.compileReport(inputStream);//編譯報表格式
????????? //填充報表數據生成JasperPrint對象
????????? 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);
??? }
}