java 操作doc_Java如何操作Word?

JavaExcelAPI文档http://www。andykhan。com/jexcelapi/1、一个jacob操作Word的例子,其他操作excel,pdf的sample里都有importjava。io。File;importcom。  jacob。com。*;importcom。jacob。activeX。*;publicclassWordTest{publicstaticvoidmain(String[]args){WordBeanword=newWordBean();word。  openWord(true);word。createNewDocument();word。insertText("Helloword。");}}importcom。jacob。activeX。*;importcom。  jacob。com。*;publicclassWordBeanextendsjava。awt。Panel{privateActiveXComponentMsWordApp=null;privateDispatchdocument=null;publicWordBean(){super();}publicvoidopenWord(booleanmakeVisible){//OpenWordifwe'venotdoneitalreadyif(MsWordApp==null){MsWordApp=newActiveXComponent("Word。  Application");}//Setthevisiblepropertyasrequired。Dispatch。put(MsWordApp,"Visible",newVariant(makeVisible));}publicvoidcreateNewDocument(){//FindtheDocumentscollectionobjectmaintainedbyWordDispatchdocuments=Dispatch。  get(MsWordApp,"Documents")。toDispatch();//CalltheAddmethodoftheDocumentscollectiontocreate//anewdocumenttoeditdocument=Dispatch。  call(documents,"Add")。toDispatch();}publicvoidinsertText(StringtextToInsert){//GetthecurrentselectionwithinWordatthemoment。  If//anewdocumenthasjustbeencreatedthenthiswillbeat//thetopofthenewdocDispatchselection=Dispatch。  get(MsWordApp,"Selection")。toDispatch();//PutthespecifiedtextattheinsertionpointDispatch。put(selection,"Text",textToInsert);}publicvoidsaveFileAs(Stringfilename){Dispatch。  call(document,"SaveAs",filename);}publicvoidprintFile(){//JustprintthecurrentdocumenttothedefaultprinterDispatch。  call(document,"PrintOut");}publicvoidcloseDocument(){//Closethedocumentwithoutsavingchanges//0=wdDoNotSaveChanges//-1=wdSaveChanges//-2=wdPromptToSaveChangesDispatch。  call(document,"Close",newVariant(0));document=null;}publicvoidcloseWord(){Dispatch。call(MsWordApp,"Quit");MsWordApp=null;document=null;}}。

阅读全文 >

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值