JasperReport的使用过程.docx
《JasperReport的使用过程.docx》由会员分享,可在线阅读,更多相关《JasperReport的使用过程.docx(9页珍藏版)》请在冰豆网上搜索。
JasperReport的使用过程
JasperReport(http:
//jasperforge.org/)
MinimumRequirementsandrequiredjarfiletogetstartedjasperreport:
●SunJDK1.4orlaterversion
●commons-beanutils-{version}.jar
●commons-collections-{version}.jar
●commons-digester-{version}.jar;
●commons-logging-{version}.jar;
●commons-logging-api-{version}.jar;
●itext-{version}.jar(pdfprinting)
●jasperreports-{version}.jar
●jdt-compiler-{version}.jar
●jxl-{version}.jar(excelprinting)
●png-encoder-{version}.jar
●poi-2.0-final-20040126.jar(excelprinting)
●jfreechart-1.0.12.jar(chatprinting)
●barbecue-1.1.jar(barcodeprinting)
iReportGettingStarted
Step1:
Download,Install,andRuniReportDownloadinhttp:
//jasperforge.org/
ToinstalliReportonaWindowsplatform:
runiReport--windows-installer.exe.
StartiReportusingthemenuitem
Programsmenu>Jaspersoft>iReport
IfyouareusingaMacoraLinuxbox,considertodownloadiReport-.tar.gz,extractthearchiveinyourfavoritedirectoryandrun:
iReport.sh.fromashell.
Step2:
Connecttoadatabase
iReportcanuseseveraldatasourcestocreatereports,suchasJDBCconnection,XMLfile,CSVfile,JavaBeanresultset,Hibernateconnection,etc...ThisdocumentwillshowhowtocreateJDBCconnectionDataSourceandXMLfileDataSource.
AddOracleJDBCDrivertoiReport
iReportrequiredtoaddthejavajarfileoftheJDBCDriver.IfyouareconnectingtheOracledatabaseasthedatasourceofthereport,youhavetoaddthejarfileoforaclejdbcdrivertoiReport.
AddingOracleclassfileforconnectiontoOracledatabase:
Tools->Options->Classpath->AddJAR
AddDataSource
ClickReportDatasourceicon
->New
OracleJDBCconnection
1.ClickDatabaseJDBCconnection.
2.fillintheconnectioninformation
XMLfileDataSource
1.ClickXMLfiledatasource
2.fillintheconnectioninformation
Step3:
Createyourfirstreport
Nowyouhavesetupadatabaseconnection,startthereportwizard(File->ReportWizard)toquicklycreateyourfirstreport.
1.Createanemptyreport“QuickQuote”
Thelayoutofreportcanbesplitinto:
TitleBand,PageHeaderBand,ColumnHeaderBand,DetailBand,ColumnFooterBand,PageFooterBand,SummaryBand
Inthisreport,wewilluseTitleBand,ColumnHeaderBandandDetailBandonly.
WecansettheBandHeightto0forunusedband
Definethequeryforthedata:
RightclickthemouseinblankareaofiReport.
Typethesqlstatementforthedata:
e.g.
SELECTID,QUICK_QUOTE_NOFROMmer_quick_quote
whereID='284e33e20a2bb9cb9ce510d0fe100897'
Addthefollowingobject,andpreviewthereport.
AddSubReport:
Hideunusedlayoutband:
Deletethe$F{PICTURE},andaddanImageobjectintothelayout.
Setthevalueoftheimageas:
"C:
\\filesystem\\uploadFiles\\QuickQuote\\thumbnail\\20090408\\"+$F{PICTURE}+".png"
PreviewtheSubReport:
Previewthewholereport:
UsingParametersinaQueryandSubReportQuery
ParametersmaybeusedtofilteraSQLquery.Supposeyouwanttoprintinformationabouta
particularcustomeridentifiedbycustomerID(notknownatdesigntime).Yourquerywillbeas
follows:
select*fromcustomers
whereCUSTOMERID=$P{MyCustomerId}
MyCustomerIdisaparameterdeclaredasaninteger.JasperReportswilltransformthisqueryto
select*fromcustomerswhereCUSTOMERID=?
ItwillrunthisSQLusingapreparedstatementbypassingtheMyCustomerIdvalueasa
queryparameter.Ifyouwouldliketopasstheparametervaluedirectly,youcanusethisspecial
syntax:
$P!
{parameter_name}
Thisallowsyoutoreplacetheparameternameinthequerywithitsvalue.Forexample,ifyouhave
aparameternamedMyWherewiththevalueofwhereCUSTOMERID=5,thequery
select*fromcustomers$P!
{MyWhere}
willbetransformedinto
select*fromcustomerswhereCUSTOMERID=5
byusingthissyntax.
Example:
SELECTID,QUICK_QUOTE_NOFROMmer_quick_quote
whereID=$P{docId}
PrintingbarcodeiniReport
1.Addanimagetothereport
2.SettheImageExpressionas:
e.g.
mon.image.BarcodeImage.getBarcodeImage(13,"0815",true,false,null,0,0)
3.SettheImageExpressionClass:
java.awt.Image
SubReportwithXMLdatasource:
ConnectionType
Useadatasourceexpression
DataSourceExpression
((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/DOC/LIN")
JasperPrintinginMerchant
Configuredinstruts-printing.xml
XMLsourcetoPDF->xml_print_pdf.action
XMLsourcetoExcel->xml_print_excel.action
XMLsourcetoHTML->xml_print_html.action
Exampletoruntheprintfeatureinoursystem:
http:
//localhost:
8083/merchantWeb/xml_print_pdf.action?
reportName=PacSunPO&docId=0000201569.xml
:
Parameter
Value
Parameter
Value
reportName
JasperclassfileName
docId
Xmlfilename
DBtoPDF->jasper_print_pdf.action
DBtoExcel->jasper_print_excel.action
DBtoHTML->jasper_print_html.action
Exampletoruntheprintfeatureinoursystem:
http:
//localhost:
8083/merchantWeb/jasper_print_pdf.action?
reportName=QuickQuote&docId=284e33e20a2bb9cb9ce510d0fe100897
http:
//localhost:
8083/merchantWeb/jasper_print_html.action?
reportName=CHART
UsefullinkforjasperreportandiReport.
iReportTutorials
http:
//ireport-
CreatingandDesigningReportUsingiReport(usingoldversionasexample)
JasperReport报表设计总结
JasperReport经验谈