The table operation in word needs to be implemented with the help of data region, which requires that the data region completely contains the contents of the whole table, so that the table can be controlled and operated through the data region. Therefore, to use table, you must insert bookmarks in the word file. For table insertion, you can insert it manually in the label of the word template: Toolbar "insert" → "table", or add it dynamically in the program through the data area.
Here are the steps to add tables dynamically
1: Create a bookmark for the Word template. (two methods)
(1) You can manually add a bookmark in the word template: toolbar insert → bookmark
(2) Dynamically create a data area (bookmark) with pageoffice
2: Insert table
2: Specific code
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="com.zhuozhengsoft.pageoffice.*,com.zhuozhengsoft.pageoffice.wordwriter.*"%> <% PageOfficeCtrl poCtrl = new PageOfficeCtrl(request); WordDocument doc = new WordDocument(); //Dynamically create a table with 3 rows and 5 columns in the data area of "Po Table 1" specified in word Table table1 = doc.openDataRegion("PO_table").createTable(3,5,WdAutoFitBehavior.wdAutoFitWindow); //Merge cells from (1,1) to (3,1) and assign values table1.openCellRC(1,1).mergeTo(3,1); table1.openCellRC(1,1).setValue("Merged cells"); //Assign values to the remaining cells in Table 1 for(int i=1;i<4;i++){ table1.openCellRC(i, 2).setValue("AA" + String.valueOf(i)); table1.openCellRC(i, 3).setValue("BB" + String.valueOf(i)); table1.openCellRC(i, 4).setValue("CC" + String.valueOf(i)); table1.openCellRC(i, 5).setValue("DD" + String.valueOf(i)); } //A new data area "Po Table 2" is dynamically created after "Po Table 1", which is used to create a new table 2 with 5 rows and 5 columns DataRegion drTable2= doc.createDataRegion("PO_table2", DataRegionInsertType.After, "PO_table1"); Table table2=drTable2.createTable(5,5,WdAutoFitBehavior.wdAutoFitWindow); //Assign value to new table table2 for(int i=1;i<6;i++){ table2.openCellRC(i, 1).setValue("AA" + String.valueOf(i)); table2.openCellRC(i, 2).setValue("BB" + String.valueOf(i)); table2.openCellRC(i, 3).setValue("CC" + String.valueOf(i)); table2.openCellRC(i, 4).setValue("DD" + String.valueOf(i)); table2.openCellRC(i, 5).setValue("EE" + String.valueOf(i)); } poCtrl.setWriter(doc);//This trip must poCtrl.setServerPage(request.getContextPath()+"/poserver.zz"); poCtrl.webOpen("doc/createTable.doc", OpenModeType.docNormalEdit,"Zhang Yiming"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Word Create tables dynamically in</title> </head> <body> <div style="width: auto; height: 800px;"> <%=poCtrl.getHtmlCode("PageOfficeCtrl1")%> </div> </body> </html>
Final effect
You can go to the official pageoffice website to download the sample code and directly throw the samples4 folder under Tomcat's webapps to start Tomcat and access the browser.
You can also watch videos to get started quickly when you first contact pageoffice http://www.zhuozhengsoft.com/Technical/