[Original]java 5 ways to export word

Source: Internet
Author: User

Find a lot of days on the Internet solution that exports information from the database to Word, now share the summary of these days. Overall, Java exports Word has roughly 5 solutions:

1:jacob is the abbreviation for Java-com Bridge, which builds a bridge between Java and Microsoft's COM components. The call to Microsoft Office's COM interface on the Java platform was implemented through Jacob.

Pros: Call Microsoft Office's COM interface to generate the Word file format specification.

Disadvantage: 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 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.

Pros: Support for Windows, UNIX, and Linux across platforms.

Disadvantage: Relative to the Word file processing, POI is more suitable for Excel processing, for Word to implement some simple file operation is good, cannot set the style and the resulting Word file format is not enough specification.

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.

Advantages: Simple enough, more simple to operate than freemarker.

Cons: No freemarker powerful, not able to generate Word documents according to the template, Word's document style and other information can not be very good operation.

4:freemarker the ability to generate Word documents is implemented by Xml+freemarker. Save the Word file as XML, insert a special string placeholder into the XML file, translate the XML into a freemarker template, and finally parse the Freemarker template with Java, encode calls Freemarker implement the text substitution and output doc.

Pros: More powerful than Java2word, but also pure Java programming.

Disadvantage: The generated file is essentially XML, not the real Word file format, there are many commonly used word format can not be processed or behave strange, such as: Hyperlink, newline, garbled, some of the generated files do not open and so on.

5:pageoffice generates a Word file. The Pageoffice encapsulates Microsoft Office's cumbersome VBA interface, providing simple and easy-to-use Java programming objects that support the generation of Word files while simultaneously editing Word documents online and reading Word document content.

Pros: Cross-platform support for Windows, UNIX and Linux, generate Word file format standards, support text, pictures, tables, fonts, paragraphs, colors, hyperlinks, headers and other formats of operations, support multiple word merge, no need to handle concurrency, no server resources, run stable.

Disadvantage: You must generate files on the client side (you can not display the interface) and do not support pure server-side makefile.

Comprehensive information on the reference, and some comments on the Internet, and finally selected, the 5th kind of using Pageoffice to do the export program.

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

Read the information in a database table fill in the template for a leave bar, replacing the text content of the "department, name, Reason, days, date" data location.
  

Main code:

    //declaring variables to store data read from the databaseString DocName = "", Docdept = "", Doccause = "", DocNum = "", Docdate = ""; //database Data read operations (different databases with 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 a Pageoffice worddocument object, manipulate the Word fileWorddocument doc =Newworddocument (); 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); //To create a Pageofficectrl object Open filePageofficectrl POCTRL1 =NewPageofficectrl (Request); Poctrl1.setserverpage (Request.getcontextpath ()+ "/poserver.zz");//This line must//Get Data ObjectPoctrl1.setwriter (DOC); //Open DocumentPoctrl1.webopen ("Doc/template.doc", Openmodetype.docreadonly, "Tom");

The resulting file effect:

  

[Original]java 5 ways to export word

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.