资源描述:
《java操作word-需要导入一个包jacob-1.15-M4.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、packagescau.action;importcom.jacob.activeX.ActiveXComponent;importcom.jacob.com.Dispatch;importcom.jacob.com.Variant;publicclassCreatWordDemo{publicstaticvoidmain(Stringargs[]){ActiveXComponentwordApp=newActiveXComponent("Word.Application");//启动wordDispatch.put(wordApp,"Visible",newVariant(
2、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();//打开文档Dispatchdocumen
3、t=Dispatch.call(docs,"Add").toDispatch();//createnewdocumentStringuserName=wordApp.getPropertyAsString("Username");//显示用户信息//System.out.println("用户名:"+userName);//文档对齐,字体设置Dispatchselection=Dispatch.get(wordApp,"Selection").toDispatch();Dispatchalign=Dispatch.get(selection,"ParagraphFormat"
4、).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,6,0,0");//字型颜色红色Dispatch.call(selection,"TypeText"
5、,"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();Dis
6、patchrange=Dispatch.get(selection,"Range").toDispatch();Dispatchtable1=Dispatch.call(tables,"Add",range,newVariant(3),newVariant(3),newVariant(1)).toDispatch();//设置行数,列数,表格外框宽度//所有表格VarianttableAmount=Dispatch.get(tables,"count");//System.out.println(tableAmount);//要填充的表格Dispatcht1=Dispatch
7、.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