Convert to java to operate word

1. Demand background

  In the process of doing projects, we often encounter the need to export database data to Word files, because in many cases, we need to export data to Word for printing. This requirement can be achieved by filling data into the word template programmatically. The so-called template is also a Word file that marks the location of the data. Templates can be simply divided into two types: one template contains a fixed and limited number of data locations. For example, a template for a leave request only has several data locations of “department, name, reason, days, and date”. It may also be is 4, 3, or 1 of these 5 data locations, but the number of tags in the template is always a subset of this set (as shown in the figure below: ); the other is the one that contains loop data For example, to generate an employee information table, this table contains an indeterminate number of employee information, and the information of each employee includes "number, department, name, age, and place of origin". The first template to export word files is relatively simple, and the implementation method of this scheme is described first in this article.

If the picture is not displayed, see: http://blog.51cto.com/11430719/2112360
2. Implementation method

  1. Edit the template: The template must mark the location where the data is to be inserted, so that the program can be used to insert data into the file. Corresponding location, that is to say, in order to generate a file, the location of the data must be marked with some kind of element. To use PageOffice to fill data into word files, you need to use Word bookmarks to mark where you want to insert data. First mark the data locations of "department, name, reason, days, and date" in the Word template: PO_Dept, PO_Name, PO_Cause, PO_Num, PO_Date, as shown in the following figure:
If the picture is not displayed, please refer to: http://blog. 51cto.com/11430719/2112360

The average developer uses Word less and may not know how to insert Word bookmarks. The following is a brief introduction to the method of inserting bookmarks.
  The first method: Position the cursor to the place where the data needs to be marked, click "Insert" - "Bookmark" in the Word menu, a dialog box titled "Bookmark" will pop up (as shown in the figure below), enter a new The name of the bookmark, note: the bookmark name must start with letters, Chinese characters, Chinese punctuation, etc., and can contain numbers but no spaces in the middle (it is not recommended to use Chinese name bookmarks when developing with PageOffice). Click the "Add" button on the right, and the new bookmark name will appear in the list below.
  The second method: select a few texts, or a piece of text, or select a piece of content that contains tables and pictures, assign a bookmark object to the selected content, and then perform the same operation as the first method, "Insert" → "Bookmark" ...
Note: If the new insertion position or the new object uses an existing bookmark name, the original bookmark will be automatically canceled.

  When the template in the PageOffice sample code is made, the second method is used to define bookmarks. Before inserting a bookmark, a marked word will be written in square brackets, such as: [name], and then select "[name]" , and insert a bookmark. The purpose of this is to make it easier to see at a glance when viewing or editing the location of template data.

  In the process of developing with PageOffice, in order to avoid conflicts with user-defined bookmarks, it is required that the name of the inserted bookmarks must start with "PO". Note the letter o, not the number 0. Bookmarks are case-insensitive and can also be written as "po". The data area mentioned in the concept of PageOffice is essentially a bookmark, but only bookmarks starting with "po_" are called data areas, please pay attention to this.

  2. Write the code to call the interface of PageOffice to fill the data into the word file:

copy the code
// declare the variable to store the data read from the database
String docName = "", docDept = "", docCause = "", docNum = "", docDate = "";
// Database data read operation (different databases use different codes)
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 the WordDocument object of PageOffice, operate the Word document
WordDocument doc = new WordDocument();
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);
//Create a PageOfficeCtrl object to open a file
PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz"); // This line must
// get the data object   poCtrl1.setWriter
(doc);
// Open the document poCtrl1.webOpen
("doc/template.doc", OpenModeType.docReadOnly, "Tom"); word 4. Download the example from   http://www.zhuozhengsoft.com/down4/Java/BigDemo/poword.rar, or download the development kit of PageOffice for Java, and check the examples in the development kit: 3.5. Example of leave application








Guess you like

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