需求:根据用户信息,以及和他签订合同的企业信息,用这些参数生成一份word的合同文档,并让用户在小程序手写签名,生成一个图片,插入到word文档里的个人签名那里。

以前只是利用poi做过一些导入导出excel,这个就没搞过,网上的解决方案也是各种各样的,最终我找到了这样的一套解决方案,使用的是freemarker模板,并测试通过了。

1.导入需要的包

     <dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.30</version></dependency>

2.创建一个word文档,然后把它另存为xml格式

2.重命名,将文件后缀改为ftl

3.使用编辑器打开这个文件,将需要填入的参数改成下面的格式

注意图片这里,打开之后是转码成为base64的,

把这段直接删了,改为<w:binData w:name=“wordml://1.png”>${signaturelegal}
</w:binData>

剩下的就是Java代码了,,我就直接贴下面了


```bash
package com.yunsw.medicine.user.controller;import com.yunsw.medicine.common.utils.KeyId;
import com.yunsw.medicine.user.pojo.User;
import com.yunsw.medicine.user.service.CompService;
import com.yunsw.medicine.user.service.UserService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;@RestController
public class WordController {@Autowiredprivate CompService compService;@Autowiredprivate UserService userService;//生成合同@PostMapping("Contract")public ResponseEntity createContract(String username,String compname,String qianming,String areaname,String startyear,String startmouth,String startday,String endyear, String endmouth,String endday){String filepath=null;try {WordController wordController = new WordController();
//            User user = userService.selectUserById(userid);
//            if(user==null){
//                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
//            }String filepath1=wordController.saveToFile(qianming);Map<String,String> dataMap = new HashMap<String,String>();dataMap.put("uno", compService.getSysCustomerid(5).toString());dataMap.put("compshopname", compname);dataMap.put("uname", username);dataMap.put("ucardid", "42062845552358696696");dataMap.put("uarea", areaname);dataMap.put("endyear", endyear);dataMap.put("endmonth", endmouth);dataMap.put("endday", endday);dataMap.put("signaturelegal", wordController.getImageStr(filepath1));dataMap.put("startyear", startyear);dataMap.put("startmonth", startmouth);dataMap.put("startday", startday);Configuration configuration = new Configuration();configuration.setDefaultEncoding("utf-8");//指定模板路径的第二种方式,我的路径是D:/      还有其他方式configuration.setDirectoryForTemplateLoading(new File("D:/testword/"));// 输出文档路径及名称filepath="file/test.doc";File outFile = new File("D:/yiyao/pages/"+filepath);//以utf-8的编码读取ftl文件Template t =  configuration.getTemplate("法人授权委托书1.ftl","utf-8");Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240);t.process(dataMap, out);out.close();} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}return ResponseEntity.ok("http://192.168.1.100:8585/"+filepath);}/*** 获得图片的Base64编码* @param imgFile* @return* @Author Huang Xiaocong 2018年12月15日 下午10:15:10*/public String getImageStr(String imgFile) {InputStream in = null;byte[] data = null;try {in = new FileInputStream(imgFile);} catch (FileNotFoundException e) {//log.error("加载图片未找到", e);e.printStackTrace();}try {data = new byte[in.available()];//注:FileInputStream.available()方法可以从输入流中阻断由下一个方法调用这个输入流中读取的剩余字节数in.read(data);in.close();} catch (IOException e) {//log.error("IO操作图片错误", e);e.printStackTrace();}BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(data);}public String saveToFile(String destUrl) {FileOutputStream fos = null;BufferedInputStream bis = null;HttpURLConnection httpUrl = null;URL url = null;int BUFFER_SIZE = 1024;byte[] buf = new byte[BUFFER_SIZE];int size = 0;String filepath=null;try {url = new URL(destUrl);httpUrl = (HttpURLConnection) url.openConnection();httpUrl.connect();bis = new BufferedInputStream(httpUrl.getInputStream());filepath="D:\\testword\\"+ KeyId.nextId()+".png";fos = new FileOutputStream(filepath);while ((size = bis.read(buf)) != -1) {fos.write(buf, 0, size);}fos.flush();} catch (IOException e) {} catch (ClassCastException e) {} finally {try {fos.close();bis.close();httpUrl.disconnect();} catch (IOException e) {} catch (NullPointerException e) {}}return filepath;}public static void main(String[] args) {WordController wordController = new WordController();//wordController.createWord();}}```

这样执行就可以生成一个新的word了

java 指定参数生成world文档并插入图片相关推荐

  1. poi生成word文档,插入图片,echar报表生成到word,word表格

    poi生成word文档,word表格,将echar报表生成到word 项目中用到生成word报表,报表中有表格的合并 .页眉.表格中会有报表图片.然后查找了网上的资料,利用echar生成柱状图,然后已 ...

  2. Java使用FreeMarker自动生成Word文档(带图片和表单)

    Java使用FreeMarker自动生成Word文档(带图片和表单) 1 背景 2 目标效果 3 创建Word模板 3.1 创建模板文档 3.2 转换模板文档 3.3 处理模板文档中的占位符 3.4 ...

  3. Java使用poi-tl生成word文档

    Java使用poi-tl生成word文档,可以对模板文件进行文本替换,图片.表格.超链接添加.图表处理等.大概的说明都在代码注释里,只有一个地方需要注意,就是图表的替换,占位符{{barChart}} ...

  4. Java使用freemarker生成word文档并转pdf文档

    Java使用freemarker生成word文档后转pdf 先来看看效果图 进入正题 项目需求: 为订单后生成对应的pdf文档,文档内包含图片. 方案一:使用freemarker和itext把html ...

  5. Java:iText生成pdf文档

    依赖 <!-- pdf:start --> <dependency><groupId>com.itextpdf</groupId><artifac ...

  6. java 导出word_Java 生成Word文档

    Word具有强大的文字处理功能,是我们日常工作生活中广泛使用到的工具之一.本文就将介绍如何使用Free Spire.Doc for Java在Java应用程序中创建Word文档,插入图片,并且设置段落 ...

  7. word文档里插入图片显示不完整,只显示一半,怎么处理?

    word文档里插入图片显示不完整,只显示一半,怎么处理? 目录 word文档里插入图片显示不完整,只显示一半,怎么处理? 1.把鼠标光标放置图片的末尾,然后点击鼠标右键,选择[段落]选项 2.在[缩进 ...

  8. python数据写入表格生成图片_python在word文档里插入图片和表格实例代码演示

    # -*- coding: UTF8 -*- from docx import Document from docx.shared import Pt doc = Document() # 文件存储路 ...

  9. word文档中插入图片显示不全解决办法

    在windows下写word文档,正常情况下,我们应该不会遇到插入图片显示不全的问题,好像是如果在已有的文档中插入图片,比如文档中间插入,图片没办法自动扩展空间,这就导致了显示不全的问题. 这个问题也 ...

最新文章

  1. Onchain Capital创始人看涨BCH
  2. 零基础自学python教程-零基础人员可以学习python吗?|Python培训基础教程
  3. Leetcode1702. 修改后的最大二进制字符串[C++题解]:思维题
  4. 微服务架构统一安全认证设计与实践
  5. 数据库中的表还是一定要建索引
  6. oracle 12g 无监听,Oracle 12.2监听无法启动解决一例
  7. js中的数据转换、整数、小数保存、四舍五入
  8. 离地球近的星星,远离情况怎样?
  9. 佳能 6D Mark II与 90D 对比评测
  10. 省钱兄同城外卖源码O2O同城服务源码校园外卖源码uniapp前端模版
  11. java获取pdf文字坐标_Java 获取PDF关键字坐标
  12. 成都市金牛区2018年小学入学划片范围
  13. docker服务假死解决方案
  14. mysql 索引配置_Mysql索引配置
  15. 安装更强大更美观的zsh,配置oh my zsh及插件
  16. linux下 部署调用SAP接口
  17. Lottie 动画导出为 GIF/MP4 以及与 QML 集成演示
  18. linux电脑开机进不了系统更新失败,电脑卡在配置Windows Update失败界面无法开机怎么办...
  19. 千寻位置 开发demo_千寻位置发布全新操作系统,助力卫星导航产业化
  20. python毕业设计作品基于django框架校园网站系统毕设成品(4)开题报告

热门文章

  1. jQuery+Ajax+Axios
  2. 哈尔,那个失心的少年
  3. Android使能volte高清视频通话功能
  4. css怎么将块元素变成,CSS块元素、行内元素、行内块元素的转换
  5. android camera慢动作,关于android:这可能是全网关于Camera慢动作录像SlowMotion介绍最全的文章了...
  6. WSL2--Config
  7. input里面添加图片
  8. python 内存分析工具_python内存监控工具memory_profiler和guppy的用法详解
  9. OpenCV-Python学习(8)—— OpenCV 颜色表操作(cv.LUT、cv.applyColorMap)
  10. MySQL数据库学习——啥是SQL语句?