Java2Word,用来生成word文档的,它的api文档就使用一个Document类就可以了,Document类里面的函数都有中文说明,而且还有几个examples教你怎么使用。
解压缩Java2Word.rar后,有一个Doc目录,打开里面的index.html就可以看到中文的api文档了,只要看Document就可以了。
Java2Word资料下载地址:http://download.youkuaiyun.com/detail/lypf19900912/8411415
前面的博客中讲到使用FreeMarker来到处word文档,其实这个Java2Word也是能够到处Word文档的,能够生成表格并将数据插入,操作起来要比FreeMarker简单的多,但是他没有FreeMarker强大。他不能够根据模版生成Word文档,word的文档的样式等信息都不能够很好的操作,他的特点是足够简单。所以这个工具适合于我们生成简单的Word文档。如果word文档的要求比较多,这个就不能够满足了。
注意在使用的时候要安装Java2Word1.1+Install.exe这个应用程序。
将Jar包放在 WEB-INFO的lib下面
实现的几个示例代码
public class test {
public test() {
Document doc = null;
try {
doc = new Document();
doc.open("e:/test.doc");
doc.insert("第一章:宪政概论","biaoti1");
doc.insertAtBookmark("testbookm","第一章:宪政概论","标题1");
doc.insertAtBookmark("testbookm",
new java.io.File("C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg"));
}
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();
}
package com.pf.word;
import com.heavenlake.wordapi.Document;
import java.util.List;
public class testReadTable {
public testReadTable() {
Document doc = null;
try {
doc = new Document();
doc.open("e:/test1.doc");
List tableData = doc.readTable(1, 1);
for (int i = 0; i < tableData.size(); i++) {
List rowData = (List) tableData.get(i);
for (int j = 0; j < rowData.size(); j++) {
System.out.print("|" + rowData.get(j));
}
System.out.println("|");
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if(doc!=null) doc.close(false);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
testReadTable testReadTable1 = new testReadTable();
}
}