Jump to content
We've recently updated our Privacy Statement, available here ×

minhduc

Members
  • Posts

    10
  • Joined

  • Last visited

minhduc's Achievements

Rookie

Rookie (2/14)

  • Week One Done
  • One Month Later
  • One Year In
  • First Post Rare
  • Collaborator Rare

Recent Badges

0

Reputation

  1. sorry, I go wrong, should use JasperCompileManager.compileReportToFile(design, destFileName);
  2. write input to file to get jasper file public void writeToFile(InputStream is, File file) { try { DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); int c; while((c = is.read()) != -1) { out.writeByte©; } is.close(); out.close(); } catch(IOException e) { System.err.println("Error Writing/Reading Streams."); } }[/code]writeToFile(input, new File([jasper file]);
  3. I think you have a problem in setParameter; replaced excelExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out); by excelExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, filename); and check it out again. This is my sample package test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; import net.sf.jasperreports.engine.JRExporter; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JRResultSetDataSource; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.design.JasperDesign; import net.sf.jasperreports.engine.export.JRPdfExporter; import net.sf.jasperreports.engine.xml.JRXmlLoader; package test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; import net.sf.jasperreports.engine.JRExporter; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JRResultSetDataSource; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.design.JasperDesign; import net.sf.jasperreports.engine.export.JRXlsExporter; import net.sf.jasperreports.engine.xml.JRXmlLoader; public class JasperReportDemo{ public static void main(String[] args) throws SQLException, FileNotFoundException, IllegalAccessException, ClassNotFoundException,Exception{ try{ final String driverClass = "oracle.jdbc.driver.OracleDriver"; final String connectionURL = "jdbc:oracle:thin:@localhost:1521:pnb"; final String userID = "pnb"; final String userPassword = "pnb"; Connection con = null; Statement stmt = null; ResultSet rset = null; Class.forName(driverClass).newInstance(); con = DriverManager.getConnection(connectionURL, userID, userPassword); String queryString = "SELECT * FROM GLMAST"; stmt = con.createStatement(); rset = stmt.executeQuery(queryString); InputStream input = new FileInputStream(new File("template.jrxml")); JasperDesign design = JRXmlLoader.load(input); JasperReport report = JasperCompileManager.compileReport(design); JRResultSetDataSource jasperReports = new JRResultSetDataSource(rset); JasperPrint print = JasperFillManager.fillReport(report, new HashMap(), jasperReports); long start = System.currentTimeMillis(); JRExporter exporter; exporter = new JRXlsExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, print); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "C:/" + start + ".xls"); exporter.exportReport(); System.out.println("Done"); } catch(Exception e){ e.printStackTrace(); } } }
  4. Is a web application? When you deployed it on server, the File location and resource location are not same. If not, make sure com/jrxml/FirstReport.xml is located on your project folder, NOT in src folder. Anyway, I think you shoud use compileReport method and InputStream object. InputStream input = this.getClass().getResourceAsStream([your resource jrxml template ]); JasperDesign design = JRXmlLoader.load(input); JasperReport report = JasperCompileManager.compileReport(design); System.out.println("Template id loaded and complied"); JasperPrint print = JasperFillManager.fillReport(report, parameters, jRBeanArrayDataSource); System.out.println("Filled data"); JRExporter exporter = null; exporter = new JRXlsExporter(); Post Edited by minhduc at 04/02/2010 08:36
  5. rename your report template file to [template].jrxml and check it out again :))
  6. Jasper report get data from JRDataSource interface. Some Object implemented it are JRBeanArrayDataSource, JRBeanCollectionDataSource, JREmptyDataSource, JRHibernateAbstractDataSource, JRResultSetDataSource... If you want no connection used to export report. I think JRBeanArrayDataSource, JRBeanCollectionDataSource are resonable. In JRBeanArrayDataSource, it allowed to init one with JRBeanArrayDataSource(Object[]), so all of you need are a array of bean. And in JRBeanCollectionDataSource, JRBeanCollectionDataSource(Collection<Object>) can use. Therefore, List, ArrayList, Vector.... or any extended Interface or implemented Object can use.
  7. @hopesolutions: Nothing extra library need here. In font tag, fontName, isBold, isItalic or isUnderline property are not useful for pdf report exporter. In pdf report , the text style depend on the font filename. Example: If you want the text in pdf report file in Arial and : - Bold: pdfFontName="arialbd.ttf" - Italic: pdfFontName="ariali.ttf" ... make sure isPdfEmbedded="true" and embeded fonts were on your classpath application <font pdfFontName="arialbd.ttf" isPdfEmbedded="true"/> Äức, Trần Minh ductm@fpt.com.vn Post Edited by minhduc at 04/01/2010 11:17
  8. Pdf Embedded mode was turned on? <font fontName="Arial" pdfFontName="arialbd.ttf" isPdfEmbedded="true"/> In my sample, html report file will be in Arial font and normal style. But in pdf file, it displayed in bold style. Remember copy arialbd.tff font to your application class path. Äức, Trần Minh ductm@fpt.com.vn
  9. See the my report class to solve, all of you need is red text, dataSource instance of JRBeanCollectionDataSource variable is may be ArrayList, Vector of java Object (Login or Schedule in my sample), nothing db connection need here. Fields of report template have same of name and datatype of Object bean. See also : http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/data/JRBeanCollectionDataSource.html http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JRDataSource.html Äức, Trần Minh ductm@fpt.com.vn java file package com.form.user.report; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.Calendar; import java.util.HashMap; import java.util.List; import net.sf.jasperreports.engine.JRExporter; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.design.JasperDesign; import net.sf.jasperreports.engine.export.JRXlsExporter; import net.sf.jasperreports.engine.xml.JRXmlLoader; import com.data.mapping.Login; import com.data.mapping.Schedule; public class ReportUtil { public static final int SCHEDULE = 0; public static final int LOGEDIN = 1; public static String exportReport(Object dataSource, HashMap parameters, String filename, File template, int type){ try { Calendar calendar = Calendar.getInstance(); System.out.println(dataSource.getClass()); JRBeanCollectionDataSource jRBeanArrayDataSource = null; switch(type){ case SCHEDULE: jRBeanArrayDataSource = new JRBeanCollectionDataSource((List<Schedule>)dataSource); break; case LOGEDIN: jRBeanArrayDataSource = new JRBeanCollectionDataSource((List<Login>)dataSource); break; } System.out.println("Got data"); System.out.println(template.getAbsoluteFile()); InputStream input = new FileInputStream(template); JasperDesign design = JRXmlLoader.load(input); JasperReport report = JasperCompileManager.compileReport(design); System.out.println("Template id loaded and complied"); JasperPrint print = JasperFillManager.fillReport(report, parameters, jRBeanArrayDataSource); System.out.println("Filed data"); JRExporter exporter = null; exporter = new JRXlsExporter(); filename += ".xls"; exporter.setParameter(JRExporterParameter.JASPER_PRINT, print); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, filename); exporter.exportReport(); System.out.println("Done"); return filename; } catch (Exception ex) { ex.printStackTrace(); } return ""; } } Template file <?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="Schedule" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0"> <property name="ireport.zoom" value="1.0"/> <property name="ireport.x" value="0"/> <property name="ireport.y" value="0"/> <parameter name="DateFrom" class="java.util.Date"/> <parameter name="DateTo" class="java.util.Date"/> <field name="login" class="java.util.Date"/> <field name="useraccount.employee.username" class="java.lang.String"/> <field name="useraccount.employee.fullname" class="java.lang.String"/> <field name="useraccount.employee.department" class="java.lang.String"/> <field name="logout" class="java.util.Date"/> <variable name="PRINT_INFO" class="java.lang.String"> <variableExpression><![CDATA["Từ " + com.GeneralUtil.date2VNformat($P{DateFrom}) + " đến " + com.GeneralUtil.date2VNformat($P{DateTo})]]></variableExpression> <initialValueExpression><![CDATA["Từ " + com.GeneralUtil.date2VNformat($P{DateFrom}) + " đến " + com.GeneralUtil.date2VNformat($P{DateTo})]]></initialValueExpression> </variable> <variable name="PRINT_DATE" class="java.lang.String"> <variableExpression><![CDATA["Ngày in: " + com.GeneralUtil.date2VNformat(java.util.Calendar.getInstance().getTime())]]></variableExpression> <initialValueExpression><![CDATA["Ngày in: " + com.GeneralUtil.date2VNformat(java.util.Calendar.getInstance().getTime())]]></initialValueExpression> </variable> <group name="username"> <groupHeader> <band height="27"> <textField> <reportElement x="81" y="0" width="180" height="27" backcolor="#999999"/> <box> <topPen lineWidth="0.5"/> <leftPen lineWidth="0.5"/> <bottomPen lineWidth="0.5"/> <rightPen lineWidth="0.0"/> </box> <textElement> <font isBold="true" isUnderline="true"/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$F{useraccount.employee.username}]]></textFieldExpression> </textField> <textField> <reportElement x="261" y="0" width="159" height="27" backcolor="#999999"/> <box> <topPen lineWidth="0.5"/> <bottomPen lineWidth="0.5"/> <rightPen lineWidth="0.5"/> </box> <textElement> <font isBold="true" isUnderline="true"/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$F{useraccount.employee.fullname}]]></textFieldExpression> </textField> </band> </groupHeader> </group> <background> <band splitType="Stretch"/> </background> <title> <band height="130" splitType="Stretch"> <staticText> <reportElement x="81" y="65" width="339" height="20"/> <textElement textAlignment="Center"> <font size="12" isBold="true"/> </textElement> <text><![CDATA[bÁO CÁO THỜI GIAN TRUY CẬP HỆ THá»NG]]></text> </staticText> <staticText> <reportElement x="81" y="12" width="100" height="16"/> <textElement/> <text><![CDATA[bá»™ xây dá»±ng]]></text> </staticText> <staticText> <reportElement x="81" y="28" width="100" height="16"/> <textElement/> <text><![CDATA[ban QLDA Q.12]]></text> </staticText> <textField> <reportElement x="81" y="85" width="339" height="14"/> <textElement textAlignment="Center"> <font size="8"/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$V{PRINT_INFO}]]></textFieldExpression> </textField> <textField> <reportElement x="81" y="99" width="339" height="14"/> <textElement textAlignment="Center"> <font size="8"/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$V{PRINT_DATE}]]></textFieldExpression> </textField> </band> </title> <columnHeader> <band height="42" splitType="Stretch"> <staticText> <reportElement x="81" y="13" width="180" height="29" backcolor="#CCCCCC"/> <box> <topPen lineWidth="0.5"/> <leftPen lineWidth="0.5"/> <bottomPen lineWidth="0.5"/> <rightPen lineWidth="0.5"/> </box> <textElement textAlignment="Center"> <font fontName="Arial" size="11" isBold="true"/> </textElement> <text><![CDATA[Äăng nhập]]></text> </staticText> <staticText> <reportElement x="261" y="13" width="159" height="29" backcolor="#C0C0C0"/> <box> <topPen lineWidth="0.5"/> <bottomPen lineWidth="0.5"/> <rightPen lineWidth="0.5"/> </box> <textElement textAlignment="Center"> <font fontName="Arial" size="11" isBold="true"/> </textElement> <text><![CDATA[Äăng xuất]]></text> </staticText> </band> </columnHeader> <detail> <band height="20" splitType="Stretch"> <textField pattern="dd/MM/yyyy h:mm a"> <reportElement x="81" y="0" width="180" height="20"/> <box> <topPen lineWidth="0.0"/> <leftPen lineWidth="0.5" lineStyle="Solid"/> <bottomPen lineWidth="0.5" lineStyle="Dashed"/> <rightPen lineWidth="0.5" lineStyle="Solid"/> </box> <textElement/> <textFieldExpression class="java.util.Date"><![CDATA[$F{login}]]></textFieldExpression> </textField> <textField pattern="dd/MM/yyyy h:mm a"> <reportElement x="261" y="0" width="159" height="20"/> <box> <leftPen lineWidth="0.5" lineStyle="Solid"/> <bottomPen lineWidth="0.5" lineStyle="Dashed"/> <rightPen lineWidth="0.5" lineStyle="Solid"/> </box> <textElement/> <textFieldExpression class="java.util.Date"><![CDATA[$F{logout}]]></textFieldExpression> </textField> </band> </detail> <columnFooter> <band height="38" splitType="Stretch"> <textField> <reportElement x="320" y="8" width="100" height="20"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA["Trang: " + $V{PAGE_NUMBER}]]></textFieldExpression> </textField> </band> </columnFooter> <summary> <band height="125" splitType="Stretch"> <staticText> <reportElement x="173" y="56" width="100" height="20"/> <textElement/> <text><![CDATA[NgÆ°á»i lập]]></text> </staticText> <staticText> <reportElement x="313" y="56" width="100" height="20"/> <textElement/> <text><![CDATA[NgÆ°á»i duyệt]]></text> </staticText> <line> <reportElement x="79" y="37" width="339" height="1"/> <graphicElement> <pen lineStyle="Dashed"/> </graphicElement> </line> <textField> <reportElement x="320" y="9" width="100" height="20"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA["Trang: " + $V{PAGE_NUMBER}]]></textFieldExpression> </textField> </band> </summary> </jasperReport>
  10. Hello every expert jasperreport developers :P; I'm a newbie JR. My pdf report file have some Vietnamese characters. It seem to be hard to done, 'cuz when I tried, the Vietnamese character losed :(. I has googled and did: - Copy font (Arial Bold, arialbd.ttf.) to classpath of java code. - Put pdf font file name into to attribute of font tag and turn on embed font to pdf. <font fontName="Arial" pdfFontName="arialbd.ttf" isPdfEmbedded="true"/> Try again, but my original text "Ngân Hàng Äức" displayed "Ngân Hàng Ä?c" with Arial bold style. I saw the arial font has put is right locaction, 'cuz if it was wrong, pdf file has display my text with Helvetica font. I'm changed pdfEncoding="Cp1258", run report again. But nothing changed :(. <font fontName="Arial" pdfFontName="arialbd.ttf" pdfEncoding="Cp1258" isPdfEmbedded="true"/> Anybody can support me :(. Thanks in advanced, Äức, Trần Minh - ductm@fpt.com.vn
×
×
  • Create New...