IReport study notes, Study Notes

Source: Internet
Author: User

IReport study notes, Study Notes
Overview

This section describes how to generate pdf documents based on jasper reports and data, solutions to Chinese font problems, and formatted output of date and time.

IReport version: 5.2.0

Generate maven dependency for pdf documents
<dependency>    <groupId>net.sf.jasperreports</groupId>    <artifactId>jasperreports</artifactId>    <version>5.2.0</version></dependency>

Complete dependency tree:

  

[INFO] +- net.sf.jasperreports:jasperreports:jar:5.2.0:compile
[INFO] |  +- commons-beanutils:commons-beanutils:jar:1.8.0:compile
[INFO] |  +- commons-digester:commons-digester:jar:2.1:compile
[INFO] |  +- com.lowagie:itext:jar:2.1.7.js2:compile
[INFO] |  |  +- bouncycastle:bcmail-jdk14:jar:138:compile
[INFO] |  |  +- bouncycastle:bcprov-jdk14:jar:138:compile
[INFO] |  |  \- org.bouncycastle:bctsp-jdk14:jar:1.38:compile
[INFO] |  |     +- org.bouncycastle:bcprov-jdk14:jar:1.38:compile
[INFO] |  |     \- org.bouncycastle:bcmail-jdk14:jar:1.38:compile
[INFO] |  +- jfree:jcommon:jar:1.0.15:compile
[INFO] |  +- jfree:jfreechart:jar:1.0.12:compile
[INFO] |  +- xml-apis:xml-apis:jar:1.3.02:compile
[INFO] |  +- eclipse:jdtcore:jar:3.1.0:compile
[INFO] |  +- org.codehaus.castor:castor:jar:1.2:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-core:jar:2.0.5:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.0.5:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-annotations:jar:2.0.5:compile
[INFO] \- sinobest:font-song:jar:1.0:compile

Here, font-song jar is a self-made font jar package: http://pan.baidu.com/s/1nzmkm.

SQL-based data 1. Configure the data source
  

public void test() throws JRException, IOException, SQLException { InputStream is = Main.class.getClassLoader().getResourceAsStream("reports/XZFYSQS-query.jasper"); Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("SQD_SYSID", "4028ca9850d19ccb0150d19cd1ae0000"); Connection conn = ...; byte[] datas = JasperRunManager.runReportToPdf(is, parameters, conn); conn.close();}

Data Based on JavaBean 1. Create a JavaBean
package cn.sinobest.jzpt.fzywgz.reports.pojo;public class XZFYSQS_RY {    private java.lang.String SQR;    private java.lang.String XB;    private java.sql.Timestamp CSNY;    ......}

2. Add the compilation path to iReport.
  

public void test() throws JRException, IOException { InputStream is = Main.class.getClassLoader() .getResourceAsStream(reports/XZFYSQS-javabean.jasper); List<XZFYSQS_RY> ryList = new ArrayList<XZFYSQS_RY>(); ryList.add(...); JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource( ryList); byte[] datas = JasperRunManager.runReportToPdf(is, null, dataSource); ...}

Chinese font Solution

When generating pdf content based on the template during programming, Chinese characters may not be printed. The solution is as follows:

  1. Add the font jar package: font-song
    Http://pan.baidu.com/s/1NzMkm
  2. Chinese font, which can be simplified to simplified Chinese characters, such as,, and .
  3. Set Properties of the Chinese component
    Date and Time formatting output

    Right-click TextField and choose Field pattern.

    For the date type, MM always displays 2-digit month, M Displays 1 or 2-digit as needed, and other fields and so on.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.