[Original]java populate the Word template with data (summary)

Source: Internet
Author: User

People who have used pageoffice to dynamically generate Word documents know that Pageoffice can populate a Word document at a specified location, where we say that there are two concepts in the Pageoffice terminology. One is called the data region (DataRegion) and the other is the data label (DATATAG).

First, the concept

Data range: A document area that is located in a Word document that has a bookmark named "Po_" prefixed to it. In simple terms, a data region is a special Word bookmark object that makes it easier to locate content in a Word document.

Data label: is a special style of any valid character string (such as "" Time "," {name} "," ${name} "and so on) as a marker, theoretically such a string (such as" gender "," Address "," AA "," BBCC ") can also be used as data labels, but not recommended, Because such labels do not visually visually reveal which data labels are included in a Word document.

  Second, use

What you can fill in the data area: text, pictures, Word files, Excel files.

Worddocument doc =Newworddocument (); //Fill TextDataRegion DataRegion1 = doc.opendataregion ("Po_username"); Dataregion1.setvalue ("Zhang San"); //you can also set fonts, colors, bold styles, and so on for filled textDataregion1.getfont (). SetColor (Color.Blue); Dataregion1.getfont (). SetSize (24); Dataregion1.getfont (). SetName (Lishu); Dataregion1.getfont (). Setbold (true); //Fill PictureDoc.opendataregion ("Po_deptname"). SetValue ("[Image]img/123.jpg[/image]"); //Populating Word filesDoc.opendataregion ("Po_deptname"). SetValue ("[Word]doc/aabb.doc[/word]"); //populating an Excel fileDoc.opendataregion ("Po_deptname"). SetValue ("[Excel]doc/bbcc.xls[/excel]");

What you can fill in the data label: text.

 

    New worddocument ();     // fill in the text, and 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 ("The song Body");    Depttag.getfont (). SetSize (28);

 Three, the difference

The DataRegion class is used in conjunction with bookmarks to define a data region for a bookmark that begins with "Po_" in a Word document, and then in the program through the Worddocument class object Opendataregion (String Dataregionname) method to get the DataRegion object, and through the Worddocument class object's Createdataregion (String newdataregionname, Dataregioninserttype Inserttype, String relativedataregionname) method creates a data region directly to obtain a DataRegion class object. The Datatag class is used in conjunction with custom feature text inserted by users in a Word document. Datatag class objects can only be obtained through the Opendatatag (String datatagname) method in the program.

In addition, the name of the data region (that is, the bookmark name) must begin with "Po_", with a minimum of one valid character between the two bookmarks, which cannot be duplicated, and the name of the data region corresponds to the position in the Word document one by one. The name of the data label is a word string literal with certain characteristics that facilitates locating the contents of a Word document, repeating the name of the data label and the position in the Word document as a one-to-many relationship. That is, dataregion of the same name can represent only one location in a Word document, while the same feature string in a Word document may appear in multiple places.

So the advantage of dataregion is that dataregion can fill the contents of the document, create the contents of the document, submit the content entered by the user in DataRegion, get the content of the DataRegion submitted by the user, and Datatag can only be used to populate the document content. The disadvantage of dataregion is that dataregion cannot be repeated easily, you have to set different bookmarks, and Datatag can generate multiple items in the document that need to be duplicated.

And should be used Datatag or dataregion, it should be depending on the specific needs. When a user submits a part of the data in Word or wants to get a portion of the data submitted in Word, the user must use DataRegion, either when they do not need to submit data or get the data, and if the data is populated without duplicates, and when the user does not need to submit data or obtain data, When there are multiple duplicates of the populated data, both are available, but for easy programming and code efficiency, it is recommended to use Datatag.

[Original]java populate the Word template with data (summary)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.