天天看點

pentaho Report Designer 入門教程(三)

       采用pentaho report designer5.1版本,也是最新的版本。

一、       安裝和介紹

介紹部分内容略,首先安裝jdk,并配置java相關環境變量,下載下傳pentaho report并解壓,直接運作即可。

二、       第一個示例

三、在swing程式中內建

四、在j2ee程式中內建

Ø  建立web項目

Ø  編寫ant腳本,編譯運作項目

<?xml version="1.0"encoding="utf-8" standalone="no"?>

<projectbasedir="."default="start_tomcat"name="ch2webapp">

    <propertyname="webdir"value="war"/>

    <propertyname="tomcat.home"value="d:/program/tomcat6"/>

    <pathid="classpath">

       <filesetdir="lib">

           <includename="*.jar"/>

       </fileset>

       <filesetfile="${tomcat.home}/lib/servlet-api.jar"/>

    </path>

    <pathid="runtime_classpath">

       <dirsetdir="classes"/>

    <targetname="compile">

       <echomessage="compile"/>

       <mkdirdir="classes"/>

       <javacclasspathref="classpath"destdir="classes"srcdir="src"includeantruntime="on"fork="true"encoding="utf-8"/>

    </target>

    <targetname="war"depends="compile">

       <deletefile="chapter2.war"/>

       <warbasedir="war"destfile="chapter2.war"webxml="war/web-inf/web.xml">

           <classesdir="classes"/>

           <zipfilesetdir="data"prefix="data"/>

           <zipfilesetdir="lib"prefix="web-inf/lib"/>

       </war>

       <deletedir="${tomcat.home}/webapps/chapter2"/>

       <deletefile="${tomcat.home}/webapps/chapter2.war"/>

       <copyfile="chapter2.war"todir="${tomcat.home}/webapps"/>

    <targetname="start_tomcat"depends="war">

       <exectimeout="1000"dir="${tomcat.home}/bin"executable="${tomcat.home}/bin/shutdown.bat"/>

       <sleepseconds="2"/>

       <execdir="${tomcat.home}/bin"executable="${tomcat.home}/bin/startup.bat"/>

</project>

Ø  測試通過servlet 通路不同檔案格式的報表

    publicvoid init(servletconfig config)throws servletexception {

       classicengineboot.getinstance().start();

       super.init(config);

    }

    protectedvoiddoget(httpservletrequest request,

           httpservletresponse response)throws servletexception, ioexception {

       try {

           resourcemanager manager =new resourcemanager();

           manager.registerdefaults();

           string reportpath = "file:"

                  + this.getservletcontext().getrealpath(

                         "data/ch2_1.prpt");

           resource res =manager.createdirectly(newurl(reportpath),

                  masterreport.class);

           masterreport report = (masterreport)res.getresource();

           // determine the output format and renderaccordingly

           string outputformat =request.getparameter("outputformat");

           if ("pdf".equals(outputformat)) {

              // render thepdf

              response.setcontenttype("application/pdf");

              pdfreportutil.createpdf(report,response.getoutputstream());

           } elseif ("xls".equals(outputformat)) {

              // render in excel

              response.setcontenttype("application/vnd.ms-excel");

              excelreportutil.createxls(report,response.getoutputstream());

           } elseif ("rtf".equals(outputformat)) {

              // render inrtf

              response.setcontenttype("application/rtf");

              rtfreportutil.creatertf(report,response.getoutputstream());

           }

       } catch (exception e) {

           e.printstacktrace();

       }

       super.doget(request, response);

Ø  jsp或html檔案調用servlet

<body>

    <h1>example application</h1>

    <p>this is an exampleapplication demonstrating how to embed

       pentaho reporting into yourweb application.</p>

    <a href="report?outputformat=pdf"target="black">generate pdf report </a><br/>

    <a href="report?outputformat=xls"target="black">generate excel report</a><br/>

    <a href="report?outputformat=rtf"target="black">generate rtf report </a><br/>

</body>