java 操作 word文档的问题

jiangeb 2007-05-17 05:29:51
我下了一些代码,是java操作word的内容,调试的时候总出错. 除了下面的问题,还想问,jacbo1.7是不是必须配合jdk1.3使用,我的jdk是1.4

ActiveXComponent component = new ActiveXComponent("Word.Application");
String inFile = "c:\\t.doc";
String tpFile = "c:\\t.htm";
String otFile = "d:\\t.xml";
boolean flag = false;
try {
component.setProperty("Visible", new Variant(false));
Dispatch wordacc = component.getProperty("document. ").toDispatch();
//请问上面这句是什么意思呀!
Dispatch wordfile = Dispatch.invoke(wordacc,"Open", Dispatch.Method,
new Object[]{inFile,new Variant(false), new Variant(true)},
new int[1] ).toDispatch();
//这句又是什么意思
Dispatch.invoke(wordfile,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
Variant f = new Variant(false);
Dispatch.call(wordfile, "Close", f);
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
component.invoke("Quit", new Variant[] {});
}
...全文
261 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ymhtt 2008-03-04
  • 打赏
  • 举报
回复
不清楚 帮忙顶一下
凉凉二点凉 2007-05-17
  • 打赏
  • 举报
回复
E:\\222.192.55.17\\ 这个是保存文件的文件夹呵呵
凉凉二点凉 2007-05-17
  • 打赏
  • 举报
回复
不是jdk版本的问题!我的jacob弄丢了不知道什么版本!
这是操作word并转换为txt文件的,你看看
package com.szallcom.tools;
import com.jacob.com.*;
import com.jacob.activeX.*;
import java.io.*;

//取得指定目录下面所有的doc文件名称
public class WordToTxt
{
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
public void change(String paths, String savepaths)
{

File d = new File(paths);
//取得当前文件夹下所有文件和目录的列表
File lists[] = d.listFiles();
//String pathss = new String("");

//对当前目录下面所有文件进行检索
for(int i = 0; i < lists.length; i ++)
{
if(lists[i].isFile())
{
String filename = lists[i].getName();
String filetype = new String("");
//取得文件类型
filetype = filename.substring((filename.length() - 3), filename.length());

//判断是否为doc文件
if(filetype.equals("doc"))
{
System.out.println("当前正在转换......");
//打印当前目录路径
System.out.println(paths);
//打印doc文件名
System.out.println(filename.substring(0, (filename.length() - 4)));

ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word

String docpath = paths + filename;
String htmlpath = savepaths + filename.substring(0, (filename.length() - 4));

String inFile = docpath;
//要转换的word文件
String tpFile = htmlpath;
//HTML文件

boolean flag = false;

try
{
app.setProperty("Visible", new Variant(false));
//设置word不可见
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();
//打开word文件
Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(2)}, new int[1]);
//作为html格式保存到临时文件
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
flag = true;
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
app.invoke("Quit", new Variant[] {});
}
System.out.println("转化完毕!");
}
}
else
{
// pathss = paths;
//进入下一级目录
// pathss = pathss + lists[i].getName() + "";
//递归遍历所有目录
// change(pathss, savepaths);
}
}

}
//------------------------------------------------------------------------------
//方法原型: main(String[] args)
//功能描述: main文件
//输入参数: 无
//输出参数: 无
//返 回 值: 无
//其它说明: 无
//------------------------------------------------------------------------------
/*public static void main(String[] args)
{

String paths = new String("E:\\222.192.55.17\\");
String savepaths = new String ("E:\\222.192.55.17\\");
WordToTxt a=new WordToTxt();
a.change(paths, savepaths);



}*/
}

happylion2008 2007-05-17
  • 打赏
  • 举报
回复
帮顶!JDK1.4也比较老了。

81,114

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧