freemarker,word转化为xml 占位符被分离

本来想通过占位符直接写成${fangyuan001} 这样的数据。就不用打开xml再修改。

可以遇到一部分占位符被分离

通过再wrod里面再怎么写都没用。

我的解决办法

我就把 ${fangyuan001}写到 空白的txt 里面 再直接txt里面的 ${fangyuan001} 复制粘贴到word的对应位置,就没有被分离了。保持了样式的一致性

----------------------------------------------------------------------------------------------------------

顺便插一句 图片也可以通过文字的占位符,然后写入图片的

单张图片直接把img标签一起写进去,不改xml

我们查看最后生成的word ,格式为doc。就是xml文件格式。查看图片大概是这样的格式

<w:pict><w:binData w:name="wordml://1557280704565.png" xml:space="preserve">

图片的base64


</w:binData><v:shape id="图片 1557280704565" o:spid="_x0000_i1025" type="#_x0000_t75" style="600pt;height:600pt;visibility:visible;mso-wrap-style:square"><v:imagedata src="wordml://1557280704565.png" o:title=""/></v:shape></w:pict>

 

 

其实我们可以在word里面用一个 ${photo1}的占位符。直接把上面的一串标签替换就可以显示图片了。图片的高宽也可以替换的。替换width和height的600pt值即可

图片几个细节。

1 : 图片的id 上面的1557280704565标签需要替换成随机数。否则一样的图片会显示同一个。

2:图片的base编码需要通过代码加载进来。加载成那种不是一行的baes64格式。如下生出base64方法

 public  static String picture2Base64(String picturePath){
        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(picturePath);
            data = new byte[in.available()];
            in.read(data);
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(data);
        } catch (Exception e) {
            e.printStackTrace();
            throw  new RuntimeException(e);
        }finally {
            IOUtils.closeQuietly(in);
        }
    }
原文地址:https://www.cnblogs.com/fangyuandoit/p/13713828.html