用jarcob:import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;public class WordTest
{
public static void main(String[] args)
{
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Word.Application");
try
{
app.setProperty("Visible", new Variant(false));
Object documents = app.getProperty("Documents").toDispatch();
Object document = Dispatch.call(documents, "Open", "D:\\test.doc").toDispatch();
Object content = Dispatch.get(document, "Content").toDispatch();
Dispatch.invoke(document,"SaveAs", Dispatch.Method, new Object[]{"D:\\test.html",new Variant(8)}, new int[1]);//作为html格式保存到临时文件 Variant text = Dispatch.get(content, "Text"); //取出文件的文本内容
System.out.println(text.toString());
Dispatch.call(document, "Close", new Variant(false));
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
app.invoke("Quit", new Variant[0]);
ComThread.Release();
}
}
}
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;public class WordTest
{
public static void main(String[] args)
{
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Word.Application");
try
{
app.setProperty("Visible", new Variant(false));
Object documents = app.getProperty("Documents").toDispatch();
Object document = Dispatch.call(documents, "Open", "D:\\test.doc").toDispatch();
Object content = Dispatch.get(document, "Content").toDispatch();
Dispatch.invoke(document,"SaveAs", Dispatch.Method, new Object[]{"D:\\test.html",new Variant(8)}, new int[1]);//作为html格式保存到临时文件 Variant text = Dispatch.get(content, "Text"); //取出文件的文本内容
System.out.println(text.toString());
Dispatch.call(document, "Close", new Variant(false));
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
app.invoke("Quit", new Variant[0]);
ComThread.Release();
}
}
}
http://www.csdn.net/develop/Read_Article.asp?Id=15887
http://www.csdn.net/develop/Read_Article.asp?Id=19247