Solutions for generating and printing word documents in Java

Source: Internet
Author: User

Users require the use of programs to generate standard word documents that can be printed and cannot be deformed. Many solutions have been used before, and the sacrifices are extremely fierce at customers' strict requirements.

It's okay for POI to read Word documents. It's not flattering to write documents. Complicated styles are hard to control. Imagine a 20-plus page. The word documents nested with many tables and images rely on POI to write code and output, it is worse for programmers than coal mining in Shanxi province. Besides, the document format is often changed.

It is okay to operate Excel on iText. It is also a nightmare for a large number of complicated words.

The output style through JSP is basically not up to standard, and printing is even worse.

Word supports XML format starting from 2003. It is easy to use XML.

The general idea is to first edit the word style with office2003 or 2007, then save it as xml, translate the xml into the FreeMarker template, and finally parse the FreeMarker template with java and output the Doc. It is tested that the word documents generated in this way fully comply with the office standards. Style and content control is very convenient, and printing is not distorted. The generated documents are exactly the same as the edited documents in the office.

See the actual results:

First, use office [Version 2003 or above, the following does not support xml format] to edit the document style. The red line in the figure is the part I want to output:

Save the edited document as XML

Use Firstobject free XML editor [use of Firstobject free XML editor can be found here] to mark the place where data needs to be filled in xml with FreeMarker. [for the syntax of FreeMarker, see here]

Last generated Document Style

Main program code:

Package com.havenliu.doc ument; import java. io. bufferedWriter; import java. io. file; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. outputStreamWriter; import java. io. writer; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import freemarker. template. configuration; import freemarker. template. temp Late; import freemarker. template. templateException; public class implements enthandler {private Configuration configuration = null; public implements enthandler () {configuration = new Configuration (); configuration. setDefaultEncoding ("UTF-8");} public void createDoc () {// Map dataMap = new HashMap (); getData (dataMap ); // Set the method and path of the module. FreeMarker supports multiple Template loading methods. You can reload servlet, classpath, and database tutorials. // Our template is stored in com.havenliu.doc ument. configuration under the template package. setClassForTemplateLoading (this. getClass (), "/com/havenliu/document/template"); Template t = null; try {// test. ftl is the template t = configuration to be loaded. getTemplate ("test. ftl ");} catch (IOException e) {e. printStackTrace ();} // output File path and name File outFile = new File ("D:/temp/outFile.doc"); Writer out = null; try {out = new BufferedWriter (ne W OutputStreamWriter (new FileOutputStream (outFile);} catch (FileNotFoundException e1) {e1.printStackTrace ();} try {t. process (dataMap, out);} catch (TemplateException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}}/*** note that the Key value of the data stored in dataMap must correspond to the parameter in the template * @ param dataMap */private void getData (Map dataMap) {dataMap. put ("author", "Zhang San"); dataMap. put ("remark", "This is the test remarks"); Lis T _ table1 = new ArrayList (); Table1 t1 = new Table1 (); t1.setDate ("2010-10-1"); t1.setText ("formulated the development plan for October. "); _ Table1.add (t1); Table1 t2 = new Table1 (); t2.setDate (" 2010-10-2 "); t2.setText (" meeting to discuss development plans "); _ table1.add (t2 ); dataMap. put ("table1", _ table1); List _ table2 = new ArrayList (); for (int I = 0; I <5; I ++) {Table2 _ t2 = new Table2 (); _ t2.setDetail ("test development plan" + I); _ t2.setPerson ("zhang san --" + I ); _ t2.setBegindate ("2010-10-1"); _ t2.setFinishdate ("2010-10-31"); _ t2.setRemark ("remarks"); _ table2.add (_ t2);} dataMap. put ("table2", _ table2 );}}

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.