Transfer Java to assign value to table in word

1. Preparatory work:

  Download PageOffice for Java: http://www.zhuozhengsoft.com/dowm/

2. Implementation method:

  To call PageOffice to operate the table in Word, it must be implemented with the help of the DataRegion (the reason is that in word The table only has index, no name), the data area is required to completely contain the content of the entire table, so that the table can be controlled and operated through the data area. The table insertion can be manually inserted at the bookmark in the Word template: "Insert" → "Table" in the toolbar, or it can be dynamically added through the data area in the program.

  If you don't understand what "data area" is, visit: http://www.zhuozhengsoft.com/PageOffice/course/2017/0719/276.html

  1. Edit the word template. For example: There is such a staff information table in the word document. If you want to fill data in this staff information table, you must first insert the entire table of grades into a "bookmark": PO_regTable. When inserting bookmarks, be sure to select the entire table.


  

  2. Write code to fill the table

copy code
    PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
    poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz");
    //Operate table
    WordDocument doc = new WordDocument();
    DataRegion dataRegion = doc.openDataRegion("PO_regTable");
    //Open the table, the index in the openTable(index) method represents the index of the table position in the current bookmark, starting from 1
    Table table = dataRegion.openTable(1);
    //Give The cell assignment in the table, the parameters in openCellRC(int,int) represent the row and column respectively, starting from 1
    table.openCellRC(3, 1).setValue("Company A");
    table.openCellRC( 3, 2).setValue("Development Department");
    table.openCellRC(3, 3).setValue("Li Qing");
    //Insert a row, the parameter in the insertRowAfter method represents which cell to insert a blank row under
    table.insertRowAfter(table.openCellRC(3, 3));
    table.openCellRC(4, 1).setValue("Company B");
    table.openCellRC(4, 2).setValue("Sales");
    table. openCellRC(4, 3).setValue("Zhang San");
   
    poCtrl1.setWriter(doc);
    //open a file
    poCtrl1.webOpen("doc/test.doc", OpenModeType.docNormalEdit, "username");
Copy code
  3. The effect of the generated file

  

3. Sample code

  Unzip the downloaded PageOffice for java development package, copy the Samples4 folder to Tomcat In the Webapps directory, visit: http://localhost:8080/Samples4/index.html

  Refer to Samples4: 1. 17. Simple example (WordSetTable) for assigning value to Table in Word document.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325339629&siteId=291194637