vjame

          優(yōu)化代碼是無止境的
          隨筆 - 65, 文章 - 9, 評論 - 26, 引用 - 0
          數(shù)據(jù)加載中……

          JasperReports學(xué)習(xí)筆記2-創(chuàng)建簡單的報表例子


          一、創(chuàng)建簡單的jrxml文件

          這里可以手動創(chuàng)建jrxml文件,也可以使用ireport設(shè)計器創(chuàng)建
           1<?xml version="1.0"?>
           2<!DOCTYPE jasperReport
           3PUBLIC "-//JasperReports//DTD Report Design//EN"
           4"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
           5<jasperReport name="FirstReport">
           6    <detail>
           7        <band height="20">
           8            <staticText>
           9                <reportElement x="20" y="0" width="200" height="20" />
          10                <text><![CDATA[If you see this, it work]]></text>
          11            </staticText>
          12        </band>
          13    </detail>
          14</jasperReport>

          以上是一個簡單的JRXML文件,實際上就是一個標(biāo)準(zhǔn)的XML文件,<jasperReport>是根元素,<staticText>表示靜態(tài)的數(shù)據(jù),<reportElement>定義了<staticText>的位置和寬度,<text>定義了實際的靜態(tài)數(shù)據(jù)。

          二、把JRXML文件解析成jasper文件,文件流或JasperReport對象

          編譯jrxml生成jasper文件
          JasperCompileManager.compileReportToFile("WebRoot/report/jasperreports_demo.jrxml""WebRoot/report/lanjh.jasper");

          生成文件流
          public static void compileReportToStream(InputStream inputStream,OutputStream outputStream)

          生成報表對象
          public static JasperReport compileReport("WebRoot/report/jasperreports_demo.jrxml")


          三、有了JasperReport對象,文件流或jasper文件之后,就可以用JasperFillManager生成JasperPrint對象,jrprint文件,jrprint文件流

          生成JasperPrint 對象
          public static JasperPrint fillReport(JasperReport jasperReport,Map parameters, JRDataSource dataSource)


          四、使用JasperExportManager導(dǎo)出或顯示
          JasperExportManager.exportReportToHtmlFile(jasperPrint, "c://1.html");
          JasperExportManager.exportReportToPdfFile(jasperPrint, "c://1.pdf");


          完整的例子

           1 JasperReport jasperReport;
           2        JasperPrint jasperPrint;
           3        try{
           4          jasperReport = JasperCompileManager.compileReport("WebRoot/report/jasperreports_demo.jrxml");
           5          jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(),new  JREmptyDataSource());
           6          JasperExportManager.exportReportToPdfFile(jasperPrint, "c://1.pdf");
           7   }

           8        catch (JRException e){
           9          e.printStackTrace();
          10        }
                  





          posted on 2013-10-15 14:55 lanjh 閱讀(494) 評論(0)  編輯  收藏 所屬分類: 報表

          主站蜘蛛池模板: 玉田县| 阆中市| 志丹县| 外汇| 花莲市| 汽车| 武陟县| 锦屏县| 南京市| 雷州市| 民丰县| 扬中市| 绥化市| 高淳县| 济宁市| 章丘市| 新余市| 九台市| 邮箱| 巴塘县| 泾川县| 西和县| 长垣县| 额济纳旗| 敦煌市| 鄢陵县| 夏河县| 郓城县| 青河县| 连州市| 周宁县| 东乡族自治县| 乃东县| 清徐县| 西和县| 佛学| 富裕县| 全南县| 肥城市| 遂溪县| 井研县|