For projects recently, you need to export some information to word. I have found many solutions online, and now I will share the summary of these days.
At present, there are roughly 6 solutions for exporting word from Java:
1. Jacob is the abbreviation of Java-COM Bridge, which builds a bridge between Java and Microsoft's COM components. Use the DLL dynamic link library that comes with Jacob, and the call to COM programs on the Java platform is realized through JNI. The generation of DLL dynamic link library requires support from the Windows platform. This solution can only be implemented on the Windows platform, which is its limitation.
2. Apache POI includes a series of APIs that can operate various format files based on Microsoft OLE 2 Compound Document Format. Through these APIs, you can read and write Excel, Word and other files in Java. Its Excel processing is very powerful, and its word is limited to reading. Currently, it can only implement some simple file operations and cannot set styles.
3. Java2word is a component (class library) that calls MS Office Word documents in a Java program. This component provides a simple set of interfaces for a Java program to call its services to operate Word documents. These services include: opening a document, creating a new document, finding text, replacing text, inserting text, inserting pictures, inserting tables, inserting text in bookmarks, inserting pictures, inserting tables, etc. Fill data into a table to read table data. Version 1.1 enhanced features: Specify text styles, specify table styles. In this way, word documents can be dynamically typed. It's a good solution.
4. iText is a famous open source site sourceforge project, a java class library used to generate PDF documents. Through 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. This solution is simple to implement, but the handling style is a bit flawed, and simple export can be used.
6. It's very simple to do it in XML. Word supports XML format since 2003. The general idea is to first use office2003 or 2007 to edit the word style, then save as xml, translate the xml into a FreeMarker template, and finally use java to parse the FreeMarker template and output Doc. The word documents generated in this way are tested fully comply with the Office standards, and the style and content control is very convenient, and the printing will not be deformed. The generated documents are exactly the same as those edited in Office.
Based on the references of the above information and some online opinions, I finally chose the 6th export solution using xml.
Here are the basic examples to implement a simple word export:
To export the content of the word template, the pinyin part is the part to be replaced in the code type. :
Then save word as .xml file, open the file, find the title and modify it to ${title}, and replace the content to be replaced in turn. 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:
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(), "");//The path where the template file is located 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); //Fill the fill data into the template file and output it to the target file} catch (TemplateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private 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"); List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); for (int i = 0; i < 10; 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, find the location of the list, and add the list to the file. Add <#list list as l> to its header (add a <#list your collection name as xxxx>) and add </#list> to the end. Modify the list content and add l. to the name to be output. For example, xuehao, modify it to l.xuehao. This is a bit like the use of EL expressions.
The above is all about this article, I hope it will be helpful to everyone's learning.