怎么用freemarker生成的word文档需要另存为一下poi才能读

如题所述

Jacob解决Word文档的读写问题收藏Jacob是Java-COMBridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。Jacob下载的地址为:.Variant;publicclassWordOperate{publicstaticvoidmain(Stringargs[]){ActiveXComponentwordApp=newActiveXComponent("Word.Application");//启动word//Setthevisiblepropertyasrequired.Dispatch.put(wordApp,"Visible",newVariant(true));////设置word可见Dispatchdocs=wordApp.getProperty("Documents").toDispatch();//StringinFile="d:\\test.doc";//Dispatchdoc=Dispatch.invoke(docs,"Open",Dispatch.Method,//newObject[]{inFile,newVariant(false),newVariant(false)},//参数3,false:可写,true:只读//newint[1]).toDispatch();//打开文档Dispatchdocument=Dispatch.call(docs,"Add").toDispatch();//createnewdocumentStringuserName=wordApp.getPropertyAsString("Username");//显示用户信息System.out.println("用户名:"+userName);//文档对齐,字体设置////////////////////////Dispatchselection=Dispatch.get(wordApp,"Selection").toDispatch();Dispatchalign=Dispatch.get(selection,"ParagraphFormat").toDispatch();//行列格式化需要的对象Dispatchfont=Dispatch.get(selection,"Font").toDispatch();//字型格式化需要的对象//标题处理////////////////////////Dispatch.put(align,"Alignment","1");//1:置中2:靠右3:靠左Dispatch.put(font,"Bold","1");//字型租体Dispatch.put(font,"Color","1,0,0,0");//字型颜色红色Dispatch.call(selection,"TypeText","Word文档处理");//写入标题内容Dispatch.call(selection,"TypeParagraph");//空一行段落Dispatch.put(align,"Alignment","3");//1:置中2:靠右3:靠左Dispatch.put(selection,"Text","");Dispatch.call(selection,"MoveDown");//光标标往下一行//表格处理////////////////////////Dispatchtables=Dispatch.get(document,"Tables").toDispatch();Dispatchrange=Dispatch.get(selection,"Range").toDispatch();Dispatchtable1=Dispatch.call(tables,"Add",range,newVariant(3),newVariant(2),newVariant(1)).toDispatch();//设置行数,列数,表格外框宽度//所有表格VarianttableAmount=Dispatch.get(tables,"count");System.out.println(tableAmount);//要填充的表格Dispatcht1=Dispatch.call(tables,"Item",newVariant(1)).toDispatch();Dispatcht1_row=Dispatch.get(t1,"rows").toDispatch();//所有行intt1_rowNum=Dispatch.get(t1_row,"count").getInt();Dispatch.call(Dispatch.get(t1,"columns").toDispatch(),"AutoFit");//自动调整intt1_colNum=Dispatch.get(Dispatch.get(t1,"columns").toDispatch(),"count").getInt();System.out.println(t1_rowNum+""+t1_colNum);for(inti=1;i<=t1_rowNum;i++){for(intj=1;j<=t1_colNum;j++){Dispatchcell=Dispatch.call(t1,"Cell",newVariant(i),newVariant(j)).toDispatch();//行,列Dispatch.call(cell,"Select");Dispatch.put(selection,"Text","cell"+i+j);//写入word的内容Dispatch.put(font,"Bold","0");//字型租体(1:租体0:取消租体)Dispatch.put(font,"Color","1,1,1,0");//字型颜色Dispatch.put(font,"Italic","1");//斜体1:斜体0:取消斜体Dispatch.put(font,"Underline","1");//下划线DispatchRange=Dispatch.get(cell,"Range").toDispatch();StringcellContent=Dispatch.get(Range,"Text").toString();System.out.println((cellContent.substring(0,cellContent.length()-1)).trim());}Dispatch.call(selection,"MoveDown");//光标往下一行(才不会输入盖过上一输入位置)}//合并单元格////////////////////////Dispatch.put(selection,"Text","");Dispatch.call(selection,"MoveDown");//光标标往下一行Dispatchrange2=Dispatch.get(selection,"Range").toDispatch();Dispatchtable2=Dispatch.call(tables,"Add",range2,newVariant(8),newVariant(4),newVariant(1)).toDispatch();//设置行数,列数,表格外框宽度Dispatcht2=Dispatch.call(tables,"Item",newVariant(2)).toDispatch();DispatchbeginCell=Dispatch.call(t2,"Cell",newVariant(1),newVariant(1)).toDispatch();DispatchendCell=Dispatch.call(t2,"Cell",newVariant(4),newVariant(4)).toDispatch();Dispatch.call(beginCell,"Merge",endCell);for(introw=1;row<=Dispatch.get(Dispatch.get(t2,"rows").toDispatch(),"count").getInt();row++){for(intcol=1;col<=Dispatch.get(Dispatch.get(t2,"columns").toDispatch(),"count").getInt();col++){if(row==1){Dispatchcell=Dispatch.call(t2,"Cell",newVariant(1),newVariant(1)).toDispatch();//行,列Dispatch.call(cell,"Select");Dispatch.put(font,"Color","1,1,1,0");//字型颜色Dispatch.put(selection,"Text","mergeCell!");}else{Dispatchcell=Dispatch.call(t2,"Cell",newVariant(row),newVariant(col)).toDispatch();//行,列Dispatch.call(cell,"Select");Dispatch.put(font,"Color","1,1,1,0");//字型颜色Dispatch.put(selection,"Text","cell"+row+col);}}Dispatch.call(selection,"MoveDown");}//Dispatch.call(selection,"MoveRight",newVariant(1),newVariant(1));//取消选择//Objectcontent=Dispatch.get(doc,"Content").toDispatch();//Word文档内容查找及替换////////////////////////Dispatch.call(selection,"TypeParagraph");//空一行段落Dispatch.put(align,"Alignment","3");//1:置中2:靠右3:靠左Dispatch.put(font,"Color",0);Dispatch.put(selection,"Text","欢迎,Hello,world!");Dispatch.call(selection,"HomeKey",newVariant(6));//移到开头Dispatchfind=Dispatch.call(selection,"Find").toDispatch();//获得Find组件Dispatch.put(find,"Text","hello");//查找字符串"hello"Dispatch.put(find,"Forward","True");//向前查找//Dispatch.put(find,"Format","True");//设置格式Dispatch.put(find,"MatchCase","false");//大小写匹配Dispatch.put(find,"MatchWholeWord","True");//全字匹配Dispatch.call(find,"Execute");//执行查询Dispatch.put(selection,"Text","你好");//替换为"你好"//使用方法传入的参数parameter调用word文档中的MyWordMacro宏////Dispatch.call(document,macroName,parameter);//Dispatch.invoke(document,macroName,Dispatch.Method,parameter,newint[1]);//页眉,页脚处理////////////////////////DispatchActiveWindow=wordApp.getProperty("ActiveWindow").toDispatch();DispatchActivePane=Dispatch.get(ActiveWindow,"ActivePane").toDispatch();DispatchView=Dispatch.get(ActivePane,"View").toDispatch();Dispatch.put(View,"SeekView","9");//9是设置页眉Dispatch.put(align,"Alignment","1");//置中Dispatch.put(selection,"Text","这里是页眉");//初始化时间Dispatch.put(View,"SeekView","10");//10是设置页脚Dispatch.put(align,"Alignment","2");//靠右Dispatch.put(selection,"Text","这里是页脚");//初始化从1开始//书签处理(打开文档时处理)//////////////////////////DispatchactiveDocument=wordApp.getProperty("ActiveDocument").toDispatch();DispatchbookMarks=Dispatch.call(document,"Bookmarks").toDispatch();booleanisExist=Dispatch.call(bookMarks,"Exists","bookMark1").getBoolean();if(isExist==true){DispatchrangeItem1=Dispatch.call(bookMarks,"Item","bookMark1").toDispatch();Dispatchrange1=Dispatch.call(rangeItem1,"Range").toDispatch();Dispatch.put(range1,"Text",newVariant("当前是书签1的文本信息!"));StringbookMark1Value=Dispatch.get(range1,"Text").toString();System.out.println(bookMark1Value);}else{System.out.println("当前书签不存在,重新建立!");Dispatch.call(bookMarks,"Add","bookMark1",selection);DispatchrangeItem1=Dispatch.call(bookMarks,"Item","bookMark1").toDispatch();Dispatchrange1=Dispatch.call(rangeItem1,"Range").toDispatch();Dispatch.put(range1,"Text",newVariant("当前是书签1的文本信息!"));StringbookMark1Value=Dispatch.get(range1,"Text").toString();System.out.println(bookMark1Value);}//保存操作////////////////////////Dispatch.call(document,"SaveAs","D:/wordOperate.doc");//Dispatch.invoke((Dispatch)doc,"SaveAs",Dispatch.Method,newObject[]{htmlPath,newVariant(8)},newint[1]);//生成html文件//0=wdDoNotSaveChanges//-1=wdSaveChanges//-2=wdPromptToSaveChanges//Dispatch.call(document,"Close",newVariant(0));////worddoc.olefunction("protect",2,true,"");////DispatchbookMarks=wordApp.call(docs,"Bookmarks").toDispatch();////System.out.println("bookmarks"+bookMarks.getProgramId());////Dispatch.call(doc,"Save");//保存////Dispatch.call(doc,"Close",newVariant(true));////wordApp.invoke("Quit",newVariant[]{});//wordApp.safeRelease();//Finalizerscallthismethod}}
温馨提示:答案为网友推荐,仅供参考
相似回答