JasperReports

本文介绍如何使用JasperReports生成各种格式的报表文件,包括从*.jrxml文件开始,到生成并填充*.jasper文件,最后导出为Excel、PDF及XML等多种格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

生成*.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文件导出报表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值