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