生成word文档和word域动态赋值,很多时候需要生成这样的word文档供下载和打印,先制作一个包含了域的 word的模板附件,放在服务器端或者字节流存入数据库,以供需要的时候代码可以获取到,如:

其中右击每个域可以选择编辑域,修改域的名称;

获取到保存在数据库中的word模板内容:

// DocAttachFile为数据库存放这个word附件的表对应的实体,通过这个实体类的content属性对应表的content字段获取到word的内容
DocAttachFile docAttachFile = (DocAttachFile ) docAttachFile list.get(0);
String licenseName = JsfHelper.getDeployWarPath() + "WEB-INF/classes/license.xml";
try {
License license = new License();
license.setLicense(licenseName);
Document doc = new Document(docAttachFile .getContent());
String[] fieldNames = null;
Object[] values = null;

使用aspose的word组件展示word内容和给域赋值:

            Map<?, ?> docmap = getDocMap();
if (docmap.containsKey(this.doctype)) {
Map<String, String> map = (Map<String, String>) docmap.get(this.doctype);
fieldNames = new String[(map == null) ? 0 : map.size()];
values = new Object[(map == null) ? 0 : map.size()];
int num = 0;
for (Map.Entry entry : map.entrySet()) {
fieldNames[num] = ((String) entry.getKey());
values[num] = entry.getValue();
++num;
} // 获取邮件合并类型的域
doc.getMailMerge().execute(fieldNames, values);               boolean isfiltersubmit = true;
if ((String.valueOf(5).equals(this.doctype)) || (String.valueOf(2).equals(this.doctype))) {
isfiltersubmit = false;
}
              // 调用的aspose的word组件方法
mergertable(doc, isfiltersubmit);
String docname = this.autoService.getItemTextByName("word附件", this.doctype) + ".doc";
public void mergertable(Document doc, boolean isfiltersubmit) throws Exception {
String sql = this.userMaterialService.getDocMaterialSQL(this.user.getPviguid(), isfiltersubmit);
CRUDService crud = new CRUDService(DataSourceFactory.getFrameDs());
Connection conn = crud.getDb().getConnection();
ResultSet resultSet = SQLManageUtil.executeDataTable(sql, crud, conn, 1005, 1007);
if (resultSet.next()) {
try {
resultSet.first();
DataTable orderTable = new DataTable(resultSet, "Material");
doc.getMailMerge().executeWithRegions(orderTable); crud.closeDataSource();
conn.close();
resultSet.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
else {
List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
Map<String, Object> data1 = new HashMap<String, Object>();
data1.put("row_index", "");
data1.put("MaterialName", "");
dataList.add(data1);
doc.getMailMerge().executeWithRegions(new MapMailMergeDataSource(dataList, "Material"));
}
}

获取其他信息给word的域赋值:

 public Map<String, Map<String, String>> getAllDocValue() {
Map<String, Map<String, String>> docmap = new HashMap<String, Map<String, String>>(); // 三好青年申报审批表
Map<String, String> threehqnApply= new HashMap<String, String>();
threehqnApply.put("FlowSn", this.user.getFlowsn());
threehqnApply.put("Shenpilb", this.autoService.getItemTextByName("三好青年类别", this.user.getShType()));
threehqnApply.put("Year", String.valueOf(DateUtil.getYearOfDate(new Date())));
threehqnApply.put("ApplyerName", this.user.getApplyername());
threehqnApply.put("contact", this.user.getContactperson());
threehqnApply.put("ApplyDate", DateUtil.convertDate2String(this.user.getApplydate(), "yyyy年MM月dd日"));
threehqnApply.put("TaskName", this.user.getProjectname());
docmap.put(String.valueOf(1), threehqnApply); // 四好青年申请表附件
Map<String, String> fourhqnApply= new HashMap<String, String>();
fourhqnApply.put("flownum", this.user.getFlowsn());
fourhqnApply.put("plateNumber", user.getPlateNumber());
fourhqnApply.put("ownerName", user.getOwnerName());
docmap.put(String.valueOf(32), fourhqnApply); return docmap;
}

这个方法中可以同时给多个附件赋值;

生成word附件和word域动态赋值的更多相关文章

  1. 生成带有表格的word附件和动态赋值

    生成带有表格的word附件和动态赋值, 表格前后需要添加一个特殊的开始和结束的域,表格第一列 TableStart:AuditJdcttbzInfo 和表格的最后一列 TableEnd:AuditJd ...

  2. Java利用poi生成word(包含插入图片,动态表格,行合并)

    转(小改): Java利用poi生成word(包含插入图片,动态表格,行合并) 2018年12月20日 09:06:51 wjw_11093010 阅读数:70 Java利用poi生成word(包含插 ...

  3. JSP利用freemarker生成基于word模板的word文档

    利用freemarker生成基于word模板的word文档 freemarker简介 FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与Web容器 ...

  4. 【jacob word】使用jacob,合并多个word为一个word文件

    将几个word文件合并到一个word文件,使用注意点: 1.后面附项目运用的jar包jacob-1.9, 2.并且jacob运用中,需要将附件内的jacob.dll放到windows/system32 ...

  5. 使用Word API打开Word文档 ASP.NET编程中常用到的27个函数集

    使用Word API(非Openxml)打开Word文档简单示例(必须安装Word) 首先需要引入参照Microsoft.Office.Interop.Word 代码示例如下: public void ...

  6. 利用模板导出文件(二)之jacob利用word模板导出word文件(Java2word)

    https://blog.csdn.net/Fishroad/article/details/47951061?locationNum=2&fps=1 先下载jacob.jar包.解压后将ja ...

  7. POI 读取word (word 2003 和 word 2007) (转)

    最近在给客户做系统的时候,用户提出需求,要能够导入 word 文件,现在 microsoft word 有好几个版本 97.2003.2007的,这三个版本存储数据的格式上都有相当大的差别,而现在 9 ...

  8. Highcharts动态赋值学习

    最近老大让做项目中的统计图表功能,需要使用highcharts这个插件,弄出来了两种类型的图表动态赋值的实现,首先贴上中文网的官网和api地址,使用这个英语不好的同学肯定会用到的: 中文网官网:htt ...

  9. URL动态赋值

    url动态赋值: 指的是url中包含{selector},即花括号括起来的jQuery选择器,当提交该url时,框架会自动将selector对应元素的值替换到花括号所占区域. (感觉实现了一点类似el ...

随机推荐

  1. iOS 循环引用 委托 (实例说明)

    如何避免循环引用造成的内存泄漏呢: 以delegate模式为例(viewcontroller和view之间就是代理模式,viewcontroller有view的使用权,viewcontroller同时 ...

  2. vue之用法

    一.安装 对于新手来说,强烈建议大家使用<script>引入 二. 引入vue.js文件 我们能发现,引入vue.js文件之后,Vue被注册为一个全局的变量,它是一个构造函数. 三.使用V ...

  3. 慕课网,vue高仿饿了吗ASP源码视频笔记

    1.源码笔记 我的源码+笔记(很重要):http://pan.baidu.com/s/1geI4i2Z 感谢麦子学院项目相关视频 2.参考资料 Vue.js官网(https://vuejs.org.c ...

  4. JPA的锁机制

    JPA 各种实体锁模式的区别 字数2084 阅读304 评论0 喜欢4 为了能够同步访问实体,JPA提供了2种锁机制.这两种机制都可以避免两个事务中的其中一个,在不知情的情况下覆盖另一个事务的数据. ...

  5. 在Ubuntu中添加和删除PPA的软件源

    PPA,英文全称为 Personal Package Archives,即个人软件包档案.是 Ubuntu Launchpad 网站提供的一项源服务,允许个人用户上传软件源代码,通过 Launchpa ...

  6. ububtu16.04下安装protobuf

    重新下载protobuf,我下载的时最新的protobuf-all-3.5.1.tar.gz   protobuf网址:https://github.com/google/protobuf/relea ...

  7. HIT 2715 - Matrix3 - [最小费用最大流][数组模拟邻接表MCMF模板]

    题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2715 Time limit : 5 sec Memory limit : 64 M Zhouguyue ...

  8. TensorFlow 实现分类操作的函数学习

    函数:tf.nn.sigmoid_cross_entropy_with_logits(logits, targets, name=None) 说明:此函数是计算logits经过sigmod函数后的交叉 ...

  9. 【紫书】【重要】Abbott's Revenge UVA - 816 bfs 复杂模拟 带方向参数的迷宫

    题意:一个迷宫,每个交叉路口有一路标,限制了你从某方向进入该路口所能进入的路口. 题解:1.对于方向的处理:将node多增加一维dir,通过一个const 字符数组 加 上dir_id函数 以及一个方 ...

  10. 线性参照,M值的相关测试

    怎样使用普通线要素获取带M值的线要素 怎样查看线要素各个折点上的M值,怎样导出为一张表 线性参照:http://resources.arcgis.com/zh-cn/help/main/10.2/in ...