DynamicJasper输出动态报表

DynamicJasper是一套简化JasperReports使用的API,它旨在减少开发自定义报表模板的复杂性,允许开发者高效地动态创建报表。

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

Jie 2017/09/27

DynamicJasper是一套API,其隐藏JasperReports的复杂性,帮助开发者节约定制模板、自动生成报表的时间。

<!-- DynamicJasper -->
<dependency>
	<groupId>ar.com.fdvs</groupId>
	<artifactId>DynamicJasper</artifactId>
	<version>${dynamicjasper.version}</version>
</dependency>


/**
 * DynamicJasper输出动态报表
 * 
 * @author Jie
 *
 */
public class DynamicReportDemo {
	private JRDataSource ds = null;

	/**
	 * 先实例化JRDataSource
	 * 
	 * @param coll
	 *            待输出的数据集合
	 */
	public void initJRDataSource(Collection coll) {
		ds = new JRBeanCollectionDataSource(coll);
	}

	/**
	 * 报表模板
	 * 
	 * @return
	 * @throws Exception
	 */
	public DynamicReport buildReport() throws Exception {
		FastReportBuilder builder = new FastReportBuilder();
		builder.addColumn("Name", "name", String.class.getName(), 30)
				.addColumn("Type", "type", String.class.getName(), 30)
				.addColumn("Mode", "mode", String.class.getName(), 30) //
				.setTitle("DynamicReportDemo").setSubtitle("This report was generated at " + new Date()) //
				.setPrintBackgroundOnOddRows(true).setUseFullPageWidth(true);

		DynamicReport dr = builder.build();
		return dr;
	}

	/**
	 * 输出类型enum
	 *
	 */
	public enum ExportType {
		CVS, HTML, PDF
	}

	/**
	 * 输出报表文件
	 * 
	 * @param type
	 * @param jp
	 * @param fileName
	 * @throws Exception
	 */
	public void export(ExportType type, JasperPrint jp, String fileName) throws Exception {
		switch (type) {
		case CVS:
			JRCsvExporter xlsExporter = new JRCsvExporter();
			xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
			xlsExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, fileName + ".cvs");
			xlsExporter.exportReport();
			break;
		case HTML:
			JRHtmlExporter htmlExporter = new JRHtmlExporter();
			htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
			htmlExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, fileName + ".html");
			htmlExporter.exportReport();
			break;
		case PDF:
			JRPdfExporter pdfExporter = new JRPdfExporter();
			pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
			pdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, fileName + ".pdf");
			pdfExporter.exportReport();
			break;
		}
	}

	/**
	 * 测试数据项内部类
	 *
	 */
	public class TData {
		private String name;
		private String type;
		private String mode;

		public TData(String name, String type, String mode) {
			this.name = name;
			this.type = type;
			this.mode = mode;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getType() {
			return type;
		}

		public void setType(String type) {
			this.type = type;
		}

		public String getMode() {
			return mode;
		}

		public void setMode(String mode) {
			this.mode = mode;
		}
	}

	/**
	 * 生成测试数据集合
	 * 
	 * @return
	 */
	public Collection createTDataCollection() {
		List<TData> coll = new ArrayList<TData>();
		for (int i = 0; i != 10; i++) {
			TData tmp = new TData("TData" + i, "Type" + i, "ASYNC");
			coll.add(tmp);
		}
		return coll;
	}

	/**
	 * 测试输出报表文件
	 * 
	 * @throws Exception
	 */
	public void testReport() throws Exception {
		DynamicReport dr = buildReport();
		JasperReport jr = DynamicJasperHelper.generateJasperReport(dr, new ClassicLayoutManager(), null);
		JasperPrint jp = JasperFillManager.fillReport(jr, null, ds);

		export(ExportType.PDF, jp, "DynamicReportDemo");
	}

	public static void main(String[] args) {
		DynamicReportDemo test = new DynamicReportDemo();
		test.initJRDataSource(test.createTDataCollection());
		try {
			test.testReport();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值