服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java程序把word文档直接转换成html文件














  jacob是java和windows下的com桥,通过它我们可以在java程序中调用com组件。如果你的jdk是1.4,那你需要下载jacob1.9的jni库才能正常运行,早期版本在jdk1.4下有些问题。

package com; 
/**
* 〈p〉title:word文档转html类〈/p〉
* 〈p〉description: 〈/p〉
* 〈p〉copyright:() 2002〈/p〉
* @author 舵手
* @version 1.0
*/
import com.jacob.com.*; 
import com.jacob.activex.*; 

public class wordtohtml {
/**
*文档转换函数
*@param docfile word文档的绝对路径加文件名(包含扩展名)
*@param htmlfile 转换后的html文件绝对路径和文件名(不含扩展名)
*/
public static void change(string docfile, string htmlfile) {
activexcomponent app = new activexcomponent("word.application"); // 启动word
try {
app.setproperty("visible", new variant(false)); 
//设置word不可见
object docs = app.getproperty("documents").todispatch(); 
object doc = dispatch.invoke(docs,"open",dispatch.method,new object[] 
{ docfile, new variant(false),new variant(true) }, new int[1]).todispatch(); 
// 打开word文件
dispatch.invoke(doc, "saveas", dispatch.method, new object[] {htmlfile,
 new variant(8) }, new int[1]); 
// 作为html格式保存到临时文件
variant f = new variant(false); 
dispatch.call(doc, "close", f); 
} catch (exception e) {
e.printstacktrace(); 
} finally {
app.invoke("quit", new variant[]{}); 
}
}
public static void main(string[] strs){
wordtohtml.change("c:\\a\\运输管理调度系统总体方案.doc", "c:\\a\\t"); 

}
}

扫描关注微信公众号