jasper 4.0 (小式牛刀 一)

来源:互联网 发布:x战警观看顺序知乎 编辑:程序博客网 时间:2024/06/05 02:59

最近刚好用到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 =
         new File(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">
       
          <result name="success" type="stream">
                <param name="contentType">application/octet-stream;charset=ISO8859-1</param>
                <param name="inputName">targetFile</param>
                <param name="contentDisposition">attachment;filename="${donwloadFileName}"</param>
                <param name="bufferSize">4096</param>
        </result>
       </action>

 

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

自己第一次写技术性的东西,希望能于己于人有点好处

 

原创粉丝点击