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

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

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

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

  1. // 将未执行模板工作薄导出为内置数据集模板
  2. outputStream = new FileOutputStream(new File("E:\\EmbExport.cpt"));
  3. EmbeddedTableDataExporter templateExporter = new EmbeddedTableDataExporter();
  4. templateExporter.export(outputStream, workbook);

2、导出模板文件

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

  1. // 将模板工作薄导出模板文件,在导出前您可以编辑导入的模板工作薄,可参考报表调用章节
  2. outputStream = new FileOutputStream(new File("E:\\TmpExport.cpt"));
  3. ((WorkBook) workbook).export(outputStream);

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()));

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()));

5、导出Pdf文件

  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文件

  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文件

  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文件

  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文件

  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等文件,会产生很多进程,通过下面的方案释放进程。在导出完成之后添加下面代码:

  1. outputStream.close();
  2. ModuleContext.stopModules();

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

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

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

报表开发导出各种格式文件的API的更多相关文章

  1. 导出CSV格式文件,用Excel打开乱码的解决办法

    导出CSV格式文件,用Excel打开乱码的解决办法 1.治标不治本的办法 将导出CSV数据文件用记事本打开,然后另存为"ANSI"编码格式,再用Excel打开,乱码解决. 但是,这 ...

  2. Powerdesigner 导出Excel格式数据字典 导出Excel格式文件

    版权声明:本文为博主原创文章,转载请注明出处; 网上我也看到了很多的Powerdesigner 导出方法,因为Powerdesigner 提供了部分VBA功能,所以让我用代码导出Excel格式文件得以 ...

  3. 使用PHPExcel导入导出excel格式文件

    使用PHPExcel导入导出excel格式文件  作者:zccst  因为导出使用较多,以下是导出实现过程.  第一步,将PHPExcel的源码拷贝到项目的lib下  文件包含:PHPExcel.ph ...

  4. java导出csv格式文件

    导出csv格式文件的本质是导出以逗号为分隔的文本数据 import java.io.BufferedWriter; import java.io.File; import java.io.FileIn ...

  5. asp.net NPOI导出xlsx格式文件,打开文件报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”

    NPOI导出xlsx格式文件,会出现如下情况: 点击“是”: 导出代码如下: /// <summary> /// 将datatable数据写入excel并下载 /// </summa ...

  6. JSP页面导出PDF格式文件

    JSP页面导出PDF格式文件基本在前端页面可以全部完成 <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/ ...

  7. confluence导出PDF格式文件不显示中文解决

    由于confluence导出PDF格式文件需要应用字体文件,下载字体文件在confluence管理员界面安装即可. 我这里使用从simhei.ttf楷体,可以从windowns主机里下载(c:/win ...

  8. OAF_文件系列2_实现OAF导出CSV格式文件ExportButton(案例)

    20150727 Created By BaoXinjian

  9. java导出json格式文件

    生成json文件代码: import java.io.File; import java.io.FileWriter; import java.io.Writer; public class Crea ...

随机推荐

  1. [Q&A] 远程过程调用失败。[0x800706be]

    由于先安装 SQL Server R2,后又安装 Visual Studio,导致 VS 中的 SQL Sever 版本和 SQL Server R2 版本冲突造成实例出错 打开"开始&qu ...

  2. C# 删除字符串中的中文

    /// <summary> /// 删除字符串中的中文 /// </summary> public static string Delete中文(string str) { s ...

  3. 在CentOS 6.5上安装python2.7

    1.yum groupinstall “Development tools” 2.安装编译Python需要的组件 yum install zlib-devel bzip2-devel openssl- ...

  4. ubuntu入门

    Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音.了解发音是有意义的,您不是第一个为此困惑的人,当然,也不会是最后一个:) 大多数的美国人读 ubun ...

  5. 【C语言学习趣事】_33_关于C语言和C++语言中的取余数(求模)的计算_有符号和无符号数的相互转换问题

    最近再次复习C++语言,用的教材是<C++ Primer>这本教材, 看到第二章的时候,里面有个问题困扰了我. 于是想上网查查怎么回事, 结果看了很久都没有得到一个满意的答案. 书上有这么 ...

  6. 安卓模拟器genymotion连接eclipse成功但是不显示其中项目

    安卓模拟器困了我两三天了,原装模拟器比较慢,忍受不了,查到安卓模拟器的神器——genymotion 按照网上的步骤一步步都安装完毕,最后打开后发现,genymotion界面里面没有找到新建的工程, 这 ...

  7. java实现双链表(差点没写吐系列...)

    刚才把单链表写完了,现在又把双链表写了,双链表和单链表的区别就是每个节点有prior和next两个指针,不同于单链表的一个next指针,而且,正是因为有这两个指针,所以双链表可以前后两个方向去移动指针 ...

  8. Logging configuration

    The Play logger is built on Log4j. Since most Java libraries use Log4j or a wrapper able to use Log4 ...

  9. 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(六)地图搜索模块

    config.xml文件的配置如下: <widget label="地图搜索" icon="assets/images/emergency_resource_ove ...

  10. Android开发3:Intent、Bundle的使用和ListView的应用 、RelativeLayout(相对布局)简述(简单通讯录的实现)

    前言 啦啦啦~博主又来骚扰大家啦~大家是不是感觉上次的Android开发博文有点长呢~主要是因为博主也是小白,在做实验的过程中查询了很多很多概念,努力去理解每一个知识点,才完成了最终的实验.还有就是随 ...