当前位置: 代码迷 >> ASP >> JasperReports组合iReport制作报表-使用JasperReports生成报表
  详细解决方案

JasperReports组合iReport制作报表-使用JasperReports生成报表

热度:594   发布时间:2012-06-29 15:48:46.0
JasperReports结合iReport制作报表-使用JasperReports生成报表
《JasperReports结合iReport制作报表-使用iReport设计报表》
已经在http://www.naxsu.com/jasperreports-jie-he-ireport-zhi-zuo-bao-biao-shi-yong-ireport-she-ji-bao-biao/写得很清楚了,下面就接着说

下载JasperReports

JasperReports主页:http://jasperforge.org/projects/jasperreports

下载要注册,真麻烦

现在的最新版本是4.6.0,下载地址是:http://sourceforge.net/projects/jasperreports/files/jasperreports/JasperReports%204.6.0/

在这里我下载的是jasperreports-4.6.0-project.zip

?

环境配置

需要的jar包,这是我测试需求哪个加哪个,我生成PDFHTML报表用到以下已经足够了。

commons-beanutils-1.8.0.jar

commons-collections-2.1.1.jar

commons-digester-2.1.jar

commons-logging-1.1.1.jar

groovy-all-1.7.5.jar

iText-2.1.7.jar

iTextAsian.jar

jasperreports-4.6.0.jar

mysql-connector-java-5.1.7-bin.jar

?

测试程序

在这里我们就建一个简单的java项目来测试一下。

?

生成报表主要分为三个步骤:

<!--[if !supportLists]-->1.?????? <!--[endif]-->编译报表

iReport设计好的报表report1.jrxml编译成report1.jasper

<!--[if !supportLists]-->2.?????? <!--[endif]-->填充报表

连接数据库,把1编译好的report1.jasper填充好,生成report1.jrprint

<!--[if !supportLists]-->3.?????? <!--[endif]-->导出报表

也就是把2.填充好的报表按照各种格式导出来。

?

代码比较简单,下面直接看代码:

package com.naxsu.report;

import java.io.File;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import net.sf.jasperreports.engine.JRException;

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

import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;

import net.sf.jasperreports.engine.export.JRPdfExporter;

import net.sf.jasperreports.engine.util.JRLoader;

/**

?* JasperReports生成报表简单应用

?*/

public class ReportTest {

??? /**

??? ?* 编译报表

??? ?*/

??? public static void compiler(String sourceFileName, String destFileName) {

?????? try {

?????????? JasperCompileManager.compileReportToFile(sourceFileName,

????????????????? destFileName);

?????? } catch (JRException e) {

?????????? e.printStackTrace();

?????? }

??? }

?

??? /**

??? ?* 填充报表

??? ?*/

??? public static void fill(String sourceFileName, String destFileName) {

?????? Connection conn = null;

?????? try {

?????????? Class.forName("com.mysql.jdbc.Driver");

?????????? conn = DriverManager.getConnection(

????????????????? "jdbc:mysql://localhost:3306/report", "root", "123");

?????????? JasperFillManager.fillReportToFile(sourceFileName, destFileName,null, conn);

?????? } catch (JRException e) {

?????????? e.printStackTrace();

?????? } catch (ClassNotFoundException e) {

?????????? e.printStackTrace();

?????? } catch (SQLException e) {

?????????? e.printStackTrace();

?????? }finally{

?????????? if(conn != null) {

????????????? try {

????????????????? conn.close();

????????????? } catch (SQLException e) {

????????????????? e.printStackTrace();

????????????? }

?????????? }

?????? }

??? }

?

??? /**

??? ?* 导出HTML格式报表

??? ?*/

??? public static void exportToHTML(String sourceFileName, String destFileName) {

?????? try {

?????????? JasperPrint jp = (JasperPrint) JRLoader.loadObject(new File(

????????????????? sourceFileName));

?????????? JRHtmlExporter export = new JRHtmlExporter();

?????????? export.setParameter(JRExporterParameter.JASPER_PRINT, jp);

?????????? export.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,

????????????????? destFileName);

?????????? export.setParameter(

????????????????? JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,

????????????????? Boolean.FALSE);

?????????? export.exportReport();

?????? } catch (JRException e) {

?????????? e.printStackTrace();

?????? }

??? }

?

??? /**

??? ?* 导出PDF格式报表

??? ?*/

??? public static void exportToPDF(String sourceFileName, String destFileName) {

?????? try {

?????????? JasperPrint jp = (JasperPrint) JRLoader.loadObject(new File(

????????????????? sourceFileName));

?????????? JRPdfExporter export = new JRPdfExporter();

?????????? export.setParameter(JRExporterParameter.JASPER_PRINT, jp);

?????????? export.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,

????????????????? destFileName);

?????????? export.setParameter(

????????????????? JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,

????????????????? Boolean.FALSE);

?????????? export.exportReport();

?????? } catch (JRException e) {

?????????? e.printStackTrace();

?????? }

??? }

?

??? public static void main(String[] args) {

?????? String jrxmlName = "E:\\JasperReports_iReport\\report1.jrxml";

?????? String jasperFileName = "E:\\JasperReports_iReport\\report1.jasper";

?????? String jrprintFileName = "E:\\JasperReports_iReport\\report1.jrprint";

?????? String htmlFileName = "E:\\JasperReports_iReport\\report1.pdf";

?????? ReportTest.compiler(jrxmlName, jasperFileName);

?????? ReportTest.fill(jasperFileName, jrprintFileName);

?????? ReportTest.exportToPDF(jrprintFileName, htmlFileName);

??? }

}

?PDF电子版请到:http://itway.iteye.com/blog/1555285下载