[reproduced] java operation word (1)

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 cyclic 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 the word file is relatively simple, and the implementation method of this scheme is described first in this article.

  

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 insert the data into the corresponding location of the file, that is to say, to generate the file, the location of the data must be marked with some 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 several data locations of "department, name, reason, days, date" in the Word template: PO_Dept, PO_Name, PO_Cause, PO_Num, PO_Date, as shown in the following figure:

  
  Ordinary developers use less Word, 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 the bookmark starting with "po_" is called a data area, please pay attention to this.

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

copy code
    // declare variable to store data read from database
    String  docName = "", docDept = "", docCause = "", docNum = "", docDate = "";
    // Database data read operations (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 file
    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 the file
    PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
    poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz"); //此行必须
    //get the data object
    poCtrl1.setWriter(doc);
    // open the document
    poCtrl1.webOpen("doc/template.doc", OpenModeType.docReadOnly, "Tom");
copy code

 

Third, the effect of the generated file

  

4. Sample download

  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 request

Guess you like

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