ITPub博客

首页 > 应用开发 > > 把报表插入 Word 文档(api)

把报表插入 Word 文档(api)

作者:u_509a2a8fdb46 时间:2019-10-08 13:43:42 0 删除 编辑

润乾报表制作完成后,不仅能实现展现及导出等功能,还能够根据用户需求, 通过指定模版文件中的书签名称确定插入位置,然后将报表、 图片、 文本内容插入到新的 Word 文件中。

本文主要介绍润乾报表插入 word 文档的具体步骤。

首先介绍下功能原理:

1、 建立 word 模版,在需要插入润乾报表的位置定义“书签”;

2、 Api 根据 word 书签位置,插入计算后的报表对象;

3、 输出根据模版生成的 word 文件。

具体实现过程及相关代码:

1、 建立 word 模版

2、 Api 计算报表,并通过 DocxChanger 类将报表结果插入指定书签,输出 word 结果

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileOutputStream;

importcom.raqsoft.dm.Sequence;

importcom.raqsoft.report.model.ReportDefine;

importcom.raqsoft.report.usermodel.Context;

importcom.raqsoft.report.usermodel.Engine;

importcom.raqsoft.report.usermodel.IReport;

importcom.raqsoft.report.util.ReportUtils;

importcom.raqsoft.report.view.oxml.word.DocxChanger;

public****classInsertWord {

 public staticvoidmain(String[] args) {

 try{

            // 设置报表授权文件

File flic = newFile(“D:/test.lic”);

FileInputStream lis = newFileInputStream(flic);

Sequence. readLicense( Sequence. P_RPT, lis);

File f = newFile(“D:/test.docx”); // 模板文件,注意仅仅是模版,不会被修改

File of = newFile(“D:/out.docx”); // 输出文件:最终根据模版生成的结果 word 文件。

 if(of.exists()) {

                of.delete();

            }

FileOutputStream fos = newFileOutputStream(of);

DocxChanger dc = newDocxChanger(f, fos); // 实例化 DocxChanger

File f3 = newFile(“D:/a.rpx”);

FileInputStream fis = newFileInputStream(f3);

IReport report = ReportUtils. read(fis);

            fis.close();

             // 构建报表运行上下文环境及引擎,并计算报表

Context context = newContext();

Engine engine = newEngine((ReportDefine) report, context);

            report = engine.calc();

             // 将报表结果指定插入 report1 书签处,注意名字和 word 模版内书签名一致

            dc.insertReport(“report1”, report);  

            // 执行所有修改动作,然后关闭输出文件流

            dc.execute();

            fos.close();

} catch(Throwable x) {

            x.printStackTrace();

        }

    }

}

3、 对比验证结果

(1)Word 模版(执行完 api 后,没有改变):

(2)报表实际运行结果

(3)输出的 word 结果文件(根据上面代码为 out.docx)

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69927062/viewspace-3140970/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-08

  • 博文量
    210
  • 访问量
    38