iReport编码生成表报

1.拷贝必须的JAR包到项目的lib下。
commons-beanutils-1.7.jar
commons-collections-2.1.jar
commons-digester-1.7.jar
commons-javaflow-20060411.jar
commons-logging-1.0.2.jar
iReport.jar
itext-1.3.1.jar
iTextAsian.jar
jasperreports-2.0.5.jar
jdt-compiler-3.1.1.jar
poi-3.0.1-FINAL-20070705.jar


commons-beanutils-1.7.jar
commons-collections-2.1.jar
commons-digester-1.7.jar
commons-javaflow-20060411.jar
commons-logging-1.0.2.jar
iReport.jar
itext-1.3.1.jar
iTextAsian.jar
jasperreports-2.0.5.jar
jdt-compiler-3.1.1.jar
poi-3.0.1-FINAL-20070705.jar

2.新建一个目录,用来存放设计文件,jrxml文件。

3.新建一个目录,用来存放编译后的文件。

4.编译报表。
/**
	 * 编译报表
	 */
	public static void compiler(String sourceFileName, String destFileName) {
		try {
			JasperCompileManager.compileReportToFile(sourceFileName, destFileName);
		} catch (JRException e) {
			e.printStackTrace();
		}
	}

5.填充报表
/**
	 * 填充报表
	 */
	public static void fill(String sourceFileName, String destFileName) {
		try {
			JasperFillManager.fillReportToFile(sourceFileName, destFileName, new HashMap(), DataBaseManager.getConnection());
		} catch (JRException e) {
			e.printStackTrace();
		}
	}
6.导出HTML格式报表。
/**
	 * 导出HTML格式报表
	 */
	public static void exportToHTML(String sourceFileName,String destFileName) {
		try {
			JasperPrint jp = (JasperPrint) JRLoader.loadObject(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();
		}

	}


7.导出PDF格式报表。
public static void exportToPdf(String sourceFileName,String destFileName) {
		try {
			JasperPrint jp = (JasperPrint) JRLoader.loadObject(sourceFileName);
			JRPdfExporter export = new JRPdfExporter();
			export.setParameter(JRExporterParameter.JASPER_PRINT, jp);
			export.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFileName);
			export.exportReport();
		} catch (JRException e) {
			e.printStackTrace();
		}

	}





package com.ibeifeng.report;

import java.util.HashMap;

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;

import com.ibeifeng.jdbc.DataBaseManager;

/*
 * 用来编译、填充、生成报表
 */
public class MyReportManager {
	/**
	 * 编译报表
	 */
	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) {
		try {
			JasperFillManager.fillReportToFile(sourceFileName, destFileName, new HashMap(), DataBaseManager.getConnection());
		} catch (JRException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 导出HTML格式报表
	 */
	public static void exportToHTML(String sourceFileName,String destFileName) {
		try {
			JasperPrint jp = (JasperPrint) JRLoader.loadObject(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(sourceFileName);
			JRPdfExporter export = new JRPdfExporter();
			export.setParameter(JRExporterParameter.JASPER_PRINT, jp);
			export.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFileName);
			export.exportReport();
		} catch (JRException e) {
			e.printStackTrace();
		}

	}
	
	
}


package com.ibeifeng.test;

import com.ibeifeng.report.MyReportManager;

public class TestReport {
	public static void main(String[] args) {
		String jrxmlName = "E:\\Tomcat 6.0\\webapps\\BeifengQuote\\report\\jrxml\\customer.jrxml";
		String jasperFileName = "E:\\Tomcat 6.0\\webapps\\BeifengQuote\\report\\jasper\\customer.jasper";
		String jrprintFileName = "E:\\Tomcat 6.0\\webapps\\BeifengQuote\\report\\jrprint\\customer.jrprint";
		String htmlFileName = "E:\\Tomcat 6.0\\webapps\\BeifengQuote\\report\\html\\customer.html";
		MyReportManager.compiler(jrxmlName, jasperFileName);
		MyReportManager.fill(jasperFileName, jrprintFileName);
		MyReportManager.exportToHTML(jrprintFileName, htmlFileName);
		MyReportManager.exportToPdf(jrprintFileName, htmlFileName);
	}
}

你可能感兴趣的:(html,tomcat,.net,jdbc)