Export and generate word in java, and export word in java

Source: Internet
Author: User

Export and generate word in java, and export word in java

For a recent project, you need to export some information to word. I have found many solutions on the Internet. I will share my summary over the past few days.

  • Currently, there are roughly six solutions for java to export word:

1: Jacob is short for Java-COM Bridge. It builds a Bridge between Java and Microsoft COM components. Use the DLL dynamic link library that comes with Jacob and call the COM program on the Java platform through JNI. DLL dynamic link library generation must be supported by the windows platform. This solution can only be implemented on the windows platform and is a limitation.

2: Apache POI includes a series of APIs that can be used to operate various Format files based on MicroSoft OLE 2 Compound Document Format. These APIs can be used to read and write files such as Excel and Word in Java. He is very powerful in excel processing and is limited to reading word. Currently, he can only perform operations on some simple files and cannot set styles.

3: Java2word is a component (Class Library) that calls the MS Office Word document in a java program ). This component provides a set of simple interfaces for java programs to call their service operation Word documents. These services include: open a document, create a document, search for text, replace text, insert text, insert pictures, insert tables, insert text, insert pictures, insert tables, and so on. Fill data into the table to read table data. Enhanced features in version 1.1: Specify text styles and Table Styles. In this way, the Word documents can be dynamically formatted. Is a good solution.

4: iText is a well-known open source site sourceforge project. It is a java class library used to generate PDF documents. Using iText, you can not only generate PDF or rtf documents, but also convert XML and Html files into PDF files. Powerful functions.

5: JSP output style. This solution is simple to implement, but it is somewhat defective to handle the style. You can use it for simple export.

6: it is easy to use XML. Word supports XML format from 2003. The general idea is to edit the word style with office2003 or 2007, save it as xml, and translate xml into a FreeMarker template, finally, we use java to parse the FreeMarker template 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.

Based on the reference of the above materials and some comments on the Internet, I finally chose 6th export schemes using xml.

  • Below is a basic example to achieve simple word export:

  For the content of the word template to be exported, start the pinyin part to be replaced in the code.

:

Save word as a. xml file, open the file, find the title, change it to $ {title}, and replace the content to be replaced in sequence. Change the suffix of the. xml file to. ftl, and import the. ftl template file to the specified directory. Load the jar package freemarker. jar. Start to write the code:

  • Main Code:
1 public class WordTest {2 3 private Configuration configuration = null; 4 5 public WordTest () {6 configuration = new Configuration (); 7 configuration. setDefaultEncoding ("UTF-8"); 8} 9 10 public static void main (String [] args) {11 WordTest test = new WordTest (); 12 test. createWord (); 13} 14 15 public void createWord () {16 Map <String, Object> dataMap = new HashMap <String, Object> (); 17 getData (dataMap ); 18 configuration. setClassForTemplateLoading (this. getClass (), ""); // path of the Template file 19 Template t = null; 20 try {21 t = configuration. getTemplate ("test. ftl "); // get template file 22} catch (IOException e) {23 e. printStackTrace (); 24} 25 File outFile = new File ("D:/outFile" + Math. random () * 10000 + ". doc "); // export file 26 Writer out = null; 27 try {28 out = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (outFile ))); 29} catch (FileNotFoundException e1) {30 e1.printStackTrace (); 31} 32 33 try {34 t. process (dataMap, out); // fill in the filled data in the template file and output it to the target file 35} catch (TemplateException e) {36 e. printStackTrace (); 37} catch (IOException e) {38 e. printStackTrace (); 39} 40} 41 42 private void getData (Map <String, Object> dataMap) {43 dataMap. put ("title", "title"); 44 dataMap. put ("nian", "2016"); 45 dataMap. put ("yue", "3"); 46 dataMap. put ("ri", "6"); 47 dataMap. put ("shenheren", "lc"); 48 49 List <Map <String, Object> list = new ArrayList <Map <String, Object> (); 50 for (int I = 0; I <10; I ++) {51 Map <String, Object> map = new HashMap <String, Object> (); 52 map. put ("xuehao", I); 53 map. put ("neirong", "content" + I); 54 list. add (map); 55} 56 57 58 dataMap. put ("list", list); 59} 60}

Modify the. ftl file, locate the location of the List, and add the list to the file. Add <# list as l> before the list (add a <# list your set name as xxxx> on its header), and add </# list> at the end. Modify the list content and add l. To the name to be output .. For example, change xuehao to l. xuehao. This is a bit like the use of EL expressions.

  

    

 

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.