日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

JasperReport(3)——Java簡單使用IReport生成的

系統 1901 0

先看看設計的報表樣式:


JasperReport(3)——Java簡單使用IReport生成的文件建立報表
?reportTitle是新添加的一個參數,而其他的id和name是通過數據源得到的Filed。IReport在設置參數的時候需要先在左邊新建一個parameter,然后再把該parameter托到右邊的設計欄中。

?

生成的XML文件為:

    <?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="report3" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" 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"/>
	<queryString>
		<![CDATA[select * from t_blog;]]>
	</queryString>
	<field name="id" class="java.lang.Integer"/>
	<field name="content" class="java.lang.String"/>
	<field name="postTime" class="java.sql.Timestamp"/>
	<field name="seenCount" class="java.lang.Integer"/>
	<field name="title" class="java.lang.String"/>
	<field name="blogStore" class="java.lang.Integer"/>
	<field name="owner" class="java.lang.Integer"/>
	<field name="sysCategory" class="java.lang.Integer"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="44" splitType="Stretch">
			<staticText>
				<reportElement x="227" y="12" width="100" height="20" forecolor="#FF0000"/>
				<textElement textAlignment="Center">
					<font size="13" isBold="true"/>
				</textElement>
				<text><![CDATA[標題]]></text>
			</staticText>
		</band>
	</title>
	<pageHeader>
		<band height="35" splitType="Stretch">
			<staticText>
				<reportElement x="227" y="15" width="100" height="20" forecolor="#FF0000"/>
				<textElement textAlignment="Center">
					<font size="13" isBold="true"/>
				</textElement>
				<text><![CDATA[頁眉]]></text>
			</staticText>
		</band>
	</pageHeader>
	<columnHeader>
		<band height="40" splitType="Stretch">
			<staticText>
				<reportElement x="70" y="2" width="100" height="20"/>
				<textElement/>
				<text><![CDATA[id]]></text>
			</staticText>
			<staticText>
				<reportElement x="214" y="2" width="100" height="20"/>
				<textElement/>
				<text><![CDATA[title]]></text>
			</staticText>
			<staticText>
				<reportElement x="373" y="2" width="100" height="20"/>
				<textElement/>
				<text><![CDATA[postTime]]></text>
			</staticText>
		</band>
	</columnHeader>
	<detail>
		<band height="81" splitType="Stretch">
			<textField>
				<reportElement x="70" y="24" width="100" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="214" y="25" width="100" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$F{title}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="373" y="26" width="100" height="20"/>
				<textElement/>
				<textFieldExpression><![CDATA[$F{postTime}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
	<columnFooter>
		<band height="40" splitType="Stretch">
			<staticText>
				<reportElement x="227" y="10" width="100" height="20" forecolor="#FF0000"/>
				<textElement textAlignment="Center">
					<font size="13" isBold="true"/>
				</textElement>
				<text><![CDATA[相當于表尾]]></text>
			</staticText>
		</band>
	</columnFooter>
	<pageFooter>
		<band height="43" splitType="Stretch">
			<staticText>
				<reportElement x="227" y="13" width="100" height="20" forecolor="#FF0000"/>
				<textElement textAlignment="Center">
					<font size="13" isBold="true"/>
				</textElement>
				<text><![CDATA[頁腳]]></text>
			</staticText>
		</band>
	</pageFooter>
	<summary>
		<band height="41" splitType="Stretch">
			<staticText>
				<reportElement x="227" y="10" width="100" height="20" forecolor="#FF0000"/>
				<textElement textAlignment="Center">
					<font size="13" isBold="true"/>
				</textElement>
				<text><![CDATA[用于存放一些統計信息的]]></text>
			</staticText>
		</band>
	</summary>
</jasperReport>

  

?

JasperReport在生成報表的時候可以直接操作jrxml文件,然后通過Java代碼編譯為.jasper文件,也可以直接操作IReport編譯好的.jasper文件,下面就是用的直接操作.jasper文件,注釋掉的是操作jrxml文件的。JasperReport可以把生成的報表導出為多種格式,下面導出的是普通的HTML。JasperReport在往模版里面設置參數值,是提供一個Map進行參數傳入的。

java代碼:

    import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;

public class Test2 {

	public static void main(String args[]) throws JRException, ClassNotFoundException, SQLException, FileNotFoundException {
		String fileName = "E:\\reports\\report2.jrxml";//直接操作生成的jrxml文件
		JasperReport jasperReport = null;
		JasperPrint jasperPrint = null;
//		jasperReport = JasperCompileManager.compileReport(fileName);//編譯jrxml文件
		InputStream inputStream = new FileInputStream("E:\\reports\\report2.jasper");
		Map<String, Object> parameters = new HashMap<String, Object>();//傳入參數
		parameters.put("reportTitle", "我的第一個程序");
//		jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, getConnection());
		jasperPrint = JasperFillManager.fillReport(inputStream, parameters, getConnection());
		JasperExportManager.exportReportToHtmlFile(jasperPrint, "first.html");
	}
	
	public static Connection getConnection() throws ClassNotFoundException, SQLException {
		Connection conn = null;
		Class.forName("com.mysql.jdbc.Driver");
		conn = DriverManager.getConnection("jdbc:mysql://localhost/blog", "root", "root");
		return conn;
	}
	
}

  
?

導出的HTML預覽界面:


?

JasperReport(3)——Java簡單使用IReport生成的文件建立報表


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 莎车县| 石门县| 花垣县| 随州市| 确山县| 晋城| 凤台县| 即墨市| 烟台市| 呼玛县| 新绛县| 武威市| 凤冈县| 濮阳县| 武义县| 彰化县| 察雅县| 阿鲁科尔沁旗| 建阳市| 赣榆县| 衡水市| 岳池县| 新民市| 鄄城县| 武定县| 交口县| 青州市| 天等县| 金沙县| 尼玛县| 枣强县| 馆陶县| 宜兰县| 拜城县| 元阳县| 庐江县| 时尚| 仪陇县| 溆浦县| 孙吴县| 东光县|