Java fills data into word template (summary)

Anyone who has used PageOffice to dynamically generate word documents knows that PageOffice can fill in the specified position of the word document. The specified position we are talking about here has two concepts in the professional terminology of PageOffice, one is called the data region (DataRegion), the other One is called DataTag.

  1. Concept

  Data area: is the document area located by bookmarks named with the "PO_" prefix in the Word document. Simply put, the data area is a special Word bookmark object that facilitates locating the content in the Word document.

  Data tag: It is a special style string composed of any valid characters (such as "[time]", "{name}", "${name}", etc.) as a mark, theoretically such a string (such as: "Gender", "Address", "aa", "bbcc") can also be used as data tags, but it is not recommended, because such tags cannot visually tell which data tags are contained in the word document.

  2. Use

  The content that can be filled in the data area: text, pictures, Word files, Excel files.

copy code

WordDocument doc = new WordDocument();
    //fill text
    DataRegion dataRegion1 = doc.openDataRegion("PO_userName");
    dataRegion1.setValue("张三");
    //You can also set the font, color, bold and other styles for the filled text
    dataRegion1.getFont().setColor(Color.blue);
    dataRegion1.getFont().setSize(24);
    dataRegion1.getFont().setName("隶书");
    dataRegion1.getFont().setBold(true);
    
    //fill image
    doc.openDataRegion("PO_deptName").setValue("[image]img/123.jpg[/image]");
    //fill word file
    doc.openDataRegion("PO_deptName").setValue("[word]doc/aabb.doc[/word]");
    // populate the excel file
    doc.openDataRegion("PO_deptName").setValue("[excel]doc/bbcc.xls[/excel]");

copy code

  What can be filled in the data label: text.

 

copy code

WordDocument doc = new WordDocument();
    //Fill the text, you can also set the font, color and other styles
    DataTag deptTag = doc.openDataTag("{department name}");
    deptTag.setValue("Marketing Department");
    deptTag.getFont().setColor(Color.GREEN);
    deptTag.getFont().setName("宋体");
    deptTag.getFont().setSize(28);

copy code

 

  3. Difference

        The DataRegion class is to be used in conjunction with bookmarks. You can first insert a bookmark starting with "PO_" into the Word document to define the data area, and then obtain the DataRegion object in the program through the OpenDataRegion(String DataRegionName) method of the WordDocument class object; or Use the CreateDataRegion(String NewDataRegionName, DataRegionInsertType insertType, String RelativeDataRegionName) method of the WordDocument class object to directly create the data region to obtain the DataRegion class object. The DataTag class is used in conjunction with user-inserted custom feature text in a Word document. In the program, the DataTag class object can only be obtained through the OpenDataTag(String DataTagName) method.

         In addition, the name of the data area (that is, the bookmark name) must start with "PO_", and there must be at least one valid character between two bookmarks, which cannot be repeated. The name of the data area and the position in the Word document are in one-to-one correspondence. The name of the data tag is a word string text with certain characteristics, which is convenient for locating the content in the Word document and can be repeated. The name of the data tag and the position in the Word document have a one-to-many relationship. That is to say, the DataRegion of the same Name can only represent only one position in the Word document, and the same characteristic string can appear in multiple places in the Word document.

         Therefore, the advantages of DataRegion are: DataRegion can not only fill document content, but also create document content, submit the content entered by the user in the DataRegion, and obtain the content in the DataRegion submitted by the user, while DataTag can only be used to fill the document content. The disadvantage of DataRegion is: DataRegion cannot be simply repeated, and different bookmarks must be set, while DataTag can generate repeated content in multiple places in the document.

         And should use DataTag or DataRegion, it should be determined according to the specific needs. When the user wants to submit a certain part of the data in Word or wants to obtain a certain part of the data submitted in Word, he must use DataRegion; when the user does not need to submit data or obtain data, and the filled data has no duplicates, then both Yes; when the user does not need to submit data or obtain data, and the filled data has multiple duplicates, although both are possible, it is recommended to use DataTag for the convenience of programming and code efficiency.

Tags:  javadynamic fillword

Guess you like

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