2008-07-05

JasperReports

生成*.Jasper文件
import net.sf.jasperreports.engine.*;

public class MyCompile
{
    public static void main(String[] args) throws Exception
    {
		JasperCompileManager.compileReportToFile("static.jrxml" , "static.jasper");
		System.out.println("成功编译成JasperReport文件(*.jasper)");
		System.exit(0);
    }
}


为报表填充数据 可以填充报表的数据源,参数等 。使用JasperFillManager类完成。生成*.jsprint文件
import net.sf.jasperreports.engine.*;

public class MyFill
{
    public static void main(String[] args) throws Exception
    {
		//填充时,即使没有使用数据源,也必须指定一个新建的JREmptyDataSource实例,而不能直接使用null
		JasperFillManager.fillReportToFile("static.jasper" , null , new JREmptyDataSource());
		System.out.println("成功填充了一个报表文件(*.jrprint)");
		System.exit(0);
    }
}

-------------------------------------------------------------------------------------------------------
用JFrame 来装载JRViewer,从而允许以窗口的方式查看

import net.sf.jasperreports.view.*;
import javax.swing.JFrame;

public class MyJRViewer
{
    public static void main(String[] args) throws Exception
    {
		JRViewer jr = new JRViewer("static.jrprint" , false);
		JFrame jf = new JFrame("报表预览");
		jf.add(jr);
		jf.pack();
		jf.setVisible(true);

    }
}


生成Excel文档,导入POI项目
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.*;
import net.sf.jasperreports.engine.util.*;

public class MyExportExcel
{
    public static void main(String[] args) throws Exception
    {
		JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject("static.jrprint");
		JRXlsExporter exporter = new JRXlsExporter();
		//设置要导出的jasperPrint
		exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
		exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "static.xls");
		exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
		
		exporter.exportReport();

		System.out.println("成功创建了一个XML文档");
		System.exit(0);
    }
}

导出PDF文件
import net.sf.jasperreports.engine.*;

public class MyExportPdf
{
    public static void main(String[] args) throws Exception
    {
		JasperExportManager.exportReportToPdfFile("static.jrprint" , "static.pdf");
		System.out.println("成功创建了一个PDF文档");
		System.exit(0);
    }
}

导出xml文件
import net.sf.jasperreports.engine.*;

public class MyExportXml
{
    public static void main(String[] args) throws Exception
    {
		JasperExportManager.exportReportToXmlFile("static.jrprint" , "static.xml" , true);
		System.out.println("成功创建了一个XML文档");
		System.exit(0);
    }
}



开发流程:
1,定义*.jrxml文件
2,使用JasperReports提供的JasperCompileManager工具编译。*.jrxml文件,生成*.jasper
3,使用JasperReports提供的JasperFileManager工具填充编译后的*.jasper文件
4,使用JasperExportManager 或 JRXxxExporter,将*.jrprint文件导出报表。
评论
发表评论

您还没有登录,请登录后发表评论

wangyu
搜索本博客
我的相册
4a4a4c43-84f9-3c9d-9259-f29f3081cdc9-thumb
strutsWorkflow
共 3 张
最近加入圈子
存档
最新评论