使用FineReport报表二次开发详述
- 格式:doc
- 大小:707.00 KB
- 文档页数:38
∙帆软(中国)有限公司∙商务咨询电话:86-025-5186 2240∙商务咨询邮箱:business@ ∙公司网址:使用FineReport报表二次开发详述在大多数情况下FineReport都可以完全满足用户制作各种报表的需求,但是为了能够满足个别用户的个性化需求,FineReport提供了丰富二次开发接口,方便用户将FineReport和自己的项目实现无缝结合,用户在进行二次开发的工程中可以更加深入的体会到FineReport工具的强大功能。
例如:在一些特殊领域,可能需要一些特殊的函数。
或者要将某个模板文件通过指定的打印机打印等等。
∙帆软(中国)有限公司∙商务咨询电话:86-025-5186 2240∙商务咨询邮箱:business@ ∙公司网址:目录Report的输入输出 (3)单元格格式设置 (7)将模板通过指定打印机打印 (10)自定义函数 (12)URL 传递参数 (22)向报表中添加单元格 (25)读取单元格内容 (29)创建程序网络模版 (32)读取模板报表 (36)∙帆软(中国)有限公司∙商务咨询电话:86-025-5186 2240∙商务咨询邮箱:business@ ∙公司网址:Report的输入输出FineReport提供了强大的输入输出功能,所有的这些输入输出的类都在com.fr.report.io包里面。
Report的输入指从报表的模板文件(XML格式的)创建Report对象,输出指将Report保存为模板文件,FineReport还支持将Report保存为PDF,Excel,Word,SVG,HTML,CSV等文件格式。
∙读取模板文件∙保存成模板文件∙输出成PDF文件∙输出成Word文件∙输出成Excel文件∙输出成文本文件∙可执行代码读取模板文件// 读取模板File cptFile = new File("D:\\stuff.cpt");TemplateImporter templateImporter = new TemplateImporter(); WorkBook workBook = (WorkBook)templateImporter.generate(cptFile);Stuff.cpt是用报表设计器生成的模板文件。
只需要用建立一个TemplateImporter对象,然后调用它的generateReport()方法来产生一个Report对象,同时可以将产生的Report对象强制转换成WorkSheet或者GroupReport。
保存成模板文件// CPT// 清空公式计算结果E:\\newtemplate\\stuff.cpt这个是导出后新文档生成的地址ReportHelper.clearFormulaResult(workBook);outputStream = new FileOutputStream(newFile("E:\\newtemplate\\stuff.cpt"));∙帆软(中国)有限公司∙商务咨询电话:86-025-5186 2240∙商务咨询邮箱:business@ ∙公司网址:TemplateExporter templateExporter = new TemplateExporter();templateExporter.export(outputStream,workBook.execute(parameterMap)) ;通过调用TemplateExporter的exportReport(...)方法, 可以把Report对象以CPT格式保存到外部磁盘文件当中。
可执行代码读取报表模板stuff.cpt,再分别保存为stuff.cpt,stuff.pdf和stuff.xls。
package com.fr.demo;import java.io.File;import java.io.FileOutputStream;import com.fr.base.FRContext;import com.fr.base.dav.LocalEnv;import com.fr.report.WorkBook;import com.fr.report.core.ReportHelper;import com.fr.report.io.ExcelExporter;import com.fr.report.io.PDFExporter;import com.fr.report.io.TemplateExporter;import com.fr.report.io.TemplateImporter;import com.fr.report.io.TextExporter;import com.fr.report.io.WordExporter;/*** 演示如何导入导出模板* @author edgar duan* @version 6.5∙帆软(中国)有限公司∙商务咨询电话:86-025-5186 2240∙商务咨询邮箱:business@ ∙公司网址:*/public class ReportIO {/*** @param args*/public static void main(String[] args) {// 报表运行环境路径, WEB-INF目录所放的位置String envPath = "C:/FineReport6.5/WebReport/WEB-INF";// 设置当前报表运行环境, 报表预览时需要一个运行环境// 没有WEB-INF目录时, 路径设置为null. FRContext.setCurrentEnv(newLocalEnv(null));FRContext.setCurrentEnv(new LocalEnv(envPath));try {// 读取模板File cptFile = new File("D:\\stuff.cpt");TemplateImporter templateImporter = new TemplateImporter();WorkBook workBook =(WorkBook)templateImporter.generate(cptFile);java.util.Map parameterMap = new java.util.HashMap();FileOutputStream outputStream;//生成CPT// 清空公式计算结果ReportHelper.clearFormulaResult(workBook);outputStream = new FileOutputStream(new File("E:\\newtemplate\\stuff.cpt"));TemplateExporter templateExporter = new TemplateExporter();templateExporter.export(outputStream,workBook.execute(parameterMap)) ;//生成PDFReportHelper.clearFormulaResult(workBook);∙帆软(中国)有限公司∙商务咨询电话:86-025-5186 2240∙商务咨询邮箱:business@ ∙公司网址:outputStream = new FileOutputStream(newFile("E:\\newtemplate\\stuff.pdf"));PDFExporter pdfExporter = new PDFExporter();pdfExporter.export(outputStream,workBook.execute(parameterMap)) ;// 生成DOCReportHelper.clearFormulaResult(workBook);outputStream = new FileOutputStream(newFile("E:\\newtemplate\\stuff.doc"));WordExporter wordExporter = new WordExporter();wordExporter.export(outputStream,workBook.execute(parameterMap));// 生成XLSReportHelper.clearFormulaResult(workBook);outputStream = new FileOutputStream(newFile("E:\\newtemplate\\stuff.xls"));ExcelExporter excelExporter = new ExcelExporter();excelExporter.export(outputStream,workBook.execute(parameterMap));//生成TXTReportHelper.clearFormulaResult(workBook);outputStream = new FileOutputStream(newFile("E:\\newtemplate\\stuff.txt"));TextExporter textExporter = new TextExporter();textExporter.export(outputStream,workBook.execute(parameterMap));} catch(Exception e) {e.printStackTrace();}}}∙帆软(中国)有限公司∙商务咨询电话:86-025-5186 2240∙商务咨询邮箱:business@ ∙公司网址:单元格格式设置// 新建一个单元格, 位置为(2, 2), 列宽为2// 行高为 2, 值为文本 "FineReport"CellElement cellElement = new CellElement(2, 2, 2, 2, "FineReport");// 得到CellElement的样式,如果没有新建默认样式Style style = cellElement.getStyle();if(style == null) {style = Style.getInstance();}// 设置字体和前景的颜色FRFont frFont = FRFont.getInstance("Dialog", Font.BOLD, 14);frFont = frFont.applyForeground(new Color(21, 76, 160));style = style.deriveFRFont(frFont);// 设置背景ColorBackground background = ColorBackground.getInstance(new Color(255, 255, 177));style = style.deriveBackground(background);∙帆软(中国)有限公司∙商务咨询电话:86-025-5186 2240∙商务咨询邮箱:business@ ∙公司网址:// 设置水平居中style = style.deriveHorizontalAlignment(Constants.CENTER);// 设置边框style = style.deriveBorder(Constants.LINE_DASH_DOT, Color.red, Constants.L INE_DASH_DOT, Color.yellow, Constants.LINE_DASH_DOT, Color.BLUE, Const ants.LINE_DASH_DOT, Color.CYAN);// 改变单元格的样式cellElement.setStyle(style);改变单元格的格式,应先取出该单元格(CellElement)的格式(Style)。