java calls PageOffice to generate word

1. In the development of OA office or document-related Web systems, it is inevitable to encounter the need to dynamically generate word documents. In order to solve the need for exporting word documents at work, I found some information online some time ago, and exported them in word. There are many tools available for this, jacob, poi, java2word, itext. Jacob requires that the server must be a windows operating system, and office software must be installed on the server, so give up decisively! Poi needs to write different codes for the doc and docx formats, which increases the complexity of the program. java2doc is the encapsulation of jacob, also give up! Finally, I chose to use itext to export word. Some information on the Internet said that the function of itext to export word is too simple. After the trial, the package tool class is really convenient to use, but the exported word document is difficult to meet the requirements in terms of details, such as: Font, line spacing, font spacing, first line indent, etc.

2. Because the word exported by itext has various small problems, we can only find other solutions. After a friend's recommendation, the PageOffice component can be used to export the word, and the effect can be perfectly consistent with the word document required by the user. , and the programming interface is simple, and the call is very convenient. In general, it is to make a template (use bookmarks starting with PO_ as data placeholders), then programmatically call the PageOffice interface to replace the placeholders in the template with real data, and finally generate word. Here's an example:

1. Make a template

  Open the word template to insert bookmarks: PO_Dept, PO_Name, PO_Cause, PO_Num, PO_Date, as shown in the following figure

  

2. Write the code

  Call the PageOffice interface to fill the word template with data to generate a word document:

  

copy code

1 // Declare the variable to store the data read from the database
 2     String  docName = "", docDept = "", docCause = "", docNum = "", docDate = "";
 3 // Database data read operation (different databases use different codes)
 4     ResultSet rs = stmt.executeQuery("select * from leaveRecord where ID = " + id);
 5     if (rs.next()) {
 6         docName = rs.getString("Name");
 7         docDept = rs.getString("Dept");
 8         docCause = rs.getString("Cause");
 9         docNum = rs.getString("Num");
10         docDate = rs.getString("SubmitTime");
11     }
12     rs.close();
13 //Create the WordDocument object of PageOffice, operate the Word file
14     WordDocument doc = new WordDocument();
15     doc.openDataRegion("PO_name").setValue(docName);
16     doc.openDataRegion("PO_dept").setValue(docDept);
17     doc.openDataRegion("PO_cause").setValue(docCause);
18     doc.openDataRegion("PO_num").setValue(docNum);
19     doc.openDataRegion("PO_date").setValue(docDate);
20 //Create a PageOfficeCtrl object to open the file
21     PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
22     poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz"); //此行必须
23 //Get the data object
24     poCtrl1.setWriter(doc);
25 // Open the document
26     poCtrl1.webOpen("doc/template.doc", OpenModeType.docReadOnly, "Tom");

copy code

3. Generate renderings

  

  

3. Source code download

  https://download.csdn.net/download/zi_wu_xian/10399345

Guess you like

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