jasper 小試牛刀(一)

最近剛好用到jasper 4.0 做關於報表的開發 , 隨及就寫點東西, 請各位大俠不要潑油哈…………

 

jasper 4.0 和以前版本不太相同,據說是重構了一下。 這裏主要說下jasper 4.0 的用法 以及和ireport (圖形化工具) 的聯合使用

 

1) 在ssh項目 中, 很多人都習慣將 jasper 的jar包 全部導入進去,但是這樣有時會出現問題(jar 包衝突,或者是jar包 重複)

 

      jasperreports-4.0.1.jar ,

       jasperreports-applet-4.0.1.jar,

       jasperreports-fonts-4.0.1.jar, 

      jasperreports-javaflow-4.0.1.jar

這4個jar包就是jasper核心的jar包了,但是在導入的時候,(沒特殊情況)前面三個就ok了,不然很容易就出現 多個component  我也是找了很久才找到的,當然jasper 的配套包 像 (groovy,itext,等 )groovy-all-1.7.5.jar,itext-asian.jar,commons 之類的 是必須的。

但是這些包 的版本必須要配對 特別是 json 包的版本,groovy的版本,groovy的已經寫出來了,對於json 當然是最近版本比較好,目前我用jasper4.0  用到的json包是 json-lib-2.3-jdk15.jar ,jsonplugin-0.33.jar(最低的json 2.0 還沒考證過)  。對於和struts 的結合則是用的  struts2-jasperreports-plugin-2.1.6.jar

 

2)ireport 是jasper圖形化的工具,通過它可以畫出很多複雜的報表格式,以及方便的鏈接數據源(就是取得數據) ,這裏具體的用法就不做介紹,先看一個ireport製作的報表樣式:

 

唯一的一點就是:

     如果報表要導出成pdf等文件的話,在 製作 jrxml 文件的時候就要注意到中文格式的問題.

     在有可能出現漢字的地方,要對那個 textfield 進行字體設定,具體的設定就在 “屬性” 裏面 :

          詳細如下: font name 通常就採用宋體就ok ,比較通用,也不會容易出錯 ,(TTF 的也可以)

                              pdf embeded 選項打上勾 ,在 pdf encoding 選項選擇 UniGB-UCS2-H (Chinese Simplified) 

                              在pdf font name is now ...  選項選擇   STSong-Light  。這樣就屏蔽了中文pdf的亂碼問題

 

下面貼個引用小例子:

(這個是生成報表文件並下載的)

File reportFile =
         newFile(ServletActionContext.getServletContext().             
       getRealPath("/report/"+donwloadFileName+".jasper"));
         String fileDir =reportFile.getAbsolutePath().substring(0,reportFile.getAbsolutePath().lastIndexOf(fileSeparator)+1);
        HashMap parameter = new HashMap();
        System.out.println(fileDir);
       
        parameter.put("STRWHERE"," and A.unit_id='23100' and A.stryear='2011' and A.strmonth='04'");
        parameter.put("SUBREPORT_DIR",fileDir);
        try{
        Connection con = getConnection();
    //    JasperReport jasperReport =JasperCompileManager
    //           .compileReport(fileDir+"電費交費報表.jrxml");
        JasperPrint jasperPrint =JasperFillManager.fillReport(reportFile.getPath(),
               parameter, con);
       JasperExportManager.exportReportToPdfFile(jasperPrint,
           fileDir+donwloadFileName+".pdf");
        }catch(Exception e){
            e.printStackTrace();
            return"false";
        }
       
        //   正式的文件下載
        System.out.println("get in");
        File reportFiles =null;
        try{

          // 這裏是得到文件的具體路徑
            filePath =ServletActionContext.getServletContext().             
             getRealPath("/report/"+donwloadFileName+".pdf");
             reportFiles =new File(filePath);

         // 下載的文件名
             donwloadFileName= donwloadFileName+".pdf";
        }catch(Exception e){
            success = false;
            e.printStackTrace();
            return"false";
        }

 

 

struts 配置文件:

    <!-- 報表的導出-->
        <action name="downLoad"class="downLoadJasperAction" method="downLoad">
       
          <resultname="success" type="stream">
               <param name="contentType">application/octet-stream;charset=ISO8859-1</param>
               <param name="inputName">targetFile</param>
               <paramname="contentDisposition">attachment;filename="${donwloadFileName}"</param>
               <param name="bufferSize">4096</param>
        </result>
       </action>

 

----------------------------------------------------------------------------------------------------------------------------------------------------

自己第一次寫技術性的東西,希望能於己於人有點好處

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章