標籤:course 使用者名稱 htm tom path alt image server new
一、準備工作:
下載PageOffice for Java:http://www.zhuozhengsoft.com/dowm/
二、 實現方法:
要調用PageOffice操作Word中的table,必須藉助資料區域(DataRegion)實現的(原因是word中的表格只有index,沒有name),要求資料區域完整的包含了整個Table的內容,這樣才可以通過資料區域控制和操作table。而table的插入,既可以在Word模版中書籤處手動插入:工具列“插入”→“表格”,亦可以在程式中通過資料區域動態添加。
如果不明白“資料區域”是什麼,訪問:http://www.zhuozhengsoft.com/PageOffice/course/2017/0719/276.html
1. 編輯word模板。例如:word文檔中有這樣一個人員資訊表,想要在這個人員資訊表中填充資料,則必須先將整個成績表的table插入到一個“書籤”中:PO_regTable。插入書籤的時候一定要選擇整個table。
2. 編寫代碼填充table
PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request); poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz"); //操作table WordDocument doc = new WordDocument(); DataRegion dataRegion = doc.openDataRegion("PO_regTable"); //開啟table,openTable(index)方法中的index代表當前書籤中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("張三"); poCtrl1.setWriter(doc); //開啟檔案 poCtrl1.webOpen("doc/test.doc", OpenModeType.docNormalEdit, "使用者名稱");
3. 組建檔案的效果
三、 範例程式碼
解壓下載的PageOffice for java 開發包,拷貝Samples4檔案夾到Tomcat的Webapps目錄下,訪問:http://localhost:8080/Samples4/index.html
參考Samples4中的:一、17、給Word文檔中Table賦值的簡單樣本 (WordSetTable)。
[原創]Java給word中的table賦值