1. Demand Background
In some special applications, the client wants to generate the document on the server and fill in the data at the same time, the client's page does not display the open document, but the document generation on the server puts a lot of pressure on the server. Currently, the first way to generate documents on the server is jacob , But it is limited to the windows platform, and many JAVA programs often run on other operating systems, so the program is not discussed here. The second is POI. However, its excel processing is not bad, the word module is still limited to reading the text content of word, and the function of writing word files is even weaker; there is also a terrible place, the classes that handle doc format and docx format are almost completely different. Separately write different codes for different formats, which means that if the docx format file uploaded by the user uses the doc extension, the program will crash immediately. Moreover, I personally think that the structure of poi is chaotic, the coding is more complicated, and the development process is very time-consuming and energy-consuming. PageOffice provides the FileMakerCtrl component. FileMakerCtrl generates documents on the client side and uploads them to the server, but does not display word documents on the web page. Therefore, using FileMakerCtrl to generate word files has two advantages: 1. Generating word documents on the client side does not It will cause any pressure on the server; 2. The generated document belongs to the standard word document format.
2. Core code
1. Make a template, open the word template file, and insert bookmarks in the file: PO_company, PO_year, PO_number, as shown in the following figure:
2. Dynamically populate the word document and upload it to the server
FileMakerCtrl fmCtrl = new FileMakerCtrl(request); //Set the service page of PageOffice fmCtrl.setServerPage(request.getContextPath()+"/poserver.zz"); WordDocument doc = new WordDocument(); //Assign a value to the data area, that is, fill the data to the corresponding position in the template doc.openDataRegion("PO_company").setValue("Beijing XX Software Co., Ltd."); doc.openDataRegion("PO_year").setValue("2018"); doc.openDataRegion("PO_number").setValue("008"); fmCtrl.setWriter(doc); //Set the action method or RequestMapping method used to receive the generated document fmCtrl.setSaveFilePage("/SaveFile"); fmCtrl.fillDocument("doc/template.doc", DocumentOpenType.Word);
3. Write the code of the action or RequestMapping method pointed to by SaveFilePage to receive the file stream uploaded by the client
FileSaver fs = new FileSaver(request, response); String fileName = "myfile.doc" ; fs.saveToFile(request.getSession().getServletContext().getRealPath("/") +"/"+ fileName); fs.close();
4. The generated word file effect.
3. Related downloads
Visit the following address to download the PageOffice for Java development kit, and refer to the example after deploying the Samples4 example: 2. 13. FileMaker converts a single document (take Word as an example)
http://www.zhuozhengsoft.com/dowm/