Fork me on GitHub

1.导入jar包(pom.xml构建)

<dependencies>
		<dependency>
			<groupId>com.lowagie</groupId>
			<artifactId>itext</artifactId>
			<version>4.2.1</version>
		</dependency>
		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itext-asian</artifactId>
			<version>5.2.0</version>
		</dependency>

		
		<!-- oracle数据库驱动,需要手动安装 -->
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc6</artifactId>
			<version>11.2.0.1.0</version>
		</dependency>
		<!-- jasperreport -->
		<dependency>
			<groupId>net.sf.jasperreports</groupId>
			<artifactId>jasperreports</artifactId>
			<version>5.2.0</version>
		</dependency>
		<dependency>
			<groupId>com.baidu.maven</groupId>
			<artifactId>bos_crm_domain</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<!-- groovy -->
		<dependency>
			<groupId>org.codehaus.groovy</groupId>
			<artifactId>groovy-all</artifactId>
			<version>2.2.0</version>
		</dependency>

		<!-- jasperreport -->
		<dependency>
			<groupId>net.sf.jasperreports</groupId>
			<artifactId>jasperreports</artifactId>
			<version>5.2.0</version>
			<exclusions>
				<exclusion>
					<groupId>com.lowagie</groupId>
					<artifactId>itext</artifactId>
				</exclusion>
			</exclusions>
		</dependency>  
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.5.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

2.jrxml文件的生成

<?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="report2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="286e47bd-f0a8-4480-af4f-a3c4553e6738">
	<property name="ireport.zoom" value="1.0"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="27"/>
	<style name="table">
		<box>
			<pen lineWidth="1.0" lineColor="#000000"/>
		</box>
	</style>
	<style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
		<box>
			<pen lineWidth="0.5" lineColor="#000000"/>
		</box>
	</style>
	<style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
		<box>
			<pen lineWidth="0.5" lineColor="#000000"/>
		</box>
	</style>
	<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
		<box>
			<pen lineWidth="0.5" lineColor="#000000"/>
		</box>
	</style>
	<style name="table 1">
		<box>
			<pen lineWidth="1.0" lineColor="#000000"/>
		</box>
	</style>
	<style name="table 1_TH" mode="Opaque" backcolor="#F0F8FF">
		<box>
			<pen lineWidth="0.5" lineColor="#000000"/>
		</box>
	</style>
	<style name="table 1_CH" mode="Opaque" backcolor="#BFE1FF">
		<box>
			<pen lineWidth="0.5" lineColor="#000000"/>
		</box>
	</style>
	<style name="table 1_TD" mode="Opaque" backcolor="#FFFFFF">
		<box>
			<pen lineWidth="0.5" lineColor="#000000"/>
		</box>
	</style>
	<subDataset name="Table Dataset 1" uuid="498dc51b-6cc6-416a-a7d1-2763a61727c4"/>
	<subDataset name="Table Dataset 2" uuid="6da0b8af-a1e2-4b15-8640-ae77a590da68"/>
	<subDataset name="Table Dataset 3" uuid="6806ec02-e14e-478e-b69a-1fd51a93cd31"/>
	<parameter name="comp" class="java.lang.String"/>
	<queryString>
		<![CDATA[select * from T_COURIER t]]>
	</queryString>
	<field name="company" class="java.lang.String"/>
	<field name="name" class="java.lang.String"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="79" splitType="Stretch">
			<textField>
				<reportElement x="125" y="59" width="100" height="20" uuid="66b58fb1-01ef-4277-ba9a-16fb0fca6b79"/>
				<textElement>
					<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{comp}]]></textFieldExpression>
			</textField>
		</band>
	</title>
	<pageHeader>
		<band height="35" splitType="Stretch"/>
	</pageHeader>
	<columnHeader>
		<band height="61" splitType="Stretch">
			<staticText>
				<reportElement x="104" y="41" width="100" height="20" uuid="bbce2650-4d21-4404-947a-89847081e5bc"/>
				<textElement>
					<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[姓名]]></text>
			</staticText>
			<staticText>
				<reportElement x="4" y="41" width="100" height="20" uuid="01b2ebc7-a4fe-4987-bfb2-a55979a83144"/>
				<textElement>
					<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[公司]]></text>
			</staticText>
		</band>
	</columnHeader>
	<detail>
		<band height="125" splitType="Stretch">
			<textField>
				<reportElement x="104" y="0" width="100" height="20" uuid="c063769d-59aa-4969-ab66-bc2270481b85"/>
				<textElement>
					<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="4" y="0" width="100" height="20" uuid="99b612be-749c-41b1-9916-038c6a52ef08"/>
				<textElement>
					<font fontName="宋体" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{company}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
	<columnFooter>
		<band height="45" splitType="Stretch"/>
	</columnFooter>
	<pageFooter>
		<band height="54" splitType="Stretch"/>
	</pageFooter>
	<summary>
		<band height="42" splitType="Stretch"/>
	</summary>
</jasperReport>

  文件是由软件自动生成的,无需手动写。

3.Demo编写

在这里进行数据的数据查询需要连接驱动

  使用原生的jdbc代码进行数据可的访问

package com.baidu.test01;

import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.export.JRPdfExporter;

import com.baidu.bos_crm_domain.domain.Courier;

public class DemoJavaPdf {
	public static void main(String[] args) throws Exception {
		/**
		 * 查询数据
		 */
		// 查询出 满足当前条件 结果数据
		List<Courier> c = connOr();
		// 下载导出
		// 设置头信息
		// ServletActionContext.getResponse().setContentType("application/pdf");
		// String filename = "运单数据.pdf";
		// String agent = ServletActionContext.getRequest()
		// .getHeader("user-agent");
		// filename = FileUtils.encodeDownloadFilename(filename, agent);
		// ServletActionContext.getResponse().setHeader("Content-Disposition",
		// "attachment;filename=" + filename);
		// 根据 jasperReport模板 生成pdf
		// 读取模板文件
		String jrxml ="C:\\Users\\Administrator\\Desktop\\report2.jrxml";
		JasperReport report = JasperCompileManager.compileReport(jrxml);
		// 设置模板数据
		// Parameter变量
		Map<String, Object> paramerters = new HashMap<String, Object>();
		paramerters.put("comp", "传智播客");
		// Field变量
		JasperPrint jasperPrint = JasperFillManager.fillReport(report,
				paramerters, new JRBeanCollectionDataSource(c));
		//System.out.println(wayBills);
		// 生成PDF客户端
		 JRPdfExporter exporter = new JRPdfExporter();
		 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
		 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
		 new FileOutputStream("111123.pdf"));
		 exporter.exportReport();// 导出
		// ServletActionContext.getResponse().getOutputStream().close();

		// return NONE;

	}

	public static List<Courier> connOr() {
		List<Courier> list=new ArrayList<Courier>();
		try {
			// 加载驱动
			Class.forName("oracle.jdbc.driver.OracleDriver");
			// 创建连接Connection
			Connection conn = DriverManager.getConnection(
					"jdbc:oracle:thin:@192.168.189.189:1521:ORCL", "bos",
					"root");
			// 得到执行sql的statement对象
			// conn.createStatement();
			PreparedStatement ps = conn
					.prepareStatement("select * from T_COURIER t");
			// 执行语句,并返回结果
			ResultSet rs = ps.executeQuery();
			// 处理结果
			while (rs.next()) {
				Courier u = new Courier();
				u.setId(rs.getInt(1));
				u.setCompany(rs.getString(3));
				u.setName(rs.getString(6));
				list.add(u);
			}
			System.out.println(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}

注意事项:

a.在创建jrxml文件时需要注意

在进行赋值时会不一样,因为在创建模板的时候可以看到

在内容的显示的时候也是不一样的

$P   是由parameters产生的

$F  是由fields产生的

所以在产生数据时会不一样

 

b.字段匹配

必须进行字段匹配 否则还是不能进行传值

 

posted on 2018-01-08 20:49  TopTime  阅读(5984)  评论(0编辑  收藏  举报