jacob简单调用word,execl(二)

1,jacob调用word

public static void main(String [] args){
	String paths = new String("e:\\xjy\\jcob-test\\temp\\");
	String savePaths = new String("e:\\xjy\\jcob-test\\newTemp\\");
	change(paths,savePaths);
}
/**
         * 将word文档转换成HTML文档
	 * @author 
	 * @version V1.0      
	 * @param paths
	 * @param savePaths
*/
public static void change(String paths ,String savePaths){
	File file = new File(paths);
	File lists[] = file.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());	
			if(fileType.equals("doc")){
				System.out.println(paths);
				System.out.println(fileName.substring(0, (fileName.length()-4)));
				ActiveXComponent app = new ActiveXComponent("Word.Application");
				String docPath = paths + fileName;
				String htmlPath = savePaths + fileName.substring(0, fileName.length()-4);
				String inFile = paths + "\\" + fileName;
				String tpFile = htmlPath ;
				boolean flag = false;
				try{
					app.setProperty("Visible", new Variant(false));
					Object docs = app.getProperty("Documents").toDispatch();
					Object doc = Dispatch.invoke((Dispatch) docs, "Open", Dispatch.Method, new Object[]{inFile}, new int[1]).toDispatch();
					System.out.println(tpFile);
					Dispatch.invoke((Dispatch)doc, "SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
					Variant f = new Variant(false);
					Dispatch.invoke((Dispatch)doc, "Close", Dispatch.Method, new Object[]{new Variant(false)}, new int[1]);
					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);
		}
	}
}

这个是网上找的例子,已经测试成功。

2,jacob调用execl

public static void main(String[] args) {
        ActiveXComponent xl = new ActiveXComponent("Excel.Application");
        Dispatch xlo = (Dispatch) (xl.getObject());
        try {
            System.out.println("version=" + xl.getProperty("Version"));
            System.out.println("version=" + Dispatch.get(xlo, "Version"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            xl.invoke("Quit", new Variant[] {});
        }
    }

3,jacob调用系统声音

public static void main(String[] args) {
		ActiveXComponent sap = new ActiveXComponent("SAPI.SpLexicon.1");
		Dispatch sapo = sap.getObject();
		boolean flag = false;
		try {
			// 调整音量和读的速度
			sap.setProperty("Volume", new Variant(100));
			sap.setProperty("Rate", new Variant(0));
			// 这一句是读出来abc这三个字母的
			Dispatch.call(sapo, "Speak", new Variant("abc"));
			flag = true;
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		} finally {
			if (flag) {
				System.out.println("Over!");
			} else
				System.out.println("Application end with exception!");
		}
	}
未成功。…………


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值