文件输出的多样性,准确性和稳定性对于我们常用的报表软件来说很重要。报表的输入是指从报表的模板文件(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. .NET设计模式(1):1.1 单例模式(Singleton Pattern)

    概述 单例模式就是保证在整个应用程序的生命周期中,在任何时刻,被指定的类只有一个实例,并为客户程序提供一个获取该实例的全局访问点. 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单 ...

  2. lob结构

    lob是什么? 从网上查了好多资料没找到,最后还是同事给我找到了. lob他是这样解释的:LOB专门存储大型对象数据的,类型text.image这些数据类型的数据就是存储在LOB页面 LOB_DATA ...

  3. Parallel并行编程初步

    Parallel并行编程可以让我们使用极致的使用CPU.并行编程与多线程编程不同,多线程编程无论怎样开启线程,也是在同一个CPU上切换时间片.而并行编程则是多CPU核心同时工作.耗时的CPU计算操作选 ...

  4. [WCF编程]10.操作:流操作

    一.流操作概述 在默认情况下,当客户端调用服务时,服务只有在接收到完整的消息后才会被调用,同样,客户端只有在包含了调用结果的返回消息被完整接受时,才会解除对它的阻塞. 对于数据量小的消息,这种交换模式 ...

  5. ios 学习笔记之控件属性

    1.文本框 设置密码属性:Secure Text Entry 勾选; 设置文本框带清除属性: Clear Button =Is always visible;  默认是不带清除属性:Never app ...

  6. Visual Studio for Mac 简介

    2016-12-13 Hutchinson 微软中国MSDN 在 11 月举行的 Connect(); 上,Microsoft 将推出 Visual Studio for Mac 预览版.这是一个激动 ...

  7. Netty(一)引题

    本文介绍Java BIO(同步阻塞IO),伪异步IO,NIO(非阻塞IO),AIO(异步IO)这四种IO的情况,并对不同IO模型作比较. 目录 1.BIO 2.伪异步IO 3.NIO 4.AIO 5. ...

  8. JS高程3.基本概念(3)

    1.ECMAScript数值的范围 由于内存的限制,在大多数浏览器中,ECMAScript能够拿保存的数据的范围是 5e-324 ~ 1.7976931348623157e+308,其中最小的数值保存 ...

  9. jquery实现导航图轮播

    版权声明:作者原创,转载请注明出处! 下面的几个栗子是使用jquery实现Banner轮播的效果,直接将代码贴出来,从最初级没有任何优化和封装的写法,一直到最后一个栗子,一步步进行了优化,加大程序的可 ...

  10. SharePoint 2013 Word 转换PDF服务介绍及示例

    前言:在SharePoint使用过程中,经常会发现将文档进行格式转换的需求,之前,看到SharePoint 2013有将PPT转换PDF文档的服务,后来,才发现SharePoint 2010开始,就有 ...