[Original] 5 ways to export word from java

I have been looking for a solution to export the information in the database to word for many days on the Internet, and now I will share the summary of these days. In general, there are roughly 5 solutions for java to export word:

1: Jacob is the abbreviation of Java-COM Bridge, which builds a bridge between Java and Microsoft's COM components. The COM interface of Microsoft Office is called on the Java platform through Jacob.

  Advantages: Calling the COM interface of Microsoft Office, the generated word file format specification.

  Disadvantages: The server can only be a windows platform, does not support unix and linux, and Microsoft Office must be installed on the server.

2: Apache POI includes a series of APIs, which can operate various format files based on MicroSoft OLE 2 Compound Document Format, and can read and write Excel, Word and other files in Java through these APIs.

  Advantages: Cross-platform support for windows, unix and linux.

  Disadvantages: Compared with the processing of word files, POI is more suitable for excel processing. For word, the operation of some simple files is improvised, the style cannot be set, and the format of the generated word file is not standardized.

3: Java2word is a component (class library) that calls MS Office Word documents in a java program. This component provides a set of simple interfaces, so that the java program can call its service to operate the Word document. These services include: opening a document, creating a new document, finding text, replacing text, inserting text, inserting pictures, inserting tables, inserting text at bookmarks, inserting pictures, inserting tables, etc.

  Advantages: Simple enough, much simpler to operate than FreeMarker.

  Disadvantages: Not as powerful as FreeMarker, it cannot generate Word documents according to templates, and the style and other information of word documents cannot be operated well.

4: The function of FreeMarker to generate word documents is realized by XML+FreeMarker. First save the word file as xml, insert special string placeholders in the xml file, translate the xml into a FreeMarker template, and finally use java to parse the FreeMarker template, encode and call FreeMarker to implement text replacement and output Doc.

  Advantages: It is more powerful than Java2word, and it is also pure Java programming.

  Disadvantages: The generated files are essentially xml, not the real word file format. There are many common word formats that cannot be processed or behave strangely, such as page numbers, hyperlinks, page headers, garbled characters, and partially generated files that cannot be opened.

5: PageOffice generates a word file. It encapsulates the cumbersome vba interface of Microsoft Office, and provides a simple and powerful pure server-side Java programming object. While editing Office documents online, it can also easily and efficiently read and write the content of Office documents.

  Advantages: cross-platform support for windows, unix and linux, generate word file format standards, support operations in various formats such as text, pictures, tables, fonts, paragraphs, colors, hyperlinks, headers, etc., support multi-word merging, no need to deal with concurrency , does not consume server resources, and runs stably.

  Disadvantages: The file must be generated on the client side (the interface may not be displayed), and the pure server-side generation of files is not supported.

Based on the reference of the above materials and some opinions on the Internet, I finally chose the fifth option to use PageOffice as an export solution.

Here's a basic example to achieve a simple word export:

  Read the information records in the database table and fill it into a template of leave slip, and replace the text content of several data positions of "department, name, reason, days, date".
  

  Main code:

    // Declare variables to store data read from the database 
    String docName = "", docDept = "", docCause = "", docNum = "", docDate = "" ;
     // Database data read operations (different databases use different code) 
    ResultSet rs = stmt.executeQuery("select * from leaveRecord where ID = " + id);
     if (rs.next()) {
        docName = rs.getString("Name");
        docDept = rs.getString("Dept");
        docCause = rs.getString("Cause");
        docNum = rs.getString("Num");
        docDate = rs.getString("SubmitTime");
    }
    rs.close();
    // Create the WordDocument object of PageOffice, operate the Word file 
    WordDocument doc = new WordDocument();
    doc.openDataRegion("PO_name").setValue(docName);
    doc.openDataRegion("PO_dept").setValue(docDept);
    doc.openDataRegion("PO_cause").setValue(docCause);
    doc.openDataRegion("PO_num").setValue(docNum);
    doc.openDataRegion( "PO_date" ).setValue(docDate);
     // Create a PageOfficeCtrl object to open the file 
    PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
    poCtrl1.setServerPage(request.getContextPath() +"/poserver.zz"); // This line must
     // get the data object 
    poCtrl1.setWriter(doc);
     // Open the document 
    poCtrl1.webOpen("doc/template.doc" , OpenModeType.docReadOnly, "Tom");

  Generated file effects:

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325240822&siteId=291194637