[Original]java operation Word (i)

Source: Internet
Author: User

I. Background of demand

In the process of doing the project, you often encounter the need to export database data to a Word file, because in many cases we need to export the data to Word for printing. This requirement can be achieved by populating the data into a Word template with a program. The so-called template is the Word file that marks the location of the data. Templates can be easily divided into two types: a template contains a fixed, limited number of data locations, such as a leave bar template only "department, name, Reason, days, date" Several data locations, it may be the 5 data location of some 4, some 3 or some 1, However, the number of labels in the template is always a subset of this set (as shown in:), and the other is to include the circular data, such as to generate an employee Information table, the table has an indefinite number of employee information, each employee's information contains the "number, department, name, age, place of origin." The first template to export Word files is relatively straightforward, and in this article we first describe how to implement this scenario.

  

Second, the realization method

1. Edit the template: The template must mark the location of the data to be inserted, so that the program can be used to insert data to the appropriate location of the file, that is, to create a file, the location of the data must be marked with some element. Using Pageoffice to populate your Word file, you need to use Word bookmarks to mark where you want to insert the data. First mark the "department, name, Reason, days, date" data locations in the Word Template: po_dept, Po_name, Po_cause, Po_num, po_date as shown:

  
General developers use Word less, it may not be clear how Word bookmarks are inserted, the following is a brief introduction to the method of inserting bookmarks.
The first method: position the cursor to the location where you want to mark the data, click "Insert"-"bookmark" In the Word menu, a dialog box titled "Bookmark" will pop Up (as shown), enter the name of the new book, note: The book signature must start with letters, Chinese characters, Chinese punctuation, etc. Can contain numbers but cannot have spaces in the middle (when developing with Pageoffice, it is not recommended to use the Chinese name Book signature). The Add button to the right of the point, and the new book signature will appear in the list below.
  

The second method: Select several text, or a paragraph of text, or select a section containing the table and the picture content, the selected content to specify the bookmark object, and then perform the same action of the first method, "insert" → "bookmark" ...
Note: If the new insertion position or new object takes an existing book signature, the original bookmark will be automatically canceled.

Pageoffice the template in the sample code in the production time, the second method is used to define the bookmark, before inserting the bookmark will be preceded by a marked word in brackets, such as: [Name], then select [Name], and then insert the bookmark. This is done to make it easy to see or edit the location of the template data at a glance.

In the process of developing with pageoffice, in order to avoid conflicts with the user's own defined bookmarks, the bookmark name required to be inserted must begin with "Po_". Note that the letter O, not the number 0. Book signatures are case-insensitive and can be written as "Po_". The data area mentioned in the Pageoffice concept is essentially a bookmark, but only the bookmark that begins with "Po_" is called the data area, please note this.

2. Write code to call Pageoffice's interface to populate the data into a Word file:

    //declaring variables to store data read from the databaseString DocName = "", Docdept = "", Doccause = "", DocNum = "", Docdate = ""; //database Data read operations (different databases with different code)ResultSet rs = stmt.executequery ("select * from Leaverecord where ID =" +ID); if(Rs.next ()) {DocName= Rs.getstring ("Name"); Docdept= Rs.getstring ("Dept"); Doccause= Rs.getstring ("Cause"); DocNum= Rs.getstring ("Num"); Docdate= Rs.getstring ("Submittime");    } rs.close (); //Create a Pageoffice worddocument object, manipulate the Word fileWorddocument doc =Newworddocument (); Doc.opendataregion ("Po_name"). SetValue (DocName); Doc.opendataregion ("Po_dept"). SetValue (docdept); Doc.opendataregion ("Po_cause"). SetValue (Doccause); Doc.opendataregion ("Po_num"). SetValue (DocNum); Doc.opendataregion ("Po_date"). SetValue (Docdate); //To create a Pageofficectrl object Open filePageofficectrl POCTRL1 =NewPageofficectrl (Request); Poctrl1.setserverpage (Request.getcontextpath ()+ "/poserver.zz");//This line must//Get Data ObjectPoctrl1.setwriter (DOC); //Open DocumentPoctrl1.webopen ("Doc/template.doc", Openmodetype.docreadonly, "Tom");

Third, the effect of generating files

  

Iv. Sample Download

Http://www.zhuozhengsoft.com/down4/Java/BigDemo/poword.rar, or download the Pageoffice for Java development package to see examples in the development package: three, 5, sample leave bar

[Original]java operation Word (i)

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.