[Reproduced] Java read and write word documents, the perfect solution

In the process of doing a project, it is often necessary to read the data in the data, process it, and output it in word format. I found a lot of solutions on the Internet, but they are not ideal. I occasionally found PageOffice, a domestic Office plug-in, which is very simple to develop and call! It is much easier to use than the poi, jacob and other solutions introduced on the Internet! Although there are not as many functional interfaces as poi and jacob, they meet 80 to 90% of general requirements, and it is not as troublesome to distinguish between 07 format and 03 format as poi does.

The following is the introduction of Baidu Encyclopedia PageOffice:

  https://baike.baidu.com/item/PageOffice/2737741?fr=aladdin

The basic features of PageOffice include:

  Open, edit, print preview, print Word, Excel, PowerPoint and other Office documents in web pages.
  Document concurrency control mechanism.
  Provides the necessary functions of the OA document module such as Word revision traces, handwritten comments, circle reading, keyboard comments, and electronic seals.
  Fill and export data, including text, pictures, tables, etc., to the specified location in Word and Excel templates dynamically according to the database, and can dynamically specify various formats of the content.
  Extract the content at the specified location in the Word and Excel documents, including text, pictures (not supported by Excel), tables, etc., and save them to the database.
  I won't go into detail about other functions...

For more interface descriptions, refer to the API of the PageOffice official website: http://www.zhuozhengsoft.com/help/java3/index.html

Download address: http://www.zhuozhengsoft.com/dowm/ 

After downloading the PageOffice development kit, copy the Samples4 folder to the Webapps directory of Tomcat, and visit: http://localhost:8080/Samples4/index.html to see a list of all official sample effects. Focus on the following examples:  

  1. 11. A simple example of assigning a value to a data region (DataRegion) in
  a Word document 1. 17. A simple example of assigning a value to a Table in a Word document
  1. 18. Filling the Word file with text data using DataTag
  2.6 , Background programming to insert Word files into the data area
  2. 7. Background programming to insert pictures into the data area
  2. 8. Background programming to insert Excel files into the data area
  2. 9. Add watermarks to Word documents
  2. 10. Use data tags (DataTag ) Fill the Word file with formatted data
  2. 11. Dynamically create a data area in Word

We can write a simple program to test the effect:

copy code
    PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
    poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz");

    WordDocument worddoc = new WordDocument();
    //First manually insert bookmarks at the location where the word file is to be inserted, bookmarks must be prefixed with "PO_"
    //Assign a value to DataRegion, the value is in the form of: "plain text content, [word]word file path[/word], [image]image path[/image]"
    DataRegion data1 = worddoc.openDataRegion("PO_p1");
    data1.setValue("test string");//Plain text content
    DataRegion data2 = worddoc.openDataRegion("PO_p2");
    data2.setValue("[word]doc/2.doc[/word]");//Insert word file
    DataRegion data3 = worddoc.openDataRegion("PO_p3");
    data3.setValue("[image]doc/1.jpg[/image]");//Insert picture

    //Open the file and fill in the data
    poCtrl1.setWriter(worddoc);
    poCtrl1.webOpen("doc/template.doc", OpenModeType.docNormalEdit, "用户名");
copy code

  The code is simple. In just a few sentences, the function of inserting text, pictures, and sub-Word files can be completed! However, if you are doing a project, you can define a word template first, which is conducive to typesetting and reduces the workload of writing code. You can also call the interface provided by PageOffice to control the style of the data area in the template, but the coding workload will increase. Its functions are far more than these, and it can be dealt with according to specific needs.

Guess you like

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