背景说明:
报表制作完成后,不仅能实现展现及导出等功能,还能够根据用户需求,通过指定模板文件中的书签名称确定插入位置,然后将报表、图片、文本内容插入到新的Word文件中。根据实际业务需求,提供了如下两种方式以供参考。
单个书签内容一一插入Word
try {
//设置报表授权文件
String fraq = "D:/report.xml";
ExtCellSet.readLicense(fraq);
File f = new File("E:/test.docx"); // 模板文件
File of = new File("D:/out.docx"); // 输出文件
… …
FileOutputStream fos = new FileOutputStream(of);
DocxChanger dc = new DocxChanger(f, fos); //实例化DocxChanger
//替换文字
dc.replaceText("标题文字", "润乾报表", "楷体", 16f);
//书签处插入文字
dc.insertText("a", "Hello world!", "黑体", 20f);
//书签处插入图片文件
File f1 = new File("D:/test.jpg");
dc.insertImageDirectly("b", f1);
//书签处插入图片文件,自适应单元格大小
dc.insertImageAdjustCell("b1", f1);
//书签处插入图片文件,按照给定的厘米参数设定宽高
dc.insertImageWithWH("b2", f1, 5, 3);
//书签处插入Image对象
BufferedImage bi = new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.setBackground(……);
… …
dc.insertImageDirectly("c", bi);
//书签处插入Image对象,自适应单元格大小
dc.insertImageAdjustCell("c1", bi);
//书签处插入Image对象,按照给定的厘米参数设定宽高
dc.insertImageWithWH("c2", bi, 8, 6);
//书签处插入报表
File f2 = new File("D:/api/wangge.rpx");
FileInputStream fis =new FileInputStream(f2);
IReport report = ReportUtils.read(fis);
fis.close();
Context context = new Context();
……
Engine engine = new Engine((ReportDefine) report, context);
report = engine.calc();
dc.insertReport("report1", report);
//执行所有修改动作,然后关闭输出文件流
dc.execute();
fos.close();
……
参考文件:InsertWord.java
注意事项:
根据实际情况设计模板文件,保证报表宽度不要超过Word模板的宽度,统计图不要位于分页处,否则会导致显示不全。
多个书签内容一起插入Word
我们可以将文本,Image对象或文件,报表一一插入到Word中,同样,我们也可以将这些对象一次性的插入更新到Word中。那么首先,我们需要将要插入的对象在一个xml文件中配置好,这个xml文件名可以任意取,格式参考下面batch.xml 的介绍:
该文件中可配置多个书签和插入对象,当对象来源于内存时,可配置成map,通过key从内存中取值,key值可以是IReport、byte[]、Image、String,值的类型程序会自动判断。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- file表示模板docx文件名,可配置绝对路径或相对路径(加载ide配置是相对于应用资源路径,加载web端配置时相对于raqsoftConfig.xml文件下<Report>里的<home>属性所对应的路径) -->
<docx file="D:/test.doc">
<!-- name表示docx书签,为空则不会插入。 file表示图片文件名,可配置绝对路径或相对路径(加载ide配置是相对于应用资源路径,加载web端配置时相对于raqsoftConfig.xml文件下<Report>里的<home>属性所对应的路径) -->
<bookmark name="f" type="image" file="D:/test.jpg"/>
<!-- name表示docx书签, text要插入的文本文字 -->
<bookmark name="c" type="text" text="测试的XML specified text."/>
<!-- key表示给定map中的key(未填时跟name相同,对应的value只能是IReport、byte[]、Image、String) -->
<bookmark name="a" type="map" key="f"/>
<!-- reportFile表示报表模板文件名,可配置绝对路径或相对路径(加载ide配置是相对于应用资源路径,加载web端配置时相对于raqsoftConfig.xml文件下<Report>里的<home>属性所对应的路径)-->
<bookmark name="b" type="report" reportFile="D:/student.rpx">
<!-- value表示报表参数值(串) -->
<reportParam name="arg1" type="value" value="设定参数1"/>
<!-- type为map时会从内存中根据name读取key,key未填写时跟name相同,key=""时会取map中key为空的值-->
<reportParam name="arg2" type="map"/>
<!-- key表示给定map中的key(未填时跟name相同)-->
<reportParam name="" type="map" key=""/>
</bookmark>
</docx>
2) 同时将多个书签内容插入Word
try {
// 输出文件
File of = new File("D:/out.docx");
……
FileOutputStream fos = new FileOutputStream(of);
//加载xml,batch.xml内容如上一小节所示
String xmlConfig = DocxChanger.xmlFile2String("D:/batch.xml");
// 准备环境,加载ide中配置的数据源信息和应用资源路径信息,如加载web端配置信息如下内容可省略
System.setProperty("report.home", "D:/Program Files/raqsoft/report");
System.setProperty("start.home", "D:/Program Files/raqsoft/report");
com.raqsoft.report.ide.base.ConfigOptions.load();
GV.dsModel =new com.raqsoft.report.ide.base.DataSourceListModel();
com.raqsoft.report.ide.RPX.loadDataSources();
Context rootCtx = GV.prepareContext();
Context.setInitCtx(rootCtx);
……
//当插入的对象来自内存,比如IReport对象
File f4 = new File("d:/product.rpx");
FileInputStream fis = new FileInputStream(f4);
IReport report = ReportUtils.read(fis);
fis.close();
Context context = new Context();
…… //假如需要的数据源信息在ide数据源配置中存在,则可省略
Context.setInitCtx(rootCtx);// 假如需要的数据源信息在ide数据源配置中存在,则可省略
Engine engine = new Engine((ReportDefine) report, context);
report = engine.calc();
HashMap map = new HashMap();
map.put("f", report); //设置xml中key为f的值
//当插入的对象来自内存,比如String,设置xml中key为arg2的值
map.put("arg2", "2014-12-15 12:00:23");
DocxChanger.insert(map, xmlConfig, fos);
fos.close();
} catch (Throwable x) {
x.printStackTrace();
}
参考文件:InsertWords.java