First, the preparatory work:
Download Pageoffice for java:http://www.zhuozhengsoft.com/dowm/
Second, the realization method:
To invoke Pageoffice operations on a table in Word, you must do so with the data region (DataRegion) (because the table in Word has index only, no name), and the data region is required to contain the entire table content. This allows you to control and manipulate the table through the data region. In addition, table inserts can be inserted manually in the Word Template: the toolbar "insert" → "table", and can be dynamically added through the data region in the program.
If you do not understand what "data region" is, visit: http://www.zhuozhengsoft.com/PageOffice/course/2017/0719/276.html
1. Edit the Word template. For example, if you have a person information table in your Word document that you want to populate with data in this person information table, you must first insert the table for the entire table into a bookmark: po_regtable. Be sure to select the entire table when inserting bookmarks.
2. Write code to populate table
Pageofficectrl POCTRL1 =NewPageofficectrl (Request); Poctrl1.setserverpage (Request.getcontextpath ()+ "/poserver.zz"); //OperationTableWorddocument doc =Newworddocument (); DataRegion DataRegion= Doc.opendataregion ("po_regtable"); //the index in the Open Table,opentable (Index) method represents the indexed table position in the current bookmark, starting with 1Table table = dataregion.opentable (1); //assigns a value to a cell in a table, the parameters in OPENCELLRC (Int,int) represent the first row, the first column, starting with 1TABLE.OPENCELLRC (3, 1). SetValue ("Company A")); TABLE.OPENCELLRC (3, 2). SetValue ("Development Department"); TABLE.OPENCELLRC (3, 3). SetValue ("Liqingfeng"); //Insert a row, the parameter in the Insertrowafter method represents the cell below which a blank line is insertedTable.insertrowafter (TABLE.OPENCELLRC (3, 3)); TABLE.OPENCELLRC (4, 1). SetValue ("Company B")); TABLE.OPENCELLRC (4, 2). SetValue ("Sales Department"); TABLE.OPENCELLRC (4, 3). SetValue ("Zhang San"); Poctrl1.setwriter (DOC); //Open FilePoctrl1.webopen ("Doc/test.doc", Openmodetype.docnormaledit, "user name");
3. Effects of generating files
Three, sample code
Unzip the downloaded Pageoffice for Java development Package, copy the Samples4 folder to the Tomcat WebApps directory, Access: http://localhost:8080/Samples4/index.html
Refer to Samples4: One, 17, a simple example (wordsettable) that assigns a value to a table in a Word document.
Original Java assigns a value to a table in Word