packagecom.env.util;importjava.util.HashMap;importjava.util.Iterator;importjava.util.List;importjava.util.Map;importcom.jacob.activeX.ActiveXComponent;importcom.jacob.com.ComThread;importcom.jacob.com.Dispatch;importcom.jacob.com.Variant;publicclassJavaToword{privatebooleansaveOnExit;/***word文档*/Dispatchdoc=null;/***word运行程序对象*/privateActiveXComponentword;/***所有word文档*/privateDispatchdocuments;privateDispatchselection;/***构造函数*/publicJavaToword(){saveOnExit=true;if(word==null){word=newActiveXComponent(Word.Application);word.setProperty(Visible,newVariant(false));}if(documents==null){documents=word.getProperty(Documents).toDispatch();}}/***打开文件**@paraminputDoc*String要打开的文件,全路径*@returnDispatch打开的文件*/publicDispatchopen(StringinputDoc){returnDispatch.call(documents,Open,inputDoc).toDispatch();}/***选定内容**@returnDispatch选定的范围或插入点*/publicDispatchselect(){returnword.getProperty(Selection).toDispatch();}/***把插入点移动到文件首位置**@paramselection*Dispatch插入点*/publicvoidmoveStart(Dispatchselection){Dispatch.call(selection,HomeKey,newVariant(6));}/***从选定内容或插入点开始查找文本**@paramselection*Dispatch选定内容*@paramtoFindText*String要查找的文本*@returnbooleantrue-查找到并选中该文本,false-未查找到文本*/publicbooleanfind(Dispatchselection,StringtoFindText){//从selection所在位置开始查询Dispatchfind=word.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*Dispatch选定内容*@paramnewText*String替换为文本*/publicvoidreplace(Dispatchselection,StringnewText){//设置替换文本Dispatch.put(selection,Text,newText);}/***全局替换**@paramselection*Dispatch选定内容或起始插入点*@paramoldText*String要替换的文本*@paramnewText*String替换为文本*/publicvoidreplaceAll(Dispatchselection,StringoldText,ObjectreplaceObj){//移动到文件开头moveStart(selection);StringnewText=(String)replaceObj;while(find(selection,oldText)){replace(selection,newText);Dispatch.call(selection,MoveRight);}}/***保存文件**@paramoutputPath*String输出文件(包含路径)*/publicvoidsave(StringoutputPath){Dispatch.call((Dispatch)Dispatch.call(word,WordBasic).getDispatch(),FileSaveAs,outputPath);}/***关闭文件**@paramdocument*Dispatch要关闭的文件*/publicvoidclose(Dispatchdoc){Dispatch.call(doc,Close,newVariant(saveOnExit));}/***根据模板、数据生成word文件**@paraminputPath*String模板文件(包含路径)*@paramoutPath*String输出文件(包含路径)*@paramdata*HashMap数据包(包含要填充的字段、对应的数据)*/publicvoidtoWord(StringinputPath,StringoutPath,HashMapdata){StringoldText;ObjectnewValue;try{doc=open(inputPath);Dispatchselection=select();Iteratorkeys=data.keySet().iterator();while(keys.hasNext()){oldText=(String)keys.next();newValue=data.get(oldText);replaceAll(selection,oldText,newValue);}save(outPath);}catch(Exceptione){System.out.println(操作word文件失败!+e);e.printStackTrace();}finally{if(doc!=null){close(doc);}}}publicvoidprinting(StringinPath,StringoutPath,HashMapmap){toWord(inPath,outPath,map);ActiveXComponentobjWord=newActiveXComponent(Word.Application);DispatchwordObject=(Dispatch)objWord.getObject();Dispatch.put((Dispatch)wordObject,Visible,newVariant(false));Dispatchdocuments=objWord.getProperty(Documents).toDispatch();Dispatchdocument=Dispatch.call(documents,Open,outPath).toDispatch();Dispatch.call(document,PrintOut);Dispatch.call(document,Close,newVariant(false));objWord.invoke(Quit,newVariant[0]);word.invoke(Quit,newVariant[0]);}/////////////////////////////////////////////以下部分为动态导出增加部分/***打开一个word*/publicvoidopenWord(StringinPath){doc=open(inPath);}/***替换模版中对应的字符串*@paramdata*/publicvoidreplaceTem(Mapdata){StringoldText;ObjectnewValue;Dispatchselection=select();Iteratorkeys=data.keySet().iterator();while(keys.hasNext()){oldText=(String)keys.next();newValue=data.get(oldText);replaceAll(selection,oldText,newValue);}}/***保存文件路径*@parampath*/publicvoidsavePath(Stringpath){save(path);if(doc!=null){close(doc);}ComThread.Release();}/***当前表格*/privateDispatchtable;//选定的范围或插入点//privatestaticDispatchselection=null;/***创建新表格*/publicDispatchcreateNewTable(introwCount,intcolCount,intwidth){Dispatchtables=Dispatch.get(doc,Tables).toDispatch();Dispatchrange=Dispatch.get(select(),Range).toDispatch();if(rowCount0&&colCount0){this.table=Dispatch.call(tables,Add,range,newVariant(rowCount),newVariant(colCount),newVariant(width)).toDispatch();}returnthis.table;}/***设置当前表格指定行的背景色**@paramrowIndex*@paramcolor*取值范围0color17默认:16浅灰色1:黑色2:蓝色3:浅蓝...............*/publicvoidsetRowBgColor(introwIndex,intcolor){this.getRow(rowIndex);Dispatchshading=Dispatch.get(row,Shading).toDispatch();if(color16||color1)color=16;Dispatch.put(shading,BackgroundPatternColorIndex,newVariant(color));}/***设置指定表格的指定行的背景色**@paramtableIndex*@paramrowIndex*@paramcolor*取值范围0color17默认:16浅灰色1:黑色2:蓝色3:浅蓝...............*/publicvoidsetRowBgColor(inttableIndex,introwIndex,intcolor){this.getTable(tableIndex);this.setRowBgColor(rowIndex,color);}/****得到当前文档的tables集合*/publicDispatchgetTables()throwsException{if(this.doc==null){thrownewExcep