JUST DO IT ~

我只想当个程序员

jasper ireport

读取一个模板然后处理



package report;


import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import net.sf.jasperreports.engine.JasperFillManager;  

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import oracle.jdbc.driver.OracleTypes;


public class Main {
        
    
private String driverName = "oracle.jdbc.driver.OracleDriver";   
    
private String url = "jdbc:oracle:thin:@127.0.0.1:1521:itmp";   
    
private String userName = "itmp";   
    
private String userPwd = "itmp";   
    
    
public String selectSQL(String selectSQL) {   
        Connection conn 
= null;   
        Statement st 
= null;   
        ResultSet rs 
= null;   
        
// 用来存放查询的结果   
        StringBuffer sb = new StringBuffer();   
        
try {   
            
// 加载驱动   
            Class.forName(driverName);   
            
// 创建连接   
            conn = DriverManager.getConnection(url, userName, userPwd);   
            
// 创建Statement   
            st = conn.createStatement();   
            
// 执行sql语句,得到查询结果   
            rs = st.executeQuery(selectSQL);   
                                    
            
            
/*
            // 输出查询结果   
            int cloNum = rs.getMetaData().getColumnCount();   
            sb.append("<tr class=\"th\">");   
            for (int i = 0; i < cloNum; i++) {   
                sb.append("<td>");   
                sb.append(rs.getMetaData().getColumnName(i + 1));   
                sb.append("</td>");   
            }   
            sb.append("</tr>");   
            while (rs.next()) {   
                sb.append("<tr>");   
                for (int i = 0; i < cloNum; i++) {   
                    sb.append("<td>");   
                    sb.append(rs.getString(i + 1));   
                    sb.append("</td>");   
                }   
                sb.append("</tr>");   
            }   
            // 关闭资源  
       
             
*/ 
            rs.close();   
            st.close();   
            conn.close();   
  
        } 
catch (Exception ex) {   
            
// 输出错误信息   
            ex.printStackTrace();   
        } 
finally {   
            
// finally子句总是会执行(就算发生错误),这样可以保证资源的绝对关闭   
            try {   
                
if (rs != null)   
                    rs.close();   
            } 
catch (SQLException e) {   
                e.printStackTrace();   
            }   
            
try {   
                
if (st != null)   
                    st.close();   
            } 
catch (SQLException e) {   
                e.printStackTrace();   
            }   
            
try {   
                
if (conn != null)   
                    conn.close();   
            } 
catch (SQLException e) {   
                e.printStackTrace();   
            }   
  
        }   
        
return sb.toString();   
    }   

    
    
public void report(){
          Connection conn 
= null;   
          CallableStatement stmt 
= null;   
          ResultSet rs 
= null;   
          
// 用来存放查询的结果   
          StringBuffer sb = new StringBuffer();   
         String  exchgcode 
="SH";
        String stkcode 
= "600000";
          
          
try {   
              
              
              
          
              
// 加载驱动   
              Class.forName(driverName);   
              
// 创建连接   
              conn = DriverManager.getConnection(url, userName, userPwd);   
              
// 创建Statement   
              
            String sql 
=  " call sp_gethq2 (? ,? ,? )";
           
            stmt 
= conn.prepareCall(sql);

              stmt.setString(
1, exchgcode);
               stmt.setString(
2, stkcode );
 
               
              stmt.registerOutParameter(
3, OracleTypes.CURSOR);

              
//stmt.registerOutParameter(3, OracleTypes.VARCHAR);
              
//stmt.registerOutParameter(4, OracleTypes.VARCHAR);

              stmt.execute();
              
/*
              String errorcode = "";
              errorcode = (String) stmt.getObject(3);
              String errormsg = "";
              errormsg = (String) stmt.getObject(4);
            
*/
                rs 
= (ResultSet) stmt.getObject(3);
                
                
                JRResultSetDataSource result 
=   
                   
new JRResultSetDataSource( rs );  
//              create JasperReport from .jasper  
                
              JasperReport  jasperReport 
= JasperCompileManager.compileReport(
              
"c:/report1.jrxml");

                            
/*
                JasperReport jasperReport =   
                   (JasperReport) JRLoader.loadObject("reports/Location.jasper"); 
                
*/
                
                
                JasperPrint jasperPrint 
=   
                   JasperFillManager.fillReport( jasperReport, 
new HashMap(), result );  
                
              JasperExportManager.exportReportToHtmlFile( jasperPrint, 
"c:/simple_report1.html");
                
                

          } 
catch (Exception ex) {   
              
// 输出错误信息   
              ex.printStackTrace();   
          } 
finally {   
              
// finally子句总是会执行(就算发生错误),这样可以保证资源的绝对关闭   
              try {   
                  
if (rs != null)   
                      rs.close();   
              } 
catch (SQLException e) {   
                  e.printStackTrace();   
              }   
              
try {   
                  
if (stmt != null)   
                      stmt.close();   
              } 
catch (SQLException e) {   
                  e.printStackTrace();   
              }   
              
try {   
                  
if (conn != null)   
                      conn.close();   
              } 
catch (SQLException e) {   
                  e.printStackTrace();   
              }   
    
          }   
      
        
    }
    
    
public void report(){
        Connection conn 
= null;   
        CallableStatement stmt 
= null;   
        ResultSet rs 
= null;   
        
// 用来存放查询的结果   
        StringBuffer sb = new StringBuffer();   
       String  exchgcode 
="SH";
      String stkcode 
= "600000";
        
        
try {   
            
            
            
        
            
// 加载驱动   
            Class.forName(driverName);   
            
// 创建连接   
            conn = DriverManager.getConnection(url, userName, userPwd);   
            
// 创建Statement   
            
          String sql 
=  " call sp_gethq2 (? ,? ,? )";
         
          stmt 
= conn.prepareCall(sql);

            stmt.setString(
1, exchgcode);
             stmt.setString(
2, stkcode );

             
            stmt.registerOutParameter(
3, OracleTypes.CURSOR);

            
//stmt.registerOutParameter(3, OracleTypes.VARCHAR);
            
//stmt.registerOutParameter(4, OracleTypes.VARCHAR);

            stmt.execute();
            
/*
            String errorcode = "";
            errorcode = (String) stmt.getObject(3);
            String errormsg = "";
            errormsg = (String) stmt.getObject(4);
          
*/
              rs 
= (ResultSet) stmt.getObject(3);
              
              
              JRResultSetDataSource result 
=   
                 
new JRResultSetDataSource( rs );  
//            create JasperReport from .jasper  
              
            JasperReport  jasperReport 
= JasperCompileManager.compileReport(
              
"c:/report1.jrxml");

                          
/*
              JasperReport jasperReport =   
                 (JasperReport) JRLoader.loadObject("reports/Location.jasper"); 
              
*/
              
              
              JasperPrint jasperPrint 
=   
                 JasperFillManager.fillReport( jasperReport, 
new HashMap(), result );  
              
            JasperExportManager.exportReportToHtmlFile( jasperPrint, 
"c:/simple_report1.html");
              
              

        } 
catch (Exception ex) {   
            
// 输出错误信息   
            ex.printStackTrace();   
        } 
finally {   
            
// finally子句总是会执行(就算发生错误),这样可以保证资源的绝对关闭   
            try {   
                
if (rs != null)   
                    rs.close();   
            } 
catch (SQLException e) {   
                e.printStackTrace();   
            }   
            
try {   
                
if (stmt != null)   
                    stmt.close();   
            } 
catch (SQLException e) {   
                e.printStackTrace();   
            }   
            
try {   
                
if (conn != null)   
                    conn.close();   
            } 
catch (SQLException e) {   
                e.printStackTrace();   
            }   
  
        }   
    
      
  }
  
    
public void html() throws JRException
    {
        
long start = System.currentTimeMillis();
        JasperExportManager.exportReportToHtmlFile(
"build/reports/TableReport.jrprint");
        System.err.println(
"HTML creation time : " + (System.currentTimeMillis() - start));
    }
    
    
    
    
public static void main(String []args ){
        
        
new Main().report() ;
        
        
    }
    
}









<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report1" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    
<property name="ireport.zoom" value="1.0"/>
    
<property name="ireport.x" value="0"/>
    
<property name="ireport.y" value="0"/>
    
<style name="Title" forecolor="#FFFFFF" fontName="Times New Roman" fontSize="50" isBold="false" pdfFontName="Times-Bold"/>
    
<style name="SubTitle" forecolor="#CCCCCC" fontName="Times New Roman" fontSize="18" isBold="false" pdfFontName="Times-Roman"/>
    
<style name="Column header" forecolor="#666666" fontName="Times New Roman" fontSize="14" isBold="true" pdfFontName="Times-Roman"/>
    
<style name="Detail" mode="Transparent" fontName="Times New Roman" pdfFontName="Times-Roman"/>
    
<style name="Row" mode="Transparent" fontName="Times New Roman" pdfFontName="Times-Roman">
        
<conditionalStyle>
            
<conditionExpression><![CDATA[$V{REPORT_COUNT}%2 == 0]]></conditionExpression>
            
<style mode="Opaque" backcolor="#F0EFEF"/>
        
</conditionalStyle>
    
</style>
    
<field name="EXCHGCODE_" class="java.lang.String"/>
    
<field name="STKCODE_" class="java.lang.String"/>
    
<field name="ZJCJ_" class="java.math.BigDecimal"/>
    
<background>
        
<band splitType="Stretch"/>
    
</background>
    
<title>
        
<band height="102" splitType="Stretch">
            
<frame>
                
<reportElement mode="Opaque" x="0" y="70" width="721" height="32" forecolor="#000000" backcolor="#CC0000"/>
                
<textField pattern="EEEEE dd MMMMM yyyy">
                    
<reportElement x="458" y="12" width="239" height="20" forecolor="#FFFFFF"/>
                    
<textElement textAlignment="Right">
                        
<font size="12"/>
                    
</textElement>
                    
<textFieldExpression class="java.util.Date"><![CDATA[new java.util.Date()]]></textFieldExpression>
                
</textField>
            
</frame>
        
</band>
    
</title>
    
<pageHeader>
        
<band splitType="Stretch"/>
    
</pageHeader>
    
<columnHeader>
        
<band height="18" splitType="Stretch">
            
<staticText>
                
<reportElement style="Column header" x="0" y="0" width="36" height="18" forecolor="#000000"/>
                
<textElement>
                    
<font isBold="false"/>
                
</textElement>
                
<text><![CDATA[EXCHGCODE_]]></text>
            
</staticText>
            
<staticText>
                
<reportElement style="Column header" x="53" y="0" width="34" height="18" forecolor="#000000"/>
                
<textElement>
                    
<font isBold="false"/>
                
</textElement>
                
<text><![CDATA[STKCODE_]]></text>
            
</staticText>
            
<staticText>
                
<reportElement style="Column header" x="113" y="0" width="34" height="18" forecolor="#000000"/>
                
<textElement>
                    
<font isBold="false"/>
                
</textElement>
                
<text><![CDATA[ZJCJ_]]></text>
            
</staticText>
        
</band>
    
</columnHeader>
    
<detail>
        
<band height="46" splitType="Stretch">
            
<frame>
                
<reportElement style="Row" mode="Opaque" x="5" y="9" width="797" height="37"/>
                
<box>
                    
<topPen lineWidth="1.0" lineStyle="Solid"/>
                    
<leftPen lineWidth="1.0" lineStyle="Solid"/>
                    
<bottomPen lineWidth="1.0" lineStyle="Solid"/>
                    
<rightPen lineWidth="1.0" lineStyle="Solid"/>
                
</box>
                
<textField isStretchWithOverflow="true">
                    
<reportElement style="Detail" positionType="Float" x="0" y="0" width="34" height="18"/>
                    
<textElement>
                        
<font size="14"/>
                    
</textElement>
                    
<textFieldExpression class="java.lang.String"><![CDATA[$F{EXCHGCODE_}]]></textFieldExpression>
                
</textField>
                
<textField isStretchWithOverflow="true">
                    
<reportElement style="Detail" positionType="Float" x="46" y="0" width="34" height="18"/>
                    
<textElement>
                        
<font size="14"/>
                    
</textElement>
                    
<textFieldExpression class="java.lang.String"><![CDATA[$F{STKCODE_}]]></textFieldExpression>
                
</textField>
                
<textField isStretchWithOverflow="true">
                    
<reportElement style="Detail" positionType="Float" x="106" y="0" width="34" height="18"/>
                    
<textElement>
                        
<font size="14"/>
                    
</textElement>
                    
<textFieldExpression class="java.math.BigDecimal"><![CDATA[$F{ZJCJ_}]]></textFieldExpression>
                
</textField>
            
</frame>
        
</band>
    
</detail>
    
<columnFooter>
        
<band height="22" splitType="Stretch">
            
<line>
                
<reportElement positionType="FixRelativeToBottom" x="0" y="3" width="555" height="1"/>
                
<graphicElement>
                    
<pen lineWidth="0.5" lineColor="#999999"/>
                
</graphicElement>
            
</line>
        
</band>
    
</columnFooter>
    
<pageFooter>
        
<band height="25" splitType="Stretch"/>
    
</pageFooter>
    
<summary>
        
<band splitType="Stretch"/>
    
</summary>
</jasperReport>

追求完美直接在 里面处理












<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report23" language="groovy" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    
<property name="ireport.zoom" value="1.0"/>
    
<property name="ireport.x" value="0"/>
    
<property name="ireport.y" value="0"/>
    
<style name="Title" fontName="Arial" fontSize="26" isBold="true" pdfFontName="Helvetica-Bold"/>
    
<style name="SubTitle" forecolor="#666666" fontName="Arial" fontSize="18"/>
    
<style name="Column header" forecolor="#666666" fontName="Arial" fontSize="12" isBold="true"/>
    
<style name="Detail" fontName="Arial" fontSize="12"/>
    
<parameter name="a" class="java.lang.String"/>
    
<parameter name="b" class="java.lang.String"/>
    
<parameter name="cursor" class="java.sql.ResultSet" isForPrompting="false"/>
    
<queryString language="plsql">
        
<![CDATA[{call  sp_gethq2( $P{a} ,$P{b}  ,$P{cursor}  )}]]>
    
</queryString>
    
<field name="EXCHGCODE_" class="java.lang.String"/>
    
<field name="STKCODE_" class="java.lang.String"/>
    
<field name="ZJCJ_" class="java.math.BigDecimal"/>
    
<background>
        
<band splitType="Stretch"/>
    
</background>
    
<title>
        
<band height="70" splitType="Stretch">
            
<image>
                
<reportElement x="275" y="0" width="300" height="64"/>
                
<imageExpression class="java.lang.String"><![CDATA["leaf_banner_green.png"]]></imageExpression>
            
</image>
            
<staticText>
                
<reportElement style="Title" x="0" y="13" width="263" height="33"/>
                
<textElement verticalAlignment="Middle"/>
                
<text><![CDATA[Leaf Green Title]]></text>
            
</staticText>
            
<staticText>
                
<reportElement style="SubTitle" x="100" y="46" width="174" height="22"/>
                
<textElement/>
                
<text><![CDATA[Leaf Green SubTitle]]></text>
            
</staticText>
        
</band>





posted on 2010-09-06 10:02 小高 阅读(486) 评论(0)  编辑  收藏 所属分类: J2EE

导航

<2010年9月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

统计

常用链接

留言簿(3)

随笔分类(352)

收藏夹(19)

关注的blog

手册

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜