René Nyffenegger's collection of things on the web | |
René Nyffenegger on Oracle - Most wanted - Feedback
- Follow @renenyffenegger
|
Jasper - Using reports from/with Java | ||
First example
A Java class that compiles an .jrxml and creates a PDF out of it.
<?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 ="To be called by Java" pageWidth = "595" pageHeight = "845" columnWidth = "595" leftMargin = "0" rightMargin = "0" topMargin = "0" bottomMargin = "0" > <queryString> <![CDATA[ select owner, object_type, object_name from all_objects where owner not in ('SYS', 'SYSTEM') order by owner, object_type, object_name ]]> </queryString> <field name="OWNER" class="java.lang.String"/> <field name="OBJECT_TYPE" class="java.lang.String"/> <field name="OBJECT_NAME" class="java.lang.String"/> <detail> <band height="16" splitType="Stretch"> <textField> <reportElement x= "0" y="0" width="130" height="14"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[$F{OWNER} ]]> </textFieldExpression> </textField> <textField> <reportElement x="130" y="0" width="130" height="14"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[$F{OBJECT_TYPE}]]> </textFieldExpression> </textField> <textField> <reportElement x="260" y="0" width="130" height="14"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[$F{OBJECT_NAME}]]> </textFieldExpression> </textField> </band> </detail> </jasperReport> // set path=C:\program files\Java\jdk\1.6.0_10\bin;%PATH% // set classpathdir=z:\Development\locale\ // // Compilation: // ------------ // javac -Xlint:deprecation -classpath %classpathdir%jasperreports-3.5.2.jar Ex_11_00.java // // Run // --- // Needed jars: // o jasperreports.xyz.jar // o commons-digester-x-y.jar // o commons-logging-x-y.jar // o commons-collections-x.y.z.jar // o commons-beanutils-x.y.z.jar // o jdbc5.jar (thin jdbc driver for Oracle?) // o iText-x.y.z.jar (PDF creation?) // // set classpath=%classpathdir%jasperreports-3.5.2.jar;%classpathdir%commons-digester-1.7.jar;%classpathdir%commons-logging-1.1.jar;%classpathdir%commons-collections-3.2.1.jar;commons-beanutils-1.8.0.jar;%classpathdir%ojdbc5.jar;%classpathdir%iText-2.1.0.jar;. // // java -classpath %classpath% Ex_11_00 import java.sql.*; import java.util.HashMap; import java.util.Map; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.design.JasperDesign; import net.sf.jasperreports.engine.JasperManager; public class Ex_11_00 { public static void main(String [] args) { try { JasperDesign jasperDesign = JasperManager.loadXmlDesign("Ex_11_00.jrxml"); JasperReport jasperReport = JasperManager.compileReport(jasperDesign); Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@172.17.5.24:1521:newora11", "rene", // username "rene" // password ); Map parameters = new HashMap(); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn); JasperExportManager.exportReportToPdfFile(jasperPrint,"Ex_11_00.pdf"); } catch (Exception e) { System.out.println(e); } } } Second example
Same thin except additional passing of parameters.
<?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 ="To be called by Java" pageWidth = "595" pageHeight = "845" columnWidth = "595" leftMargin = "0" rightMargin = "0" topMargin = "0" bottomMargin = "0" > <parameter name="P_OWNER" class="java.lang.String" /> <parameter name="P_OBJECT_TYPE" class="java.lang.String" /> <queryString> <![CDATA[ select object_name, created from all_objects where owner = $P{P_OWNER} and object_type = $P{P_OBJECT_TYPE} order by owner, object_type, object_name ]]> </queryString> <field name="OBJECT_NAME" class="java.lang.String"/> <field name="CREATED" class="java.sql.Timestamp" /> <detail> <band height="16" splitType="Stretch"> <textField> <reportElement x= "0" y="0" width="130" height="14"/> <textElement/> <textFieldExpression class="java.lang.String" ><![CDATA[$F{OBJECT_NAME}]]> </textFieldExpression> </textField> <textField> <reportElement x="130" y="0" width="130" height="14"/> <textElement/> <textFieldExpression class="java.sql.Timestamp"><![CDATA[$F{CREATED} ]]> </textFieldExpression> </textField> </band> </detail> </jasperReport> import java.sql.*; import java.util.HashMap; import java.util.Map; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.design.JasperDesign; import net.sf.jasperreports.engine.JasperManager; public class Ex_11_01 { public static void main(String [] args) { try { JasperDesign jasperDesign = JasperManager.loadXmlDesign("Ex_11_01.jrxml"); JasperReport jasperReport = JasperManager.compileReport(jasperDesign); Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@172.17.5.24:1521:newora11", "rene", // username "rene" // password ); Map parameters = new HashMap(); parameters.put("P_OWNER" , "RENE" ); parameters.put("P_OBJECT_TYPE", "TABLE"); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn); JasperExportManager.exportReportToPdfFile(jasperPrint,"Ex_11_01.pdf"); } catch (Exception e) { System.out.println(e); } } } Third example
A4
<?xml version="1.0" encoding="UTF-8"?> <!-- A4 = 8.27 inches * 11.69 inches 8.27 inches * 72 dots/inch = 595 dots 11.69 inches * 72 dots/inch = 842 dots 83*7 = 581 (width) 57*14 = 798 (height) Real Character width: 7.2625 ====== --> <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 ="To be called by Java" pageWidth = "581" pageHeight = "798" columnWidth = "581" leftMargin = "0" rightMargin = "0" topMargin = "0" bottomMargin = "0" > <queryString> <![CDATA[ select dummy from dual ]]> </queryString> <detail> <band height="798"> <staticText> <reportElement x="0" y="0" width="603" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[0 1 2 3 4 5 6 7 8 ]]></text> </staticText> <staticText> <reportElement x="0" y="14" width="603" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[01234567890123456789012345678901234567890123456789012345678901234567890123456789 123]]></text> </staticText> <staticText> <reportElement x="0" y="28" width="604" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[third row: 123456789012345678901234567890123456789012345678901234567890123456789 123]]></text> </staticText> <staticText> <reportElement x="0" y="42" width="605" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[fourth row: 23456789012345678901234567890123456789012345678901234567890123456789 123]]></text> </staticText> <staticText> <reportElement x="0" y="56" width="606" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[fifth row: 123456789012345678901234567890123456789012345678901234567890123456789 123]]></text> </staticText> <staticText> <reportElement x="0" y="70" width="607" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[sixth row: 123456789012345678901234567890123456789012345678901234567890123456789 123]]></text> </staticText> <staticText> <reportElement x="0" y="84" width="608" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[seventh row: 3456789012345678901234567890123456789012345678901234567890123456789 123]]></text> </staticText> <staticText> <reportElement x="0" y="98" width="609" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[eighth row: 23456789012345678901234567890123456789012345678901234567890123456789 123]]></text> </staticText> <staticText> <reportElement x="0" y="112" width="610" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[nineth row: 23456789012345678901234567890123456789012345678901234567890123456789 123]]></text> </staticText> <staticText> <reportElement x="0" y="126" width="611" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[tenth row: 123456789012345678901234567890123456789012345678901234567890123456789 123]]></text> </staticText> <staticText> <reportElement x="0" y="140" width="612" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[eleventh row: 456789012345678901234567890123456789012345678901234567890123456789 123]]></text> </staticText> <staticText> <reportElement x="0" y="154" width="1000" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[twelth rwo: 23456789012345678901234567890123456789012345678901234567890123456789 123]]></text> </staticText> </band> </detail> </jasperReport> import java.sql.*; import java.util.HashMap; import java.util.Map; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.design.JasperDesign; import net.sf.jasperreports.engine.JasperManager; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.export.JRTextExporter; import net.sf.jasperreports.engine.export.JRTextExporterParameter; public class Ex_11_02 { public static void main(String [] args) { try { JasperDesign jasperDesign = JasperManager.loadXmlDesign("Ex_11_02.jrxml"); JasperReport jasperReport = JasperManager.compileReport(jasperDesign); Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@172.17.5.24:1521:newora11", "rene", // username "rene" // password ); Map parameters = new HashMap(); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn); JRTextExporter txtExporter = new JRTextExporter(); txtExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); txtExporter.setParameter(JRTextExporterParameter.BETWEEN_PAGES_TEXT ,"\f"); txtExporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT , new Integer(798)); txtExporter.setParameter(JRTextExporterParameter.PAGE_WIDTH , new Integer(581)); txtExporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH , new Integer( 7)); txtExporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT , new Integer( 14)); // txtExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,myfile.toString()); txtExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,"Ex_11_02.txt"); txtExporter.exportReport(); } catch (Exception e) { System.out.println(e); } } } Fourth example
Page size 5 (h) x 100 (w)
<?xml version="1.0" encoding="UTF-8"?> <!-- 5 Rows, of which each height is 14 dots: 70 101 Characters wide, each character 7 dots: 707 --> <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 ="To be called by Java" pageWidth = "707" pageHeight = "70" columnWidth = "707" leftMargin = "0" rightMargin = "0" topMargin = "0" bottomMargin = "0" > <queryString> <![CDATA[ select dummy from dual ]]> </queryString> <detail> <band height="70"> <staticText> <reportElement x="0" y="0" width="7" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[.]]></text> </staticText> <staticText> <reportElement x="70" y="0" width="7" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[.]]></text> </staticText> <staticText> <reportElement x="140" y="0" width="7" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[.]]></text> </staticText> <staticText> <reportElement x="210" y="0" width="7" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[.]]></text> </staticText> <staticText> <reportElement x="280" y="0" width="7" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[.]]></text> </staticText> <staticText> <reportElement x="350" y="0" width="7" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[.]]></text> </staticText> <staticText> <reportElement x="420" y="0" width="7" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[.]]></text> </staticText> <staticText> <reportElement x="490" y="0" width="7" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[.]]></text> </staticText> <staticText> <reportElement x="560" y="0" width="7" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[.]]></text> </staticText> <staticText> <reportElement x="630" y="0" width="7" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[.]]></text> </staticText> <staticText> <reportElement x="700" y="0" width="7" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[1]]></text> </staticText> <!-- <staticText> <reportElement x="0" y="0" width="720" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[. . . . . . . . . . 1]]></text> </staticText> --> <staticText> <reportElement x="0" y="14" width="730" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[0 1 2 3 4 5 6 7 8 9 0]]></text> </staticText> <staticText> <reportElement x="0" y="28" width="73" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[ 123456789]]></text> </staticText> <staticText> <reportElement x="70" y="28" width="73" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[ 123456789]]></text> </staticText> <staticText> <reportElement x="140" y="28" width="73" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[ 123456789]]></text> </staticText> <staticText> <reportElement x="210" y="28" width="73" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[ 123456789]]></text> </staticText> <staticText> <reportElement x="280" y="28" width="73" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[ 123456789]]></text> </staticText> <staticText> <reportElement x="350" y="28" width="73" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[ 123456789]]></text> </staticText> <staticText> <reportElement x="420" y="28" width="73" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[ 123456789]]></text> </staticText> <staticText> <reportElement x="490" y="28" width="73" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[ 123456789]]></text> </staticText> <staticText> <reportElement x="560" y="28" width="73" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[ 123456789]]></text> </staticText> <staticText> <reportElement x="630" y="28" width="73" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[ 123456789]]></text> </staticText> <staticText> <reportElement x="0" y="42" width="14" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[x]]></text> </staticText> <staticText> <reportElement x="7" y="56" width="14" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[y]]></text> </staticText> </band> </detail> </jasperReport> import java.sql.*; import java.util.HashMap; import java.util.Map; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.design.JasperDesign; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.export.JRTextExporter; import net.sf.jasperreports.engine.export.JRTextExporterParameter; import net.sf.jasperreports.engine.xml.JRXmlLoader; public class Ex_11_03 { public static void main(String [] args) { try { JasperDesign jasperDesign = JRXmlLoader.load("Ex_11_03.jrxml"); JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@172.17.5.24:1521:newora11", "rene", // username "rene" // password ); Map parameters = new HashMap(); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn); JRTextExporter txtExporter = new JRTextExporter(); txtExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); // 5 * 14 = 70 // 100 * 7 = 700 txtExporter.setParameter(JRTextExporterParameter.BETWEEN_PAGES_TEXT ,"\f"); // Do the following two really play a role? // // txtExporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT , new Integer( 70)); // txtExporter.setParameter(JRTextExporterParameter.PAGE_WIDTH , new Integer(707)); txtExporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH , new Integer( 7)); txtExporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT , new Integer( 14)); // txtExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,myfile.toString()); txtExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,"Ex_11_03.txt"); txtExporter.exportReport(); } catch (Exception e) { System.out.println(e); } } }
See also other Jasper examples.
|