Perfect solution for reading and writing Word documents in java
2794 people read comments (2) collect reports
I am working on a project recently. I need to read the data in the data and process the data in the word format.
Type output. I have been searching for these days and have not found a good solution! I occasionally found java2word, a package developed by Chinese people. It's very easy to use today! Compared with those introduced on the Internet
Poi, jacob and other solutions are much easier to get started! There are not many functions, but they should meet general requirements. Unfortunately, the 07 format is not supported. If you need to read and write the 07 format, use
The openxml4j package is not described here.
The following is an introduction to java2word:
Java2word is a component (Class Library) that calls the MS Office Word document in a java program ). This component provides a set of simple interfaces for java programs to call their service operation Word documents.
These services include:
Open a document, create a document,
Search for and replace text,
Insert text, images, and tables,
Insert text, images, and tables in bookmarks.
Fill data in the table
Read table data
Enhanced features of Version 1.1:
Specify text styles and Table Styles. In this way, the Word documents can be dynamically formatted.
When filling in table data, you can specify the row and column from which to fill. With the input data size, you can modify any part of the table or even only the content of one cell.
Merge cells.
For more exciting functions, see the following details:
Http://www.heavenlake.com/java2word/doc
:
Http://dev.heavenlake.com: 81/developer/viewthread? Thread = 24
After completing the package, let's look at the first program:
Import com. heavenlake. wordapi. Document;
Public class test {
Public test (){
Document doc = null;
Try {
Doc = new Document ();
Doc. open ("e:/test.doc"); // open the file
Doc. insert ("Chapter 1: Introduction to constitutional government", "biaoti1"); // insert text
Doc. insertAtBookmark ("testbookm", "Chapter 1: Introduction to constitutional government", "Title 1 ");
Doc. insertAtBookmark ("testbookm ",
New java. io. File ("E:/Photo/Changbai Mountains/DSC00663.JPG"); // insert an image
}
Catch (Exception e ){
E. printStackTrace ();
}
Finally {
Try {
If (doc! = Null) doc. close (true );
}
Catch (Exception e ){
E. printStackTrace ();
}
}
}
Public static void main (String [] args ){
Test test1 = new test ();
}
}
The code is very simple. Insert text, images, and titles in just a few words! However, to implement projects, it is best to define a word template first, which is conducive to typographical layout. You can also call the template style. Of course, its functions are far from pervasive. You can refer to its api documentation. It is worth noting that the output word does not support the 07 format. It's very simple, because there are Chinese comments, haha!