生成方式是用doc或者docx格式的模板,然后用流将内容写入。
大概的代码如下:
byte[] modelByte = Files.readAllBytes(Paths.get(inputUrl)); Template<WayTemplate> template = new Template<>(wayTemplate); byte[] resultByte = devSiteBusiness.fillWordDataByDomain(modelByte, template); String fileName = StringUtil.uuid() + "." + "doc"; String resultPath = SysConfig.UPLOAD_WAY_PATH + fileName; File resultFile = new File(UploadUtils.absolutePath, resultPath); FileOutputStream fos = new FileOutputStream(resultFile); fos.write(resultByte); fos.close(); return new WayInfomation(applyId, "通信电源负载投退方式单", resultPath, 4, "doc");
生成的word文档中会插入一张图片,java类型为BufferImage。生成word后,发现图片的布局是嵌入,这样图片显示的时候会被遮住一大半。修改图片的布局或者行间距可以解决这个问题,选择修改word模板的行间距解决图片问题解决。
但是一个新的问题又出现了,图片是显示正常了,文档中的中文又变成乱码了。咋修改行间距还有这个影响?在网上查找解决方案的时候,看到了一句有趣的话:“ 模板里要插入参数的地方使用中宋字体”。试了试,果然可以。简直太神奇了。

