[Original] Java assigns value to table in word

1. Preparation:

  下载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 data region (DataRegion) (the reason is that the table in word has only index and no name), and the data region is required to completely contain the content of the entire Table, so that the data region can be Control and manipulate tables. The table insertion can be manually inserted at the bookmark in the Word template: "Insert" → "Table" in the toolbar, or it can be added dynamically 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 personnel information table in the word document. If you want to fill data in this personnel 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 populate the table

    PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
    poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz"); 
    //操作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("A company" );
    table.openCellRC( 3, 2).setValue("Development" );
    table.openCellRC( 3, 3).setValue("Li Qing" );
     // Insert a row, the parameter in the insertRowAfter method represents the cell under which to insert a blank row 
    table.insertRowAfter(table.openCellRC(3, 3 )) ;
    table.openCellRC(4, 1).setValue("B公司");
    table.openCellRC( 4, 2).setValue("Sales" );
    table.openCellRC(4, 3).setValue("张三");
    
    poCtrl1.setWriter(doc);
    // Open the file 
    poCtrl1.webOpen("doc/test.doc", OpenModeType.docNormalEdit, "username");

  3. The effect of the generated file

  

3. Sample code

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

  Refer to Samples4: 1. 17. A simple example of assigning value to Table in Word document (WordSetTable).

Guess you like

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