Recently done projects, you need to export some information to Word. On the internet to find a lot of solutions, now will be a summary of these days to share.
- For now, Java exports Word has roughly 6 solutions:
1:jacob is the abbreviation for Java-com Bridge, which builds a bridge between Java and Microsoft's COM components. Using Jacob's own DLL dynamic link library, and through the way of JNI implementation of the Java platform on the COM program calls. DLL dynamic-link library generation requires support from the Windows platform. This scenario is only implemented on the Windows platform and is its limitation.
2:apache POI includes a series of APIs that can manipulate various format files based on the Microsoft OLE 2 Compound document format, which can be used to 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, cannot set style.
3:java2word is a component (class library) that calls the MS Office Word document in a Java program. The component provides a simple set of interfaces so that the Java program invokes 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 at a bookmark, inserting a picture, inserting a table, and so on. Populating data into tables reading tabular data, version 1.1 enhanced features: Specify a text style, specify a table style. This way, you can dynamically compose Word documents. is a good solution.
4:itext is a well-known open source site SourceForge a project and is a Java class library for generating PDF documents. By Itext, you can not only generate PDF or RTF documents, but also convert XML and HTML files into PDF files. Powerful.
5:jsp output style, the scheme is simple to implement, but the processing style is a bit flawed, simple export can be used.
6: It's easy to do it with XML. Word supports XML format from 2003, the general idea is to use office2003 or 2007 to edit the word style, and then save as XML, the XML is translated into a freemarker template, Finally, we use Java to parse the Freemarker template and output doc. A Word document that has been tested in such a way is fully compliant with Office standards, has a convenient style, content control, and does not distort printing, and the resulting document is exactly the same as editing a document in office.
To synthesize the reference of the above information, and some comments on the net, finally I chose, the 6th kind uses the XML to do the export scheme.
- Here's a basic example to implement a simple word export:
To export the contents of the Word template, start the phonetic section as part of the code that you want to replace.
:
Then save Word as an. xml file, open the file, find the title to modify it to ${title}, and replace it with the following. Then change the. xml file suffix to. FTL, and import the. FTL template file to the specified directory. Load the jar package Freemarker.jar. Start writing 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 Ten Public static void Main (string[] args) { One wordtest test = new Wordtest (); A Test.createword (); - } - the Public void Createword () { -Map<String, Object>Datamap=new HashMap<String, Object>(); - GetData (DATAMAP); - configuration.setclassfortemplateloading (This.getclass (), "");//path where template files are located + Template T=null; - try { + t = configuration.gettemplate ("Test. FTL");//Get template file A } catch (IOException e) { at E.printstacktrace (); - } - file OutFile = new file ("D:/outfile" +math.random () *10000+ ". Doc");//Export Files - Writer out = null; - try { - out = new BufferedWriter (new OutputStreamWriter (New FileOutputStream (OutFile))); in } catch (FileNotFoundException E1) { - E1.printstacktrace (); to } + - try { the t.process (DataMap, out); Fill in the fill data into the template file and output to the destination file * } catch (Templateexception e) { $ E.printstacktrace (); Panax Notoginseng } catch (IOException e) { - E.printstacktrace (); the } + } A theprivate void GetData (Map<String, Object>DataMap) { + datamap.put ("title", "title"); - datamap.put ("Nian", "2016"); $ datamap.put ("Yue", "3"); $ datamap.put ("Ri", "6"); - datamap.put ("Shenheren", "LC"); - theList<Map<string,object>> list = new ArrayList<Map<string,object>> (); -for (int i = 0; i<Ten; i++) { Wuyi Map<string,object>Map = new HashMap<String, Object>(); the map.put ("Xuehao", I); - map.put ("Neirong", "content", +i); Wu list.add (map); - } About $ - datamap.put ("list", list); - } -}
Modify the. ftl file, locate the list, and add the list that you want to add to the file. In front of the list, join < #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 to precede the name to be exported with L. such as Xuehao, modified to L.xuehao. This is a bit like the use of an El expression.
Java Export Build Word