报表开发导出各种格式文件的API

文件输出的多样性,准确性和稳定性对于我们常用的报表软件来说很重要。报表的输入是指从报表的模板文件(XML格式的)创建WorkBook对象,输出则指将报表保存为各种格式文件,比如Pdf、Excel、Word这种常见的文件格式,比如FineReport还支持cpt、Svg、Csv、Image(包含png、 jpg、gif、 bmp、wbmp)等多种文件格式。

         因为常常会碰到报表的开发工作,这里总结了几种格式文件导出的API。

1、导出成内置数据集模板

导出成内置数据集模板,就是将原模板的数据源根据参数条件查询出结果并转为内置数据集,然后把模板导出,不需要对原模板进行计算(数据列扩展、公式计算等)。

 

[java]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 将未执行模板工作薄导出为内置数据集模板  
  2.             outputStream = new FileOutputStream(new File("E:\\EmbExport.cpt"));  
  3.             EmbeddedTableDataExporter templateExporter = new EmbeddedTableDataExporter();  
  4.             templateExporter.export(outputStream, workbook);  

 

2、导出模板文件

我们可以将原模板通过程序编辑后再次导出为模板文件,或者将某一路径下的模板保存至另一路径下。

[java]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 将模板工作薄导出模板文件,在导出前您可以编辑导入的模板工作薄,可参考报表调用章节  
  2.             outputStream = new FileOutputStream(new File("E:\\TmpExport.cpt"));  
  3.             ((WorkBook) workbook).export(outputStream);  

3、导出Excel文件

模板工作薄WorkBook执行后为结果工作薄ResultWorkBook,我们可以把计算后的结果导出成Excel文件。

[java]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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()));  

4、导出Word文件

[java]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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()));  

5、导出Pdf文件

 

[java]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 将结果工作薄导出为Pdf文件  
  2.                             outputStream = new FileOutputStream(newFile("E:\\PdfExport.pdf"));  
  3.                             PDFExporter PdfExport = newPDFExporter();  
  4.                             PdfExport.export(outputStream,workbook.execute(parameterMap,new WriteActor()));  

 

6、导出Txt文件

[java]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 将结果工作薄导出为Txt文件(txt文件本身不支持表格、图表等,被导出模板一般为明细表)  
  2.             outputStream = new FileOutputStream(new File("E:\\TxtExport.txt"));<pre code_snippet_id="1709587" snippet_file_name="blog_20160606_6_1825679" name="code" class="java"></pre>  

7、导出Csv文件

 

[java]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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()));  

 

8、导出Svg文件

[java]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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()));  

9、导出Image文件

[java]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //将结果工作薄导出为image文件    
  2.             outputStream = new FileOutputStream(new File("D:\\PngExport.png"));    
  3.             ImageExporter ImageExport = new ImageExporter();    
  4.             ImageExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));  

10、释放进程

通过导出API在后台导出excel等文件,会产生很多进程,通过下面的方案释放进程。在导出完成之后添加下面代码:

[java]   view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. outputStream.close();  
  2. ModuleContext.stopModules();  

例如,一个完整的可执行代码:

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

编译运行该代码后,就会在E盘下生成不同格式的文件,这样就导出成功了。

猜你喜欢

转载自teacherzhang.iteye.com/blog/2303342