一.通过 java 流读取 Word 内容
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadWordByStream {
public static void main(String[] args) throws IOException {
String rowContent = new String();
String content = new String();
BufferedReader in = new BufferedReader(new FileReader("d:\test3.doc"));
while ((rowContent = in.readLine()) != null) {
content = content + rowContent + "";
}
System.out.println(content.getBytes());
System.out.println(new String(content.getBytes(),"utf-8"));//因为编码方式不同,不容易解析
in.close();
}
}
二.通过 Jacob 读取 Word 内容
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class WordReader {
public static void main(String args[]) {
ComThread.InitSTA();// 初始化 com 的线程
ActiveXComponent wordApp = new ActiveXComponent("Word.Application"); // 启动 word
// Set the visible property as required.
Dispatch.put(wordApp, "Visible", new Variant(true));// //设置 word 可见
Dispatch docs = wordApp.getProperty("Documents").toDispatch();//所有文档窗口
// String inFile = "d:\test.doc";
// Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,
// new Object[] { inFile, new Variant(false),new Variant(false) },//参数3,false:可写,true:只读
// new int[1]).toDispatch();//打开文档
Dispatch doc = Dispatch.call(docs, "Add").toDispatch(); //创建一个新文档
Dispatch wordContent = Dispatch.get(doc, "Content").toDispatch(); //取得 word 文件的内容
Dispatch font = Dispatch.get(wordContent, "Font").toDispatch();
Dispatch.put(font, "Bold", new Variant(true)); // 设置为粗体
Dispatch.put(font, "Italic", new Variant(true)); // 设置为斜体
Dispatch.put(font, "Underline", new Variant(true));
Dispatch.put(font, "Name", new Variant("宋体"));
Dispatch.put(font, "Size", new Variant(14));
for(int i=0;i<10;i++){//作为一个段落
Dispatch.call(wordContent, "InsertAfter", "current paragraph"+i+" ");
}