




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、javajacob操作word文檔,進(jìn)行寫操作,如生成表格,添加圖片jacob-1.15-M3-x86.dllcopy到c:/windows/system32引入jacob.jar示例代碼javaviewplaincopy1.importjavaio.File;2.importcom.jacob.activeX.ActiveXComponent;3.Dispatch;4.Variant;5.classWordBean6/代表一個word程序7.privateActiveXComponentMsWordApp=null;8./代表進(jìn)彳亍處理的word文檔9.privateDispatchdocu
2、ment=null;10.publicWordBean()11./OpenWordifwe/venotdoneitalready12.if(MsWordApp=null)13.MsWordApp=newActiveXComponent(WordApplication);14.15.16./設(shè)置是否在前臺打開word程序,17.publicvoidsetVisible(booleanvisible)18.MsWordApp.setProperty(“Visible,newVariant(visible);19./這一句作用相同20./Dispatch.put(MsWordApp,Visible,
3、newVariant(visible);21.22./創(chuàng)建一個新文檔23.publicvoidcreateNewDocument()24./FindtheDocumentscollectionobjectmaintainedbyWord25./documents表示word的所有文檔窗口,(word是多文檔應(yīng)用程序)26.Dispatchdocuments=Dispatchget(MsWordApp,Documents)toDispatch();27./CalltheAddmethodoftheDocumentscollectiontocreate28./anewdocumenttoedit2
4、9.document=Dispatch.call(documents,Add)toDispatch();30.31./打開一個存在的word文檔,并用document引用弓I用它32.publicvoidopenFile(StringwordFilePath)33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77./FindtheDocumentscollectionobjectma
5、intainedbyWord/documents表示word的所有文檔窗口,(word是多文檔應(yīng)用程序)Dispatchdocuments=Dispatchget(MsWordApp,Documents)toDispatch();document=Dispatch.call(documents,Open,wordFilePath,newVariant(true)/*是否進(jìn)彳亍轉(zhuǎn)換ConfirmConversions*/,newVariant(false)/*是否只讀*/).toDispatch();/document=Dispatch.invoke(documents,Open,Dispatc
6、h.Method,/newObjectwordFilePath,newVariant(true),/newVariant(false)/,newint1)toDispatch();/向document中插入文本內(nèi)容publicvoidinsertText(StringtextTolnsert)/GetthecurrentselectionwithinWordatthemoment/anewdocumenthasjustbeencreatedthenthiswillbeat/thetopofthenewdoc獲得選中的內(nèi)容,如果是一個新創(chuàng)建的文件,因里面無內(nèi)容,則光標(biāo)應(yīng)處于文件開頭處Dispat
7、chselection=Dispatchget(MsWordApp,Selection)toDispatch();/取消選中,應(yīng)該就是移動光標(biāo),否則新添加的內(nèi)容會覆蓋選中的內(nèi)容Dispatch.call(selection,MoveRight,newVariant(1),newVariant(1);/PutthespecifiedtextattheinsertionpointDispatch.put(selection,Text,textTolnsert);/取消選中,應(yīng)該就是移動光標(biāo)Dispatch.call(selection,MoveRight,newVariant(1),newVari
8、ant(1);/向文檔中添加一個圖片,publicvoidinsertJpeg(StringjpegFilePath)Dispatchselection=Dispatchget(MsWordApp,Selection)toDispatch();Dispatchimage=Dispatchget(selection,InLineShapes)toDispatch();Dispatch.call(image,AddPicture,jpegFilePath);/段落的處理,插入格式化的文本publicvoidinsertFormatStr(Stringtext)DispatchwordContent
9、=Dispatch.get(document,Content)toDispatch();/取得word文件的內(nèi)容Dispatchcall(wordContent,InsertAfter,text);/插入一個段落到最后Dispatchparagraphs=Dispatchget(wordContent,Paragraphs).toDispatch();/所有段落intparagraphCount=Dispatch.get(paragraphs,Count)changeType(VariantVariantint)getint();/一共的段落數(shù)/找到剛輸入的段落,設(shè)置格式Dispatchlas
10、tParagraph=Dispatch.call(paragraphs,Item,newVariant(paragraphCount).toDispatch();/最后一段(也就是剛插入的)/Range對象表示文檔中的一個連續(xù)范圍,由一個起始字符位置和一個終止字符位置定義DispatchlastParagraphRange=Dispatchget(lastParagraph,Range)toDispatch();Dispatchfont=Dispatch.get(lastParagraphRange,Font)toDispatch();Dispatch.putfont,Bold,newVari
11、ant(true);/設(shè)置為黑體Dispatch.putfont,Italic,newVariant(true);/設(shè)置為斜體Dispatch.putfont,Name,newVariant(宋體);/Dispatch.putfont,Size,newVariant(12);/小四Dispatchselection=Dispatchget(MsWordApp,Selection)toDispatch();Dispatch.call(selection,TypeParagraph);/插入一個空行Dispatchalignment=Dispatchget(selection,ParagraphF
12、ormat).toDispatch();/段落格式Dispatch.put(alignment,Alignment,2);/(1:置中2:靠右3:靠左)/word中在對表格進(jìn)彳亍遍歷的時候,是先列后行先column后cell/另外下標(biāo)從1開始publicvoidinsertTable(StringtableTitle,introw,intcolumn)Dispatchselection=Dispatch.get(MsWordApp,Selection)toDispatch();/輸入內(nèi)容需要的對象Dispatch.call(selection,TypeText,tableTitle);/寫入標(biāo)
13、題內(nèi)容/標(biāo)題格彳亍Dispatch.call(selection,TypeParagraph);/空一行段落Dispatch.call(selection,TypeParagraph);/空一行段落Dispatch.call(selection,MoveDown);/游標(biāo)往下一行/建立表格Dispatchtables=Dispatchget(document,Tables)toDispatch();/intcount=Dispatchget(tables,/Count)changeType(VariantVariantInt)getInt();/document中的表格數(shù)量/Dispatcht
14、able=Dispatch.call(tables,Item,newVariant(/1).toDispatch();/文檔中第一個表格Dispatchrange=Dispatch.get(selection,Range).toDispatch();/當(dāng)前光標(biāo)位置或者選中的區(qū)域DispatchnewTable=Dispatch.call(tables,Add,range,newVariant(row),newVariant(column),newVariant(1).toDispatch();/設(shè)置row,column,表格外框?qū)挾菵ispatchcols=Dispatch.get(newTa
15、ble,Columns).toDispatch();/此表的所有列,intcolCount=Dispatchget(cols,Count)changeType(Variant.Variantint).getint();/共有多少列實際上這個數(shù)=columnSystemoutprintln(colCount+列);for(inti=1;i=colCount;i+)/循環(huán)取岀每一列Dispatchcol=Dispatch.call(cols,Item,newVariant(i)toDispatch();Dispatchcells=Dispatch.get(col,Cells).toDispatch
16、();/當(dāng)前列中單元格intcellCount=Dispatchget(cells,Count)changeType(Variant.Variantint).getint();/當(dāng)前列中單元格數(shù)實際上這個數(shù)等于rowfor(intj=1;j=cellCount;j+)/每一列中的單元格數(shù)/Dispatchcell=Dispatch.call(cells,Item,new/Variant(j).toDispatch();/當(dāng)前單元格/Dispatchcell=Dispatch.call(newTable,Cell,new/Variant(j),newVariant(i).toDispatch()
17、;/取單元格的另一種方法78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.97.98.99.100.101.102.103.104.105.106.107.108.109.110.111.112.113.114.115.116.117.118.119.120.121.122./Dispatch.call(cell,Select);/選中當(dāng)前單元格/Dispatch.put(selection,Text,/合并兩個單元格123.124.125.126.127.128.129.130.131.132.133.134.135.136.1
18、37.138.139.140.141.142.143.144.145.146.147.148.149.150.151.152.153.154.155.156.157.158.159.160.161.162.163.164.165.166./第+j+行,第+i+列);/往選中的區(qū)域中填值,也就是往當(dāng)前單元格填值putTxtToCell(newTable,j,i,第+j+行,第+i+列);/與上面四句的作用相同/*/*在指定的單元格里填寫數(shù)據(jù)*paramtableIndexparamcellRowIdxparamcellColIdxparamtxt*/publicvoidputTxtToCell(
19、Dispatchtable,intcellRowIdx,intcellColIdx,Stringtxt)Dispatchcell=Dispatch.call(table,Cell,newVariant(cellRowIdx),newVariant(cellColIdx)toDispatch();Dispatch.call(cell,Select);Dispatchselection=Dispatchget(MsWordApp,Selection).toDispatch();/輸入內(nèi)容需要的對象Dispatch.put(selection,Text,txt);/*/*在指定的單元格里填寫數(shù)據(jù)*p
20、aramtableIndexparamcellRowIdxparamcellColIdxparamtxt*/publicvoidputTxtToCell(inttableIndex,intcellRowIdx,intcellColIdx,Stringtxt)/所有表格Dispatchtables=Dispatchget(document,Tables)toDispatch();/要填充的表格Dispatchtable=Dispatch.call(tables,Item,newVariant(tableIndex)toDispatch();Dispatchcell=Dispatch.call(t
21、able,Cell,newVariant(cellRowIdx),newVariant(cellColIdx)toDispatch();Dispatch.call(cell,Select);Dispatchselection=Dispatchget(MsWordApp,Selection).toDispatch();/輸入內(nèi)容需要的對象Dispatch.put(selection,Text,txt);/合并兩個單元格167.168.169.170.171.172.173.174.175.176.177.178.179.180.181.182.183.184.185.186.187.188.18
22、9.190.191.192.193.194.195.196.197.198.199.200.201.202.203.204.205.206.207.208.209.210.211.publicvoidmergeCell(Dispatchcell1,Dispatchcell2)Dispatch.call(cell1,Merge,cell2);publicvoidmergeCell(Dispatchtable,introw1,intcol1,introw2,intcol2)Dispatchcell1=Dispatch.call(table,Cell,newVariant(row1),newVari
23、ant(col1)toDispatch();Dispatchcell2=Dispatch.call(table,Cell,newVariant(row2),newVariant(col2)toDispatch();mergeCell(cell1,cell2);publicvoidmergeCellTest()Dispatchtables=Dispatchget(document,Tables)toDispatch();inttableCount=Dispatchget(tables,Count)changeType(VariantVariantint).getInt();/document中的
24、表格數(shù)量Dispatchtable=Dispatch.call(tables,Item,newVariant(tableCount).toDispatch();/文檔中最后一個tablemergeCell(table,1,1,1,2);/將table中x=1,y=1與x=1,y=2的兩個單元格合并/=/*/*把選定的內(nèi)容或光標(biāo)插入點(diǎn)向上移動*parampos移動的距離*/publicvoidmoveUp(intpos)Dispatchselection=Dispatchget(MsWordApp,Selection).toDispatch();/輸入內(nèi)容需要的對象for(inti=0;ipos
25、;i+)/MoveDownMoveLeftmoveRight/moveStart(Dispatch.call(selection,HomeKey,newVariant(6);/)/moveEndDispatch.call(selection,EndKey,newVariant(6);Dispatch.call(selection,MoveUp);/*/*從選定內(nèi)容或插入點(diǎn)開始查找文本*paramtoFindText要查找的文本returnbooleantrue-查找到并選中該文本,false-未查找到文本*/合并兩個單元格212.213.214.215.216.217.218.219.220.
26、221.222.223.224.225.226.227.228.229.230.231.232.233.234.235.236.237.238.239.240.241.242.243.244.245.246.247.248.249.250.251.252.253.254.255.256.publicbooleanfind(StringtoFindText)if(toFindText=null|toFindTextequals()returnfalse;Dispatchselection=Dispatchget(MsWordApp,Selection).toDispatch();/輸入內(nèi)容需要的
27、對象/從selection所在位置開始查詢Dispatchfind=Dispatch.call(selection,Find)toDispatch();/設(shè)置要查找的內(nèi)容/合并兩個單元格/合并兩個單元格Dispatch.put(find,Text,toFindText);/合并兩個單元格/合并兩個單元格/向前查找Dispatch.put(find,Forward,True);/合并兩個單元格/合并兩個單元格/設(shè)置格式Dispatch.put(find,Format,True);/合并兩個單元格/合并兩個單元格/大小寫匹配Dispatch.put(find,MatchCase,True);/合并
28、兩個單元格/合并兩個單元格/全字匹配Dispatch.put(find,MatchWholeWord,True);/查找并選中returnDispatch.call(find,Execute)getBoolean();/*/*把選定選定內(nèi)容設(shè)定為替換文本*paramtoFindText查找字符串paramnewText要替換的內(nèi)容return*/publicbooleanreplaceText(StringtoFindText,StringnewText)if(!find(toFindText)returnfalse;Dispatchselection=Dispatchget(MsWordAp
29、p,Selection).toDispatch();/輸入內(nèi)容需要的對象Dispatch.put(selection,Text,newText);returntrue;publicvoidprintFile()/JustprintthecurrentdocumenttothedefaultprinterDispatch.call(document,Printout);/保存文檔的更改publicvoidsave()Dispatch.call(document,Save);publicvoidsaveFileAs(Stringfilename)257.Dispatchcall(document,
30、SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);publicvoidcloseDocument()258.259.260.261.262.263.264.265.266.267.268.269.270.271.272.273.274.275.276.277.278.279.280.281.282.283.284.285.286.287.288.289.2
31、90.291.292.293.294.295.296.297.298.299.300.257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);/0=wdDoNotSaveChanges/-1=wdSaveChangesClosethedocumentwithoutsavingchanges257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);257.Dispatc
32、hcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);/-2=wdPromptToSaveChangesDispatch.call(document,Close,newVariant(0);document=null;publicvoidcloseWord()Dispatch.call(MsWordApp,Quit);MsWordApp=null;document=null;/設(shè)置wordApp打開后窗口的位置publicvoidsetLocation()DispatchactiveWindow=D
33、ispatchget(MsWordApp,Application)toDispatch();Dispatch.put(activeWindow,Windowstate,newVariant(1);/0=default/1=maximize/2=minimizeDispatch.put(activeWindow,Top,newVariant(0);Dispatch.put(activeWindow,Left,newVariant(0);Dispatch.put(activeWindow,Height,newVariant(600);publicDispatch.put(activeWindow,
34、classJacobTest2width,newVariant(800);257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);publicstaticvoidcreateANewFileTest()WordBeanwordBean=newWordBean();程序/wordopenWord(true);/打
35、開wordwordBean.setVisible(true);wordBean.createNewDocument();/創(chuàng)建一個新文檔wordBean.setLocation();/設(shè)置打開后窗口的位置wordBean.insertText(你好);/向文檔中插入字符wordBean.insertJpeg(D:+File.separator+ajpg);/插入圖片/如果,想保存文件,下面三句257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall
36、(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);/wordsaveFileAs(d:/adoc);257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);/word.closeDocument();/wordcloseWord();257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);publicstaticvoidopenAnExistsFileTest()WordBeanwordBean=newWordBean();301.302.303.304.305.306.307.308.309.310.
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 學(xué)習(xí)動力激發(fā)輔導(dǎo)考核試卷
- 舊貨零售店鋪選址與商圈分析考核試卷
- 拉丁語基礎(chǔ)與古羅馬文化考核試卷
- 智能材料設(shè)計與制造考核試卷
- 小學(xué)生經(jīng)典誦讀愛國課件
- 智能餐飲顧客服務(wù)系統(tǒng)考核試卷
- ehs之家安全培訓(xùn)課件
- 施工安全合同范本
- 城管部門采購合同范本
- 貨物拉運(yùn)合同范本
- 2025年1月浙江高考英語聽力試題真題完整版(含答案+文本+MP3)
- 2024年3、6、9月青少年軟件編程Python等級考試一級真題(全3套 含答案)
- T-IMAS 087-2024 托克托縣辣椒地方品種提純復(fù)壯技術(shù)規(guī)程
- 2025年全國道路運(yùn)輸企業(yè)安全管理人員考試題庫(含答案)
- 太陽能光伏發(fā)電安裝工程監(jiān)理實施細(xì)則
- 小學(xué)科學(xué)課件《水》
- 全新版大學(xué)高階英語:綜合教程 第3冊 Unit 6 China Rejuvenated課件
- 2024年下半年江蘇省鹽城市射陽縣人民政府項目辦公室招聘易考易錯模擬試題(共500題)試卷后附參考答案
- 醫(yī)療行業(yè)信息安全等級保護(hù)
- 新公務(wù)員法培訓(xùn)講稿
- 荊州市國土空間總體規(guī)劃(2021-2035年)
評論
0/150
提交評論