1、 / 读取模板 File cptFile = new File(D:stuff.cpt); TemplateImporter templateImporter = new TemplateImporter(); WorkBook workBook = (WorkBook)templateImporter.generate(cptFile); Stuff.cpt是用报表设计器生成的模板文件。只需要用建立一个TemplateImporter对象, 然后调用它的generateReport()方法来产生一个Report对象,同时可以将产生的Report对象强制转换成WorkSheet或者GroupR
2、eport。保存成模板文件/ CPT / 清空公式计算结果 E:newtemplatestuff.cpt这个是导出后新文档生成的地址 ReportHelper.clearFormulaResult(workBook);outputStream = new FileOutputStream(new File(E:newtemplatestuff.cpt); TemplateExporter templateExporter = new TemplateExporter(); templateExporter.export(outputStream,workBook.execute(paramet
3、erMap) ;通过调用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;imp
4、ort 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 dua
5、n * version 6.5 */ 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(new LocalEnv(null); FRContext.setCurrentEnv(new Lo
6、calEnv(envPath); try java.util.Map parameterMap = new java.util.HashMap(); FileOutputStream outputStream; /生成 CPT ReportHelper.clearFormulaResult(workBook); outputStream = new FileOutputStream(new File( templateExporter.export(outputStream, workBook.execute(parameterMap) ; /生成 PDF newtemplatestuff.p
7、df PDFExporter pdfExporter = new PDFExporter(); pdfExporter.export(outputStream, workBook.execute(parameterMap) ; / 生成DOC newtemplatestuff.doc WordExporter wordExporter = new WordExporter(); wordExporter.export(outputStream,workBook.execute(parameterMap); / 生成XLS newtemplatestuff.xls ExcelExporter e
8、xcelExporter = new ExcelExporter(); excelExporter.export(outputStream,workBook.execute(parameterMap); /生成 TXT newtemplatestuff.txt TextExporter textExporter = new TextExporter(); textExporter.export(outputStream,workBook.execute(parameterMap); catch(Exception e) e.printStackTrace(); 单元格格式设置/新建一个单元格,
9、位置为(2,2),列宽为2 行高为2,值为文本FineReportCellElementcellElement=newCellElement(2,得到CellElement的样式,如果没有新建默认样式 StylestylecellElement.getStyle();if(style=null) Style.getInstance(); 设置字体和前景的颜色 FRFontfrFontFRFont.getInstance(Dialog,Font.BOLD,14);frFont.applyForeground(newColor(21,76,160);style.deriveFRFont(frFon
10、t);设置背景 ColorBackgroundbackgroundColorBackground.getInstance(newColor(255,255,177);style.deriveBackground(background);设置水平居中 style.deriveHorizontalAlignment(Constants.CENTER);设置边框 style.deriveBorder(Constants.LINE_DASH_DOT,Color.red,Constants.LINE_DASH_DOT,Color.yellow,Color.BLUE,Color.CYAN);改变单元格的样
11、式 cellElement.setStyle(style);改变单元格的格式,应先取出该单元格(CellElement)的格式(Style)。在默认的情况下Style是null,故当取出Style后应先判断其值是否为null,如果这个值为空,则需先新建一个Style,然后再将该值赋给CellElement。最后根据Style和FRFont中的方法进一步地设置该单元格的各种属性。可执行代码如下:import java.awt.Color;import java.awt.Font;import com.fr.base.Constants;import com.fr.base.FRFont;impo
12、rt com.fr.base.Style;import com.fr.base.background.ColorBackground;import com.fr.report.CellElement;import com.fr.report.DefaultCellElement;import com.fr.report.WorkBookTemplate;import com.fr.report.WorkSheet;import com.fr.web.Reportlet;import com.fr.web.ReportletRequest; * ReportletFromTemplateDemo
13、. * author marks * 设置单元格格式 public class CellElementStyleDemo implements Reportlet public CellElementStyleDemo() /建立新报表需要传参(ReportletRequest reportletRequest) public WorkBookTemplate createReport(ReportletRequest reportletRequest) /new a WorkSheet WorkBook wb = new WorkBook(); WorkSheet workSheet = n
14、ew WorkSheet(); /new a cellElement with String CellElement cellElement1 = new DefaultCellElement(1, 1, /add the cellElement to the workSheet workSheet.addCellElement(cellElement1); /set the columnWidth of 1 to 200px,rowHeight of 1 to 30px workSheet.setColumnWidth(1, 200); workSheet.setRowHeight(1, 3
15、0); /set the font of cellElement1 Style style = Style.getInstance(); FRFont frFont = FRFont.getInstance(Tahoma, Font.BOLD | Font.ITALIC, 20); frFont = frFont.applyForeground(Color.red); style = style.deriveFRFont(frFont); / 设置背景 ColorBackground background = ColorBackground.getInstance(new Color(255,
16、 255, 177); style = style.deriveBackground(background); / 设置水平居中 style = style.deriveHorizontalAlignment(Constants.CENTER); / 设置边框 style = style.deriveBorder(Constants.LINE_DASH_DOT, Color.red, Constants.LINE_DASH_DOT, Color.yellow, Constants.LINE_DASH_DOT, Color.BLUE, Constants.LINE_DASH_DOT, Color
17、.CYAN); cellElement1.setStyle(style); /set the columnWidth of 4 to 200px,rowHeight of 5 to 50px workSheet.setColumnWidth(4, 200); wb.addReport(workSheet); return wb;结果如下图所示:将模板通过指定打印机打印用户在使用FineReport进行二次开发的过程中有时候需要对不同文件通过不同的打印机打印出来,比如A打印机只能打印A.TXT; B打印机只能打印b.txt.。得到当前报表运行环境 / 设置当前报表运行环境, 报表预览时需要一个运
18、行环境WorkBookTemplate wbTpl = FRContext.getCurrentEnv().readTemplate(stuff.cpt指定打印机打印PrintUtils.print(wbTpl.execute(null).generateReportPageSet().getTraversingCase(), false, Smart Printimport com.fr.base.print.PrintUtils;/* * 演示如何将模板通过指定打印机打印 * * author edgar duan * version 6.5 */public class PrintDem
19、o public static void main(String args) / 报表运行环境路径, WEB-INF目录所放的位置 WorkBookTemplate wbTpl = FRContext.getCurrentEnv().readTemplate( /null 为模板需要传的参数,false为是否弹出提示窗口,Smart Print 为打印机名称 PrintUtils.print(wbTpl.execute(null).generateReportPageSet().getTraversingCase(), false, 自定义函数FineReport已经提供了大量的自带函数,在正
20、常情况下足够满足用户的报表制作需求,但是在一些特殊领域,可能需要一些特殊的函数,在这种情况下,FineReport提供了自定义函数机制,可以由用户根据业务需要自己来定义一些函数,但这些函数必须满足FineReport函数定义规则。FineReport函数定义规则:Functionname(Para,Para,.),其中Functionname为函数名,Para为参数。函数原理在FineReport中,每一个函数都被定义成一个类,这个类必须要实现Function这个接口,在运算的时候首先通过函数名反射取得这个类,然后调用它的run(Object agrs)方法。下面以SUM这个函数为例。/* *
21、 Copyright(c) 2001-2008, FineReport Inc, All Rights Reserved.package com.fr.report.script.function;import com.fr.report.script.NormalFunction;import com.fr.report.script.core.FArray;import com.fr.report.script.core.FunctionHelper; * Function.public class SUM extends NormalFunction /* * Run the funct
22、ion on the stack. Pops the arguments from the stack, * then return the result. public Object run(Object args) double result = 0; for(int i = 0; i args.length; i+) /p:需要先判断是否为null. if(argsi = null) continue; result += parseObject(argsi); return FunctionHelper.parsePrimitiveDouble(result); /* * 把Object转成double类型的值 private double parseObject(Object obj) if (obj instanceof Number) return (Number)obj).doubleValue(); else if (obj instanceof Bool
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1