1. 在eclipse中导入第三方包barcode4j-light.jar,并新建一个类UPCCODE,具体代码如下:
package com.fr.report.script.function;
import java.awt.image.BufferedImage;
import org.krysalis.barcode4j.impl.upcean.UPCABean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider; import org.krysalis.barcode4j.tools.UnitConv;
import com.fr.report.script.NormalFunction;
public class UPCCode extends NormalFunction{ public Object run(Object[] args) {
if(args == null || args.length < 1) { return \参数不对,必须有一个参数\; } try {
UPCABean bean = new UPCABean();
final int dpi = Integer.parseInt(args[1].toString());
bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); // makes the narrow
bean.doQuietZone(false);
BitmapCanvasProvider canvas = new BitmapCanvasProvider(dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
// Generate the barcode
bean.generateBarcode(canvas, \ + args[0]); // Signal end of generation canvas.finish(); // return image
return canvas.getBufferedImage(); } catch (Exception e) { e.printStackTrace(); }
return args[0]; } }
2. 将编译后的UPCCODE.class文件放在报表环境目录\\WebReport\\WEB-INF\\lib\\ fr-server-6.5.jar\\com\\fr\\report\\script\\function下
3. 将第三方包barcode4j-light.jar放在报表环境目录\\WebReport\\WEB-INF\\lib文件夹下
4. UPCCODE()函数的使用
重启服务器,在报表中使用公式UPCCODE(num1, num2),num1为需要生成条形码的数值,num2为生成的图片的高度。
如输入公式:=UPCCODE(12345678912,100),预览便可以看到条形码了。
导出api
FineReport提供了强大的输入输出功能,所有的这些输入输出的类都在
com.fr.report.io包里面。Report的输入指从报表的模板文件(XML格式的)创建Report对象,输出指将Report保存为模板文件,FineReport还支持将Report保存为PDF,Excel,Word,SVG,HTML,CSV等文件格式。
读取模板文件 // 读取模板
File cptFile = new File(\);
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(new File(\)); 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 */
public class ReportIO { /**
* @param args */
public static void main(String[] args) {
// 报表运行环境路径, WEB-INF目录所放的位置
String envPath = \ // 设置当前报表运行环境, 报表预览时需要一个运行环境
// 没有WEB-INF目录时, 路径设置为null. FRContext.setCurrentEnv(new LocalEnv(null));
FRContext.setCurrentEnv(new LocalEnv(envPath)); try {
// 读取模板
File cptFile = new File(\
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(\ TemplateExporter templateExporter = new TemplateExporter(); templateExporter.export(outputStream, workBook.execute(parameterMap)) ; //生成 PDF
ReportHelper.clearFormulaResult(workBook); outputStream = new FileOutputStream(new File(\
PDFExporter pdfExporter = new PDFExporter(); pdfExporter.export(outputStream, workBook.execute(parameterMap)) ; // 生成DOC
ReportHelper.clearFormulaResult(workBook); outputStream = new FileOutputStream(new File(\
WordExporter wordExporter = new WordExporter();
wordExporter.export(outputStream,workBook.execute(parameterMap));
// 生成XLS
ReportHelper.clearFormulaResult(workBook); outputStream = new FileOutputStream(new File(\
ExcelExporter excelExporter = new ExcelExporter();
excelExporter.export(outputStream,workBook.execute(parameterMap)); //生成 TXT
ReportHelper.clearFormulaResult(workBook); outputStream = new FileOutputStream(new File(\
TextExporter textExporter = new TextExporter(); textExporter.export(outputStream,workBook.execute(parameterMap)); } catch(Exception e) { e.printStackTrace();
} } }
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库FineReport报表软件API源代码之程序数据集、自定义函数和导出API(4)在线全文阅读。
相关推荐: