1. 程式人生 > >jacob簡單呼叫word,execl(二)

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!");
		}
	}
未成功。…………