您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > java jacob 操作word 文档,进行写操作
目录第一部分Word文档自动追加表格内容.............................................................................2第二部分javajacob操作word文档,进行写操作,如生成表格,添加图片.......3第三部分.................................................................................................................................28第一部分Word文档自动追加表格内容今天在做一个自动化生成SDD文档的小工具,通过Word的模板,前台通过Flex填入数据,最后将Word文档填写好。以下是一部分代码,关于表格自动追加表格内容Java代码1.importjava.util.ArrayList;2.3.importcom.heavenlake.wordapi.Document;4.5.publicclasstest6.{7.publictest()8.{9.Documentdoc=null;10.try{11.doc=newDocument();12.doc.newDoc(e:/test4.doc);13.doc.insertln(测试文档);14.doc.insertln(天池软件产品列表);15.//doc.saveAs(e:/test2.doc);16.ArrayListtableData=newArrayList(3);17.String[]title={18.序号,代码,名称,网址};19.tableData.add(title);20.String[]field1={21.1,751002,天池软件自动化生产平台,};22.tableData.add(field1);23.String[]field2={24.2,751004,产品数据管理系统,pdm.heavenlake.com};25.tableData.add(field2);26.27.doc.insert(tableData,流行型);28.29.doc.insertln();30.31.32.ArrayListtableData1=newArrayList(3);33.34.String[]field3={35.1,751002,天池软件自动化生产平台,};36.tableData1.add(field3);37.String[]field4={38.2,751004,产品数据管理系统,pdm.heavenlake.com};39.tableData1.add(field4);40.doc.replaceTable(1,4,tableData1);41.42.}catch(Exceptione)43.{44.e.printStackTrace();45.}finally46.{47.try48.{49.if(doc!=null)50.doc.close(true);51.}catch(Exceptione)52.{53.e.printStackTrace();54.}55.56.}57.58.}59.60.publicstaticvoidmain(String[]args)61.{62.testtest1=newtest();63.}64.65.}第二部分javajacob操作word文档,进行写操作,如生成表格,添加图片jacob-1.15-M3.zipjacob-1.15-M3-x86.dllcopy到c://windows/system32引入jacob.jar示例代码viewplain1.importjava.io.File;2.importcom.jacob.activeX.ActiveXComponent;3.importcom.jacob.com.Dispatch;4.importcom.jacob.com.Variant;5.classWordBean{6.//代表一个word程序7.privateActiveXComponentMsWordApp=null;8.//代表进行处理的word文档9.privateDispatchdocument=null;10.publicWordBean(){11.//OpenWordifwe/'venotdoneitalready12.if(MsWordApp==null){13.MsWordApp=newActiveXComponent(Word.Application);14.}15.}16.//设置是否在前台打开word程序,17.publicvoidsetVisible(booleanvisible){18.MsWordApp.setProperty(Visible,newVariant(visible));19.//这一句作用相同20.//Dispatch.put(MsWordApp,Visible,newVariant(visible));21.}22.//创建一个新文档23.publicvoidcreateNewDocument(){24.//FindtheDocumentscollectionobjectmaintainedbyWord25.//documents表示word的所有文档窗口,(word是多文档应用程序)26.Dispatchdocuments=Dispatch.get(MsWordApp,Documents).toDispatch();27.//CalltheAddmethodoftheDocumentscollectiontocreate28.//anewdocumenttoedit29.document=Dispatch.call(documents,Add).toDispatch();30.}31.//打开一个存在的word文档,并用document引用引用它32.publicvoidopenFile(StringwordFilePath){33.//FindtheDocumentscollectionobjectmaintainedbyWord34.//documents表示word的所有文档窗口,(word是多文档应用程序)35.Dispatchdocuments=Dispatch.get(MsWordApp,Documents).toDispatch();36.document=Dispatch.call(documents,Open,wordFilePath,37.newVariant(true)/*是否进行转换ConfirmConversions*/,38.newVariant(false)/*是否只读*/).toDispatch();39.//document=Dispatch.invoke(documents,Open,Dispatch.Method,40.//newObject[]{wordFilePath,newVariant(true),41.//newVariant(false)42.//},newint[1]).toDispatch();43.}44.//向document中插入文本内容45.publicvoidinsertText(StringtextToInsert){46.//GetthecurrentselectionwithinWordatthemoment.47.//anewdocumenthasjustbeencreatedthenthiswillbeat48.//thetopofthenewdoc获得选中的内容,如果是一个新创建的文件,因里面无内容,则光标应处于文件开头处49.Dispatchselection=Dispatch.get(MsWordApp,Selection).toDispatch();50.//取消选中,应该就是移动光标,否则新添加的内容会覆盖选中的内容51.Dispatch.call(selection,MoveRight,newVariant(1),newVariant(1));52.//Putthespecifiedtextattheinsertionpoint53.Dispatch.put(selection,Text,textToInsert);54.//取消选中,应该就是移动光标55.Dispatch.call(selection,MoveRight,newVariant(1),newVariant(1));56.}57.//向文档中添加一个图片,58.publicvoidinsertJpeg(StringjpegFilePath){59.Dispatchselection=Dispatch.get(MsWordApp,Selection).toDispatch();60.Dispatchimage=Dispatch.get(selection,InLineShapes).toDispatch();61.Dispatch.call(image,AddPicture,jpegFilePath);62.}63.//段落的处理,插入格式化的文本64.publicvoidinsertFormatStr(Stringtext){65.DispatchwordContent=Dispatch.get(document,Content).toDispatch();//取得word文件的内容66.Dispatch.call(wordContent,InsertAfter,text);//插入一个段落到最后67.Dispatchparagraphs=Dispatch.get(wordContent,Paragraphs)68..toDispatch();//所有段落69.intparagraphCount=Dispatch.get(paragraphs,Count).changeType(70.Variant.VariantInt).getInt();//一共的段落数71.//找到刚输入的段落,设置格式72.DispatchlastParagraph=Dispatch.call(paragraphs,Item,73.newVariant(paragraphCount)).toDispatch();//最后一段(也就是刚插入的)74.//Range对象表示文档中的一个连续范围,由一个起始字符位置和一个终止字符位置定义75.DispatchlastParagraphRange=Dispatch.get(lastParagraph,Range)76..toDispatch();77.Dispatchfont=Dispatch.get(lastParagraphRange,Font).toDispatch();78.Dispatch.put(font,Bold,newVariant(true));//设置为黑体79.Dispatch.put(font,Italic,newVariant(true));//设置为斜体80.Dispatch.put(font,Name,newVariant(宋体));//81.Dispatch.put(font,Size,newVariant(12));//小四82.Dispatchselection=Dispatch.get(MsWordApp,Selection).toDispatch();83.Dispatch.call(selection,TypeParagraph);//插入一个空行84.Dispatchalignment=Dispatch.get(selection,ParagraphFormat)85..toDispatch();//段落格式86.Dispatch.put(a
本文标题:java jacob 操作word 文档,进行写操作
链接地址:https://www.777doc.com/doc-3390407 .html