| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1147 人关注过本帖
标题:报表开发导出各种格式文件的API
只看楼主 加入收藏
kaqima
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2016-3-15
结帖率:0
收藏
 问题点数:0 回复次数:0 
报表开发导出各种格式文件的API
文件输出的多样性,准确性和稳定性对于我们常用的报表软件来说很重要。报表的输入是指从报表的模板文件(XML格式的)创建WorkBook对象,输出则指将报表保存为各种格式文件,比如Pdf、Excel、Word这种常见的文件格式,比如FineReport还支持cpt、Svg、Csv、Image(包含png、 jpg、gif、 bmp、wbmp)等多种文件格式。

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

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

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

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

2、导出模板文件

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

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

3、导出Excel文件

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

[java] view plain copy 在CODE上查看代码片派生到我的代码片
// 将结果工作薄导出为Excel文件  
            outputStream = new FileOutputStream(new File("E:\\ExcelExport.xls"));  
            ExcelExporter ExcelExport = new ExcelExporter();  
            ExcelExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));  

4、导出Word文件

[java] view plain copy 在CODE上查看代码片派生到我的代码片
// 将结果工作薄导出为Word文件  
            outputStream = new FileOutputStream(new File("E:\\WordExport.doc"));  
            WordExporter WordExport = new WordExporter();  
            WordExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));  
5、导出Pdf文件

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

[java] view plain copy 在CODE上查看代码片派生到我的代码片
// 将结果工作薄导出为Txt文件(txt文件本身不支持表格、图表等,被导出模板一般为明细表)  
            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上查看代码片派生到我的代码片
// 将结果工作薄导出为Csv文件  
            outputStream = new FileOutputStream(new File("E:\\CsvExport.csv"));  
            CSVExporter CsvExport = new CSVExporter();  
            CsvExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));  
8、导出Svg文件

[java] view plain copy 在CODE上查看代码片派生到我的代码片
//将结果工作薄导出为SVG文件   
            outputStream = new FileOutputStream(new File("D:\\SvgExport.svg"));   
            SVGExporter SvgExport = new SVGExporter();   
            SvgExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));  
9、导出Image文件

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

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

[java] view plain copy 在CODE上查看代码片派生到我的代码片
outputStream.close();  
ModuleContext.stopModules();  
例如,一个完整的可执行代码:

[java] view plain copy 在CODE上查看代码片派生到我的代码片
package com.   
   
import    
import    
import com.fr.base.FRContext;   
import com.fr.general.ModuleContext;  
import com.fr.base.Parameter;  
import com.fr.dav.LocalEnv;  
import com.  
import com.  
import com.  
import com.  
import com.  
import com.  
import com.  
import com.  
import com.  
import com.fr.main.impl.WorkBook;  
import com.fr.main.workbook.ResultWorkBook;  
import com.fr.report.module.EngineModule;  
import com.fr.stable.WriteActor;  
  
   
public class ExportApi {   
    public static void main(String[] args) {   
        // 定义报表运行环境,才能执行报表   
        String envpath = "D:\\FineReport_8.0\\WebReport\\WEB-INF";   
        FRContext.setCurrentEnv(new LocalEnv(envpath));   
        ModuleContext.startModule(EngineModule.class.getName());   
        ResultWorkBook rworkbook = null;   
        try {   
            // 未执行模板工作薄   
            WorkBook workbook = (WorkBook) TemplateWorkBookIO   
                    .readTemplateWorkBook(FRContext.getCurrentEnv(),   
                            "\\doc\\Primary\\Parameter\\Parameter.cpt");   
            // 获取报表参数并设置值,导出内置数据集时数据集会根据参数值查询出结果从而转为内置数据集   
            Parameter[] parameters = workbook.getParameters();   
            parameters[0].setValue("华东");   
            // 定义parametermap用于执行报表,将执行后的结果工作薄保存为rworkBook   
            java.util.Map parameterMap = new java.util.HashMap();   
            for (int i = 0; i < parameters.length; i++) {   
                parameterMap.put(parameters[i].getName(), parameters[i]   
                        .getValue());   
            }   
            // 定义输出流   
            FileOutputStream outputStream;   
            // 将未执行模板工作薄导出为内置数据集模板   
            outputStream = new FileOutputStream(new File("D:\\EmbExport.cpt"));   
            EmbeddedTableDataExporter templateExporter = new EmbeddedTableDataExporter();   
            templateExporter.export(outputStream, workbook);   
            // 将模板工作薄导出模板文件,在导出前您可以编辑导入的模板工作薄,可参考报表调用章节   
            outputStream = new FileOutputStream(new File("D:\\TmpExport.cpt"));   
            ((WorkBook) workbook).export(outputStream);  
            // 将结果工作薄导出为2003Excel文件   
            outputStream = new FileOutputStream(new File("D:\\ExcelExport.xls"));   
            ExcelExporter ExcelExport = new ExcelExporter();   
            ExcelExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));     
            // 将结果工作薄导出为Word文件   
            outputStream = new FileOutputStream(new File("D:\\WordExport.doc"));   
            WordExporter WordExport = new WordExporter();   
            WordExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));   
            // 将结果工作薄导出为Pdf文件   
            outputStream = new FileOutputStream(new File("D:\\PdfExport.pdf"));   
            PDFExporter PdfExport = new PDFExporter();   
            PdfExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));   
            // 将结果工作薄导出为Txt文件(txt文件本身不支持表格、图表等,被导出模板一般为明细表)   
            outputStream = new FileOutputStream(new File("D:\\TxtExport.txt"));   
            TextExporter TxtExport = new TextExporter();   
            TxtExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));   
            // 将结果工作薄导出为Csv文件   
            outputStream = new FileOutputStream(new File("D:\\CsvExport.csv"));   
            CSVExporter CsvExport = new CSVExporter();   
            CsvExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));              
            //将结果工作薄导出为SVG文件   
            outputStream = new FileOutputStream(new File("D:\\SvgExport.svg"));   
            SVGExporter SvgExport = new SVGExporter();   
            SvgExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));            
            //将结果工作薄导出为image文件   
            outputStream = new FileOutputStream(new File("D:\\PngExport.png"));   
            ImageExporter ImageExport = new ImageExporter();   
            ImageExport.export(outputStream, workbook.execute(parameterMap,new WriteActor()));                        
            outputStream.close();   
            ModuleContext.stopModules();  
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
}  
编译运行该代码后,就会在E盘下生成不同格式的文件,这样就导出成功了。
搜索更多相关主题的帖子: 稳定性 数据源 Excel 开发 Word 
2016-06-06 16:52
快速回复:报表开发导出各种格式文件的API
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020630 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved