1. 程式人生 > >生成word附件和word域動態賦值

生成word附件和word域動態賦值

return arr fir inf 字段 pla 動態 field equals

生成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域動態賦值