PageOffice對Word文檔中Table的操作,包括給單元格賦值和動態添加行的效果。
1 應用場景
OA辦公中,經常要在文檔的指定位置表格,填充後端指定數據。
如word文檔中,表格數據 如下表格中
人員信息
怎麼把後端的關鍵數據,填充到word文檔表格中呢?
2 實現方法
文檔中設置好書籤,設置好表格後,通過後端調用pageOffice插件代碼就可以實現。
3 實現過程
以java的springboot框架爲例
1 集成pageOffice
https://www.zhuozhengsoft.com/dowm/
從pageOffice官網
下載頁面,找到springboot的集成示例,按照裏面的集成明說,可以集成到自己的springboot項目中。
2 設置數據區域 包含表格
數據區域的技巧
https://www.kancloud.cn/pageoffice_course_group/pageoffice_course/663125
最終效果如圖
注意,一定要書籤包含整個表格,並且還要包含表格上下一行
3 通過代碼寫入數據
@RequestMapping(value = "Word", method = RequestMethod.GET)
public ModelAndView showWord(HttpServletRequest request, Map<String, Object> map) {
PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
poCtrl.setServerPage(request.getContextPath() + "/poserver.zz");//設置服務頁面
WordDocument doc = new WordDocument();
//打開數據區域
DataRegion dataRegion = doc.openDataRegion("PO_regTable");
//打開table,openTable(index)方法中的index代表Word文檔中table位置的索引,從1開始
Table table = dataRegion.openTable(1);
//給table中的單元格賦值, openCellRC(int,int)中的參數分別代表第幾行、第幾列,從1開始
table.openCellRC(3, 1).setValue("A公司");
table.openCellRC(3, 2).setValue("開發部");
table.openCellRC(3, 3).setValue("李清");
//插入一行,insertRowAfter方法中的參數代表在哪個單元格下面插入一個空行
table.insertRowAfter(table.openCellRC(3, 3));
table.openCellRC(4, 1).setValue("B公司");
table.openCellRC(4, 2).setValue("銷售部");
table.openCellRC(4, 3).setValue("張三");
poCtrl.setWriter(doc);
//隱藏菜單欄
poCtrl.setMenubar(false);
//隱藏自定義工具欄
poCtrl.setCustomToolbar(false);
//打開Word文檔
poCtrl.webOpen("/doc/WordSetTable/test.doc", OpenModeType.docNormalEdit, "張三");
map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));
ModelAndView mv = new ModelAndView("WordSetTable/Word");
return mv;
通過以上代碼,可以將數據庫中的
A公司 開發部 李清
B公司 銷售部 張三
寫到文檔中。
4 代碼打開後的效果
5 總結
PageOffice對Word文檔中Table的操作,包括給單元格賦值和動態添加行的效果。