导出API

目录:

1.描述

FineReport提供了强大的输入输出功能,所有的这些输入输出的类都在com.fr.report.io包里面。

报表的输入指从报表的模板文件(XML格式的)创建WorkBook对象,在报表调用章节已经介绍过。

输出则指将报表保存为各种格式文件,FineReport支持将报表保存为cpt、内置数据cpt、Pdf、Excel、Word、Svg、Csv、Image(包含png、 jpg、gif、 bmp)等多种文件格式,释放导出进程。

2.实现原理

我们以Parameter.cpt模板作为被导出模板,参数为报表参数,且设置默认值为华东
2.1 导出成内置数据集模板
导出成内置数据集模板,顾名思义是将原模板的数据源根据参数条件查询出结果并转为内置数据集,然后把模板导出,不需要对原模板进行计算(数据列扩展、公式计算等)。
  1. // 将未执行模板工作薄导出为内置数据集模板  
  2.             outputStream = new FileOutputStream(new File("E:\\EmbExport.cpt"));  
  3.             EmbeddedTableDataExporter templateExporter = new EmbeddedTableDataExporter();  
  4.             templateExporter.export(outputStream, workbook);  
2.2 导出模板文件
我们可以将原模板通过程序编辑后再次导出为模板文件,或者将某一路径下的模板保存至另一路径下。
  1. // 将模板工作薄导出模板文件,在导出前您可以编辑导入的模板工作薄,可参考报表调用章节  
  2.             outputStream = new FileOutputStream(new File("E:\\TmpExport.cpt"));  
  3.             ((WorkBook) workbook).export(outputStream);  
2.3 导出Excel文件
模板工作薄WorkBook执行后为结果工作薄ResultWorkBook,我们可以把计算后的结果导出成Excel文件。
  1. // 将结果工作薄导出为Excel文件  
  2.             outputStream = new FileOutputStream(new File("E:\\ExcelExport.xls"));  
  3.             ExcelExporter ExcelExport = new ExcelExporter();  
  4.             ExcelExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));  
注:导出Excel的其他格式可以参考文档:Excel导出的多种方式章节
注:如果需要导出2007版excel,需要将ExcelExporter ExcelExport = new ExcelExporter()改成StreamExcel2007Exporter ExcelExport = new StreamExcel2007Exporter();,具体代码如下:
  1. // 将结果工作薄导出为Excel文件    
  2.             outputStream = new FileOutputStream(new File("D:\\ExcelExport.xlsx"));    
  3.             StreamExcel2007Exporter ExcelExport1 = new StreamExcel2007Exporter();    
  4.             ExcelExport1.export(outputStream, workbook.execute(parameterMap,new WriteActor()));  
2.4 导出Word文件 
  1. // 将结果工作薄导出为Word文件  
  2.             outputStream = new FileOutputStream(new File("E:\\WordExport.doc"));  
  3.             WordExporter WordExport = new WordExporter();  
  4.             WordExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));  
注:FineReport报表导出Word不支持导出悬浮元素,因此若您需导出的模板中包含有悬浮元素如图表,请将其改为单元格元素。
2.5 导出Pdf文件
  1. // 将结果工作薄导出为Pdf文件   
  2.             outputStream = new FileOutputStream(new File("E:\\PdfExport.pdf"));   
  3.             PDFExporterProcessor PdfExport = PDFExporterFactory.getPDFExporter();  
  4.             PdfExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));  
注:2016年12月30日以后的jar,原先使用new PDFExportor()接口已经淘汰 ,任然可以兼容使用,安装性能优化插件后也兼容使用PDFExportor(),但是无法达到提升性能的效果,若想提升性能需要更换新接口。
另:新的接口为PDFExporterFactory.getPDFExporter(boolean isPrint) 和PDFExporterFactory.getPDFExporter() isPrint默认为false
2.6 导出Txt文件
  1. // 将结果工作薄导出为Txt文件(txt文件本身不支持表格、图表等,被导出模板一般为明细表)  
  2.             outputStream = new FileOutputStream(new File("E:\\TxtExport.txt"));  
  3.             TextExporter TxtExport = new TextExporter();  
  4.             TxtExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));  
2.7 导出Csv文件
  1. // 将结果工作薄导出为Csv文件  
  2.             outputStream = new FileOutputStream(new File("E:\\CsvExport.csv"));  
  3.             CSVExporter CsvExport = new CSVExporter();  
  4.             CsvExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));  
2.8 导出Svg文件
  1. //将结果工作薄导出为SVG文件    
  2.             outputStream = new FileOutputStream(new File("D:\\SvgExport.svg"));    
  3.             SVGExporter SvgExport = new SVGExporter();    
  4.             SvgExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));  
2.9 导出Image文件
  1. //将结果工作薄导出为image文件      
  2. outputStream = new FileOutputStream(new File("D:\\PngExport.png"));    
  3. ImageExporter ImageExport = new ImageExporter();    
  4. ImageExport.setSuffix("png");  
  5. ImageExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));      
注:Image图片支持png, jpg, gif, bmp格式,只需要修改ImageExport.setSuffix("png");中的后缀,这里示例是png格式,默认为jpg
2.10 释放进程
通过导出API在后台导出excel等文件,会产生很多进程,通过下面的方案释放进程。
在导出完成之后添加下面代码:
  1. outputStream.close();  
  2. ModuleContext.stopModules();  

3.示例

3.1 完整可执行代码
  1. package com.fr.io;    
  2.     
  3. import java.io.File;    
  4. import java.io.FileOutputStream;    
  5.   
  6. import com.fr.base.FRContext;   
  7. import com.fr.general.ModuleContext;  
  8. import com.fr.base.Parameter;  
  9. import com.fr.dav.LocalEnv;  
  10. import com.fr.io.exporter.CSVExporter;  
  11. import com.fr.io.exporter.EmbeddedTableDataExporter;  
  12. import com.fr.io.exporter.ExcelExporter;  
  13. import com.fr.io.exporter.PDFExporter;  
  14. import com.fr.io.exporter.PDFExporterProcessor;  
  15. import com.fr.io.exporter.TextExporter;  
  16. import com.fr.io.exporter.WordExporter;  
  17. import com.fr.io.exporter.SVGExporter;  
  18. import com.fr.io.exporter.ImageExporter;  
  19. import com.fr.io.exporter.excel.stream.StreamExcel2007Exporter;  
  20. import com.fr.main.impl.WorkBook;  
  21. import com.fr.main.workbook.ResultWorkBook;  
  22. import com.fr.report.module.EngineModule;  
  23. import com.fr.stable.WriteActor;  
  24. import com.fr.web.core.reserve.PDFExporterFactory;  
  25.   
  26.     
  27. public class ExportApi {    
  28.     public static void main(String[] args) {    
  29.         // 定义报表运行环境,才能执行报表    
  30.         String envpath = "E:\\FineReport_9.0\\WebReport\\WEB-INF";    
  31.         FRContext.setCurrentEnv(new LocalEnv(envpath));    
  32.         ModuleContext.startModule(EngineModule.class.getName());   
  33.         ResultWorkBook rworkbook = null;    
  34.         try {    
  35.             // 未执行模板工作薄    
  36.             WorkBook workbook = (WorkBook) TemplateWorkBookIO    
  37.                     .readTemplateWorkBook(FRContext.getCurrentEnv(),    
  38.                             "\\doc\\Primary\\Parameter\\Parameter.cpt");    
  39.             // 获取报表参数并设置值,导出内置数据集时数据集会根据参数值查询出结果从而转为内置数据集    
  40.             Parameter[] parameters = workbook.getParameters();    
  41.             parameters[0].setValue("华东");    
  42.             // 定义parametermap用于执行报表,将执行后的结果工作薄保存为rworkBook    
  43.             java.util.Map parameterMap = new java.util.HashMap();    
  44.             for (int i = 0; i < parameters.length; i++) {    
  45.                 parameterMap.put(parameters[i].getName(), parameters[i]    
  46.                         .getValue());    
  47.             }    
  48.             // 定义输出流    
  49.             FileOutputStream outputStream;    
  50.             // 将未执行模板工作薄导出为内置数据集模板      
  51.             outputStream = new FileOutputStream(new File("D:\\EmbExport.cpt"));      
  52.             EmbeddedTableDataExporter templateExporter = new EmbeddedTableDataExporter();      
  53.             templateExporter.export(outputStream, workbook);      
  54.             // 将模板工作薄导出模板文件,在导出前您可以编辑导入的模板工作薄,可参考报表调用章节      
  55.             outputStream = new FileOutputStream(new File("D:\\TmpExport.cpt"));      
  56.             ((WorkBook) workbook).export(outputStream);    
  57.             // 将结果工作薄导出为2003Excel文件      
  58.             outputStream = new FileOutputStream(new File("D:\\ExcelExport.xls"));      
  59.             ExcelExporter ExcelExport = new ExcelExporter();      
  60.             ExcelExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));      
  61.             // 将结果工作薄导出为Excel文件      
  62.             outputStream = new FileOutputStream(new File("D:\\ExcelExport.xlsx"));      
  63.             StreamExcel2007Exporter ExcelExport1 = new StreamExcel2007Exporter();      
  64.             ExcelExport1.export(outputStream, workbook.execute(parameterMap,new WriteActor()));    
  65.             // 将结果工作薄导出为Word文件      
  66.             outputStream = new FileOutputStream(new File("D:\\WordExport.doc"));      
  67.             WordExporter WordExport = new WordExporter();      
  68.             WordExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));      
  69.             // 将结果工作薄导出为Pdf文件     
  70.             outputStream = new FileOutputStream(new File("D:\\PdfExport.pdf"));     
  71.             PDFExporterProcessor PdfExport = PDFExporterFactory.getPDFExporter();    
  72.             PdfExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));    
  73.             // 将结果工作薄导出为Txt文件(txt文件本身不支持表格、图表等,被导出模板一般为明细表)      
  74.             outputStream = new FileOutputStream(new File("D:\\TxtExport.txt"));      
  75.             TextExporter TxtExport = new TextExporter();      
  76.             TxtExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));      
  77.             // 将结果工作薄导出为Csv文件      
  78.             outputStream = new FileOutputStream(new File("D:\\CsvExport.csv"));      
  79.             CSVExporter CsvExport = new CSVExporter();      
  80.             CsvExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));                
  81.             //将结果工作薄导出为SVG文件      
  82.             outputStream = new FileOutputStream(new File("D:\\SvgExport.svg"));      
  83.             SVGExporter SvgExport = new SVGExporter();      
  84.             SvgExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));               
  85.             //将结果工作薄导出为image文件      
  86.             outputStream = new FileOutputStream(new File("D:\\PngExport.png"));    
  87.             ImageExporter ImageExport = new ImageExporter();    
  88.             ImageExport.setSuffix("png");  
  89.             ImageExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));                        
  90.             outputStream.close();    
  91.             ModuleContext.stopModules();  
  92.         } catch (Exception e) {    
  93.             e.printStackTrace();    
  94.         }    
  95.     }  
  96. }  
注:在使用API导出不同类型文件时,除了需要导入FineReport的fr-third-9.0.jar和fr-report-9.0.jar和fr-core-9.0.jar包之外,还需要导入%FR_HOME%\WebReport\WEB-INF\lib下面的sqlite-jdbc.jar和tomcat\lib下面的serverlet-api.jar。

编译运行该代码后,就会在D盘下生成不同格式的文件,这样就导出成功了,如下图:
4.聚合报表导出

注:由于聚合报表不支持WriteActor(),因此需要换成PageActor()

聚合报表导出,只需要将WriteActor()换成PageActor(),即可实现输出各种格式的文件。

导出pdf,具体代码如下:

  1. // 将结果工作薄导出为Pdf文件    
  2.             outputStream = new FileOutputStream(new File("D:\\PdfExport.pdf"));    
  3.             PDFExporterProcessor PdfExport = PDFExporterFactory.getPDFExporter();  
  4.             PdfExport.export(outputStream, workbook.execute(parameterMap,new PageActor()));   

同时,需要在头部引入PageActor(),具体代码如下:

  1. import com.fr.stable.PageActor;  

5.决策报表导出

决策报表导出,结果工作簿改用以下方法 即可:

  1. ResultWorkBook re = FSFormletHandler.executeForm("xxxx.frm"new HashMap<String, Object>());  

同时,需要在头部引入FSFormletHandler,具体代码如下:

  1. import com.fr.fs.web.FSFormletHandler;  

5.1完整示例代码如下

  1. package com.fr.io;     
  2. import java.io.File;    
  3. import java.io.FileOutputStream;    
  4. import com.fr.base.FRContext;   
  5. import com.fr.general.ModuleContext;  
  6. import com.fr.dav.LocalEnv;  
  7. import com.fr.fs.web.FSFormletHandler;  
  8. import com.fr.io.exporter.PDFExporterProcessor;  
  9. import com.fr.io.exporter.ImageExporter;  
  10. import com.fr.main.workbook.ResultWorkBook;  
  11. import com.fr.report.module.EngineModule;  
  12. import com.fr.web.core.reserve.PDFExporterFactory;  
  13.   
  14.     
  15. public class ExportApi {    
  16.     public static void main(String[] args) {    
  17.         // 定义报表运行环境,才能执行报表    
  18.         String envpath = "E:\\FineReport_9.0\\WebReport\\WEB-INF";    
  19.         FRContext.setCurrentEnv(new LocalEnv(envpath));    
  20.         ModuleContext.startModule(EngineModule.class.getName());          
  21.         try {    
  22.             java.util.Map paraMap = new java.util.HashMap();      
  23.             paraMap.put("company""VINET");  
  24.             ResultWorkBook re = FSFormletHandler.executeForm("doc\\frm\\example.frm",paraMap);              
  25.             FileOutputStream outputStream;     
  26.             // 将结果工作薄导出为Pdf文件   
  27.             outputStream = new FileOutputStream(new File("D:\\example.pdf"));   
  28.             PDFExporterProcessor PdfExport = PDFExporterFactory.getPDFExporter();  
  29.             PdfExport.export(outputStream,re);           
  30.             //将结果工作薄导出为image文件    
  31.             outputStream = new FileOutputStream(new File("D:\\example.png"));    
  32.             ImageExporter ImageExport = new ImageExporter();    
  33.             ImageExport.setSuffix("png");  
  34.             ImageExport.export(outputStream,re);                        
  35.             outputStream.close();    
  36.             ModuleContext.stopModules();  
  37.         } catch (Exception e) {    
  38.             e.printStackTrace();  
  39.         }  
  40.     }  
  41. }  

6.错误代码:1120出现原因及解决方法

运行编译好的代码时,会弹出错误提示:错误代码:1120 当前Hsql数据库已被另一线程锁定。

原因:导出的时候使用了运行环境,所以会把db.lck给死锁,导出一次后再次导出就会报错。

解决方法:

方法一:关闭eclipse进程,%FR_HOME%\WebReport\WEB-INF\logdb下的db.lck文件删除,再次运行eclipse即可。

注:该方法在运行一次后仍会报错,需要反复删除db.lck文件。

方法二:首先迁移logdb,具体可以参考平台数据迁移

其次修改%FR_HOME%\WebReport\WEB-INF\logdb下的db.properties文件,将hsqldb.lock_file=true修改为false,如下图所示:
222
之后就可以做到无限次导出了。



索引:

Excel导出的多种方式

后台批量导出Excel

Excel直接转成模板cpt

多个报表导入一个Excel

附件列表


主题:
标签: 已验证 9.0验证

文档内容仅供参考,如果你需要获取更多帮助,付费/准付费客户请咨询帆软技术支持
关于技术问题,您还可以前往帆软社区,点击顶部搜索框旁边的提问按钮
若您还有其他非技术类问题,可以联系帆软传说哥(qq:1745114201