Reproduced Java dynamically populates Word documents and uploads them to the server

Source: Internet
Author: User

I. Background of demand

In some special applications, customers want to generate documents on the server and populate the data, the client's page does not display open documents, but the server generated documents on the server pressure is very large, the current server generated documents the first is the way is Jacob, but limited to the Windows platform, Often many Java programs run on other operating systems, and this scenario is not discussed here. The second is the POI. But its Excel processing does not work, the word module is also limited to read Word's text content, the ability to write Word files is weaker; and there's a hell of a place where the doc format and the classes that process the docx format are almost completely different, to write different code for different formats separately. This means that the user uploads the docx format file if the doc extension is used, the program immediately crashes. And personally think that the POI structure confusion, coding is more complex, the development process is very time-consuming and energy. Pageoffice provides a Filemakerctrl component that Filemakerctrl generates documents on the client and uploads them to the server, but does not display the Word document in the Web page. Therefore, the use of Filemakerctrl to generate Word files has two advantages: 1. Generates a Word document on the client without any stress to the server; 2. The resulting document is in the standard Word document format.

Second, the core code

1. Make the template, open the Word template file, insert the bookmark in the file: Po_company, Po_year, Po_number, as shown:

  

2. Dynamically populate Word documents and upload them to the server

    Filemakerctrl Fmctrl = new Filemakerctrl (request);    Set Pageoffice Service page    fmctrl.setserverpage (Request.getcontextpath () + "/poserver.zz");        Worddocument doc = new worddocument ();    Assigns a value to the data region, that is, the data is populated to the corresponding position in the template    doc.opendataregion ("Po_company"). SetValue ("A Beijing Software Co., Ltd.  ");    Doc.opendataregion ("Po_year"). SetValue ("2018");    Doc.opendataregion ("Po_number"). SetValue ("008");    Fmctrl.setwriter (DOC);    Sets the action method or Requestmapping method    fmctrl.setsavefilepage ("/savefile") used to receive the generated document;     Fmctrl.filldocument ("Doc/template.doc", Documentopentype.word);

3. Write the code for the action or Requestmapping method pointed to by the 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 resulting Word file effect.

  

Third, related download

Visit the address below to download the Pageoffice for Java development package, after deploying the Samples4 sample, refer to the example: two, 13, FileMaker Convert a single document (in Word, for example)

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

Reproduced Java dynamically populates Word documents and uploads them to the server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.