Java export a simple way to generate word _java

Source: Internet
Author: User

Recently done projects, you need to export some information to Word. Find a lot of solutions on the Internet, now the summary of these days to share.

For now, Java Export Word has roughly 6 solutions:
1, Jacob is the abbreviation of Java-com Bridge, it is in Java and Microsoft's COM component between the building a bridges. Using a DLL dynamic link library with the Jacob's, and using JNI to implement the call to COM programs on the Java platform. The build of DLL dynamic link libraries requires support from the Windows platform. This scenario can only be implemented on the Windows platform and is its limitation.

2. The Apache POI includes a series of APIs that manipulate various format files based on the Microsoft OLE 2 compound document format, through which you can read and write files such as Excel, Word, and so on in Java. His Excel processing is very powerful, for word also limited to read, currently can only implement some simple file operation, can not set style.

3. Java2word is a component (class library) that invokes MS Office Word documents in a Java program. This component provides a simple set of interfaces for the Java program to invoke his service to manipulate the Word document. These services include opening a document, creating a new document, finding text, replacing text, inserting text, inserting a picture, inserting a table, inserting text into a bookmark, inserting a picture, inserting a table, and so on. Fill data into a table read tabular data, version 1.1 enhanced features: Specify text styles, specify table styles. In this way, you can compose Word documents dynamically. is a good solution.

4, Itext is a well-known open source site SourceForge a project, is used to generate a PDF document of a Java class library. Not only can you generate PDF or RTF documents through Itext, but you can convert XML and HTML files to PDF files. function is powerful.

5, JSP output style, the scheme to achieve simple, but the processing style is somewhat flawed, simple export can be used.

6. It's easy to do with XML. Word supports XML format from 2003, with the general idea of using office2003 or 2007 to edit the style of Word, then save as XML, and translate XML into Freemarker templates. Finally, we use Java to parse the Freemarker template and output doc. The Word document that was tested in this way is fully compliant with the Office standards, and style, content control is convenient, printing is not distorted, the resulting document is exactly the same as the edited document in office.

Comprehensive reference to the above information, and some comments on the Internet, finally I chose, the 6th type of XML to do the export scheme.

Here is a basic example to implement a simple word export:
the content of the Word template that you want to export, and the Start Phonetic section is the part you want to replace in code. :

Then save Word as an. xml file, open the file, find the title to modify it to ${title}, and replace it with the following. The. xml file suffix is then changed to. FTL, and the. FTL template file is imported to the specified directory. Load Jar Pack Freemarker.jar. Start writing code:

Main code:

public class Wordtest {private Configuration Configuration = null; 
    Public Wordtest () {configuration = new configuration (); 
  Configuration.setdefaultencoding ("UTF-8"); 
    public static void Main (string[] args) {wordtest test = new Wordtest (); 
  Test.createword (); 
    public void Createword () {map<string,object> datamap=new hashmap<string,object> (); 
    GetData (DATAMAP); 
    Configuration.setclassfortemplateloading (This.getclass (), "");//template file path Template t=null; 
    try {t = configuration.gettemplate ("Test. FTL");//Get template file} catch (IOException e) {e.printstacktrace (); File outfile = new file ("D:/outfile" +math.random () *10000+ ". Doc"); 
    Export file Writer out = null; 
    try {out = new BufferedWriter (new OutputStreamWriter (New FileOutputStream (outfile)); 
    catch (FileNotFoundException E1) {e1.printstacktrace (); try {t.process (datamap, out); Fills in the template file with the fill data and outputs to the target file} catch (Templateexception e) {e.printstacktrace (); 
    catch (IOException e) {e.printstacktrace (); 
    } private void GetData (map<string, object> datamap) {datamap.put ("title", "caption"); 
    Datamap.put ("Nian", "2016"); 
    Datamap.put ("Yue", "3");  
    Datamap.put ("Ri", "6"); 
     
    Datamap.put ("Shenheren", "LC"); 
    list<map<string,object>> list = new arraylist<map<string,object>> (); 
      for (int i = 0; i < i++) {map<string,object> Map = new hashmap<string,object> (); 
      Map.put ("Xuehao", I); 
      Map.put ("Neirong", "Content" +i); 
    List.add (map); 
  } datamap.put ("list", list); } 
}

Modify the. ftl file, locate the list, and add the list that you want to add to the file. Before the list, add the < #list list as l> (add a < #list your collection name as xxxx> on its head) and add </#list > at the end. Modify the list contents and precede the name you want to export with L. such as Xuehao, modified to L.xuehao. This is a bit like using an El expression.

The above is the entire content of this article, I hope to help you learn.

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.