1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Jacob 导出word文档 资源无法正常释放 解决方法

Jacob 导出word文档 资源无法正常释放 解决方法

时间:2022-06-01 21:20:28

相关推荐

Jacob 导出word文档 资源无法正常释放 解决方法

操作word文档失败!.ComFailException:Can'tmapnametodispid:Open -03-1715:11:41,812WARN[org.apache.struts.action.RequestProcessor]-<UnhandledExceptionthrown:class.ComFailException> -3-1715:11:41org.apache.catalina.core.StandardWrapperValveinvoke 严重:Servlet.service()forservletactionthrewexception .ComFailException:Can'tmapnametodispid:Quit经过多次调试,发现内存消耗严重。查看任务管理器,有多个word进程积压未关闭。

发现原因:第一次调用完后,虽然word已经被关闭。但装载word的ActiveXComponent并没有被关闭。

所以,加入管理com组件的线程,在关闭word之后,关闭线程。

修改后的代码如下:publicclassJacobWord{ /** *打开文件 *@paramdocuments *@paraminputDocPath *@return */privateDispatchopen(Dispatchdocuments,StringinputDocPath){returnDispatch.call(documents,"Open",inputDocPath).toDispatch(); } /** *选定内容 *@paramword *@return */privateDispatchselect(ActiveXComponentword){returnword.getProperty("Selection").toDispatch(); } /** *把插入点移动到文件首位置 *@paramselection */privatevoidmoveStart(Dispatchselection){ Dispatch.call(selection,"HomeKey",newVariant(6)); } /** *从选定内容或插入点开始查找文本 *@paramselection选定内容 *@paramtoFindText要查找的文本 *@returntrue:查找到并选中该文本;false:未查找到文本。 */privatebooleanfind(Dispatchselection,StringtoFindText){ //从selection所在位置开始查询 Dispatchfind=Dispatch.call(selection,"Find").toDispatch(); //设置要查找的内容 Dispatch.put(find,"Text",toFindText); //向前查找 Dispatch.put(find,"Forward","True"); //设置格式 Dispatch.put(find,"format","True"); //大小写匹配 Dispatch.put(find,"MatchCase","True"); //全字匹配 Dispatch.put(find,"MatchWholeWord","True"); //查找并选中returnDispatch.call(find,"Execute").getBoolean(); } /** *把选定内容替换为设定文本 *@paramselection *@paramnewText */privatevoidreplace(Dispatchselection,StringnewText){ Dispatch.put(selection,"Text",newText); } /** *全局替换 *@paramselection *@paramoldText *@paramreplaceObj */privatevoidreplaceAll(Dispatchselection,StringoldText,ObjectreplaceObj){ moveStart(selection); StringnewText=(String)replaceObj;while(find(selection,oldText)){ replace(selection,newText); Dispatch.call(selection,"MoveRight"); } } /** *打印 *@paramdocument */privatevoidprint(Dispatchdocument){ Dispatch.call(document,"PrintOut"); } /** *保存文件 *@paramword *@paramoutputPath */privatevoidsave(ActiveXComponentword,StringoutputPath){ Dispatch.call(Dispatch.call(word,"WordBasic").getDispatch(),"FileSaveAs",outputPath); } /** *关闭文件 *@paramdoc */privatevoidclose(Dispatchdoc){ Dispatch.call(doc,"Close",newVariant(true)); } /** *保存打印doc文档 *@paraminputDocPath *@paramoutPutDocPath *@paramdata *@paramisPrint */publicvoidsaveDoc(StringinputDocPath,StringoutPutDocPath,HashMapdata,booleanisPrint){ //初始化com的线程 ComThread.InitSTA(); //word运行程序对象 ActiveXComponentword=newActiveXComponent("Word.Application"); //文档对象 DispatchwordObject=(Dispatch)word.getObject(); //设置属性Variant(true)表示word应用程序可见 Dispatch.put((Dispatch)wordObject,"Visible",newVariant(false)); //word所有文档 Dispatchdocuments=word.getProperty("Documents").toDispatch(); //打开文档 Dispatchdocument=this.open(documents,inputDocPath); Dispatchselection=this.select(word); Iteratorkeys=data.keySet().iterator(); StringoldText; ObjectnewValue;while(keys.hasNext()){ oldText=(String)keys.next(); newValue=data.get(oldText);this.replaceAll(selection,oldText,newValue); } //是否打印if(isPrint){this.print(document); }this.save(word,outPutDocPath);this.close(document); word.invoke("Quit",newVariant[0]); //关闭com的线程 ComThread.Release(); } }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。