生成word附件和word域动态赋值
生成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;
}
这个方法中可以同时给多个附件赋值;
打听下这个变量名谁起的