java exports a simple way to generate word

  • 2020-05-05 11:21:48
  • OfStack

For a recent project, you need to export some information to word. I have found many solutions on the Internet. Now I will share the summary of these days.

Currently, there are roughly six solutions for java exporting word:
1. Jacob, short for Java-COM Bridge, Bridges the gap between Java and Microsoft's COM components. Jacob's DLL dynamic link library is used, and JNI is used to invoke COM program on Java platform. The generation of DLL dynamic link libraries requires support from the windows platform. This solution can only be implemented on windows platform, which is its limitation.

2. Apache POI includes a series of API, which can operate on MicroSoft OLE 2 Compound Document Format files in a variety of formats. Its excel processing is powerful, and word is limited to reading, so it can only perform some operations on simple files, and cannot set styles.

3. Java2word is a component (class library) that invokes MS Office Word documents in java programs. This component provides a simple set of interfaces for an java program to invoke its service operation Word document. These services include: open documents, create new documents, find text, replace text, insert text, insert pictures, insert tables, insert text in bookmarks, insert pictures, insert tables, etc. Populating data into tables to read table data, 1.1 enhancements: specify text style, specify table style. In this way, word documents can be dynamically formatted. It's a good solution.

4. iText is a project of the famous open source site sourceforge, which is an java class library for generating PDF documents. iText can not only generate PDF or rtf documents, but also convert XML and Html files into PDF files. Powerful.

5, JSP output style, the solution is simple to implement, but the handling style is a bit defective, simple export can be used.

6. It's easy to do with XML. Word has supported XML since 2003. The general idea is to edit word with office2003 or 2007, save xml, translate xml into FreeMarker template, and finally use java to parse FreeMarker template and output Doc. After testing, the word document generated in this way fully conforms to the office standard. The style and content control is very convenient, and the printing will not be deformed. The resulting document is exactly the same as the document edited in office.

Based on the reference of the above materials and some comments on the Internet, I finally chose the sixth export scheme with xml.

Here is a basic example of to implement a simple word export:
The contents of the word template to be exported by
, starting the pinyin section as the section to be replaced in the code. :

Then save word as.xml, open the file, find title and change it to ${title}. Then change the.xml file suffix to.ftl and import the.ftl template file to the specified directory. Load 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 of the template file 
    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"); // The 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 the template file with the fill data and outputs 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", " The 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 to locate the list and add the list to the file. Add < to the list #list list as l > (put an < on its head #list your collection name is as xxxx> ) and add < at the end /#list > . Modify the list content by adding l. Before the name to be output. If xuehao, change to l.xuehao. This is a bit like using the EL expression.

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


Related articles: