public boolean writeWordFile(Map<String,String> writeMap){ boolean flag = false; ActiveXComponent word = null; if(this.wordFile!=null){ try{ //1获得worfFile对象 word = new ActiveXComponent("Word.Application"); Dispatch dispatchTarget = word.getProperty("Documents").toDispatch(); String name = "Open"; int wFlags = Dispatch.Method; //是否进行转换 ConfirmConversions : new Variant(true) //是否只读 :new Variant(false) Object[] oArg = new Object[]{this.wordFile.getPath(),new Variant(true),new Variant(false)}; int[] uArgErr = new int[1]; Dispatch wordfile = Dispatch.invoke(dispatchTarget, name, wFlags, oArg, uArgErr).toDispatch(); //2获得选择器对象 Dispatch vSelection = word.getProperty("Selection").toDispatch(); //3获得表对象 Dispatch tables = Dispatch.get(wordfile, "tables").toDispatch(); //3 for (String loc : writeMap.keySet()) { String[] locs = loc.split(","); int a = Integer.parseInt(locs[0]); int b = Integer.parseInt(locs[1]); int c = Integer.parseInt(locs[2]); try{ //4获得cell Dispatch table = Dispatch.call(tables, "Item", new Variant(a)).toDispatch(); Dispatch cell = Dispatch.call(table, "Cell", new Variant(b),new Variant(c)).toDispatch(); //5获得数据 Dispatch range=Dispatch.get(cell,"Range").toDispatch(); String data=Dispatch.get(range,"Text").getString(); if(data.length()>=2){ data = data.substring(0, data.length()-2); } if(data.length()>0 &&!data.endsWith("\r")){ data=data+"\r"; } //6选中cell,并添加数据 Dispatch.call(cell, "Select"); Dispatch.put(vSelection, "Text", data+writeMap.get(loc)); }catch(Exception e){ continue; } } Dispatch.call(wordfile, "Close", new Variant(-2)); flag = true; }catch(Error e){ if("java.lang.NoClassDefFoundError".equals(e.getClass().getName())){ if("Could not initialize class com.jacob.activeX.ActiveXComponent".equals(e.getMessage())){ LOG.error("word文档中插入意见失败:请将jacob-1.14.3-x86.dll文件放入java.library.path[url=\""+System.getProperty("java.home")+"\\bin\\\"]后重启web服务器"); }else{ LOG.error("相关文档:java后台编辑word文件表格内容的功能开发备忘录、升级说明、使用说明_20141020.doc",e); } }else{ LOG.error("相关文档:java后台编辑word文件表格内容的功能开发备忘录、升级说明、使用说明_20141020.doc",e); } }catch(Exception e){ LOG.error("相关文档:java后台编辑word文件表格内容的功能开发备忘录、升级说明、使用说明_20141020.doc",e); }finally{ if(word!=null){ word.safeRelease(); Dispatch.call(word, "Quit"); } ComThread.Release(); } }else{ System.out.println("initWordFile 办事不利:未生成word文件"); } return flag; }
问题:
运行一次 任务管理器中就多一个winword.exe。
网上有说:ComThread.Release(); word.safeRelease(); 都没用
执行Dispatch.call(word, "Quit");
相关推荐
1.将jacob-1.18.zip下载解压,在文件夹中找到jacob-1.18-x64.dll,jacob-1.18-x86.dll,jacob.jar 2.通过 pom.xml 新增 <groupId>com.hynnet</groupId> <artifactId>jacob <version>1.18 3. 如果是32位系统...
内涵jacob_1.14.3-x64.dll;jacob_1.14.3-64.jdk(maven地址);解决方法:Could not initialize class com.jacob.com.ComThread
java-COM中间件,压缩包里包含jacob-1.14.3.jar、jacob-1.14.3-x64.dll
jacob 1.9语音生成文件,jacob x64.dll和jacob.jar 版本为1.9。 jacob.dll放入 C:\Program Files\Java\jdk1.8.0_121\jre\bin jacob.jar通过idea配置到项目dependencies
jacob-1.18的资源包还有 jacob-1.18-x64.dll和jacob-1.18-x86.dll. 把对应的windows版本扔到 C:\Windows\System32 并添加环境变量大到path中 “%SYSTEMROOT%\System32\jacob-1.18-x86.dll;”或者 “%SYSTEMROOT%\...
jacob.jar1.19及使用方法 解压后获得3个文件 jacob.jar jacob-1.17-x64.dll jacob-1.17-x86.dll jacob.jar 放到项目的lib目录 win7 jacob-1.17-x64.dll 放到C:\Windows\System32 jacob-1.17-x86.dll 放到C:\...
NULL 博文链接:https://liumayulingyan.iteye.com/blog/1900122
JACOB is a JAVA-COM Bridge that allows you to call COM Automation components from Java. It uses JNI to make native calls to the... JACOB runs on x86 and x64 environments supporting 32 bit and 64 bit JVMs
java操作office用到的ActiveXComponent、ComThread、Dispatch、Variant
jacob是文字转语音必不可少的驱动包,此压缩包解压后会看到jacob-1.18文件夹(是1.18版本),文件夹中主要包含jacob.jar、jacob-1.18-x64.dll文件(适合Windows32位系统使用)、jacob-1.18-x86.dll文件(适合Windows...
jacob-1.16.zip java调用word,vb
jacobe 1.6 java操作word jacob-1.16.1.zip
2020最新版jacob 1.20.jar jacob-1.20-x64.dll jacob-1.20-x86.dll
jacob-1.14.3.dll及使用方法
jacob.jar包 与 jacob-1.16.dll jacob.jar包 与 jacob-1.16.dll jacob.jar包 与 jacob-1.16.dll
jacob-1.14.3.jar 包 ,内含有相关资料以及常见错误解决方案 !
com.jacob.jacob_1.10jar包下载