




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、實(shí)用標(biāo)準(zhǔn)文檔JAVA 操作 wordJava 操作 Microsoft Word 之 jacob(1)現(xiàn)在我們一起來看看,用 J1ava 如何操作 Microsoft Word 。jacob ,官網(wǎng)是 這是一個開源的工具。最新版本 1.7 官方的解釋是: The JACOB Project: A JAva-COM Bridge 這是官方對下載文件的說明:jacob.jar: a JAR file for the java classes which you must add to your CLASSPATH.The package names replace com.ms with com.
2、jacob (for example .Variant maps to .Variant.jacob.dll: a small Win32 DLL which you must add to your PATH.samples: provided in Java source and compiled form to demonstrate variousfeatures of the product. In particular, a set of wrapper classes for Microsoft? ADO are provided as samples.開發(fā)環(huán)境:JDK 1.6M
3、yEclipse Enterprise Workbench Version: 7.0 Milestone-1Tomcat 5.5.27現(xiàn)在 MyEclipse 中新建一個項(xiàng)目 jacob ,將 jacob 的 jar 包放到該項(xiàng)目的類庫中。 我的 jacob 版本是 1.14.3 。文案大全實(shí)用標(biāo)準(zhǔn)文檔文案大全實(shí)用標(biāo)準(zhǔn)文檔下面這一步非常重要,就是拷貝 jacob 目錄中 jacob-1.14.3-x86.dll 文件到系統(tǒng)環(huán)境變量目錄中一般情況就放在當(dāng)前 jdk 中 bin 目錄下。文案大全實(shí)用標(biāo)準(zhǔn)文檔這里有一個 MSWordManager 類,是 jacob 官方發(fā)布的工具類, 里面有大多數(shù)
4、 Java 操作 MS Office 的工具。package com.test;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import com.jacob.activeX.ActiveXComponent;文案大全實(shí)用標(biāo)準(zhǔn)文檔import .Dispatch;import .Variant;public class MSWo
5、rdManager / word 文檔private Dispatch doc;/ word 運(yùn)行程序?qū)ο髉rivate ActiveXComponent word;/ 所有 word 文檔集合private Dispatch documents;/ 選定的范圍或插入點(diǎn)private Dispatch selection;private boolean saveOnExit = true;* param visible為 true 表示 word 應(yīng)用程序可見*/public MSWordManager(boolean visible) if (word = null) word = newA
6、ctiveXComponent(Word.Application);word.setProperty(Visible, new Variant(visible);if (documents = null)documents =word.getProperty(Documents).toDispatch();/* */* 設(shè)置退出時(shí)參數(shù)* param saveOnExit boolean true- 退出時(shí)保存文件, false- 退出時(shí)不保存文件 */public void setSaveOnExit(boolean saveOnExit) this.saveOnExit = saveOnEx
7、it;/* */* 創(chuàng)建一個新的 word 文檔文案大全實(shí)用標(biāo)準(zhǔn)文檔*/public void createNewDocument() doc = Dispatch.call(documents, Add).toDispatch(); selection = Dispatch.get(word, Selection).toDispatch();* 打開一個已存在的文檔* param docPath */public void openDocument(String docPath) closeDocument();doc = Dispatch.call(documents, Open, doc
8、Path).toDispatch();selection = Dispatch.get(word, Selection).toDispatch();/* */* 把選定的內(nèi)容或插入點(diǎn)向上移動* param pos 移動的距離*/ public void moveUp(int pos) if (selection = null)selection = Dispatch.get(word, Selection).toDispatch();for (int i = 0; i < pos; i+) Dispatch.call(selection, MoveUp);/* */* 把選定的內(nèi)容或者插
9、入點(diǎn)向下移動* param pos 移動的距離*/ public void moveDown(int pos) if (selection = null)selection = Dispatch.get(word, Selection).toDispatch();for (int i = 0; i < pos; i+)Dispatch.call(selection, MoveDown);文案大全實(shí)用標(biāo)準(zhǔn)文檔/* */* 把選定的內(nèi)容或者插入點(diǎn)向左移動* param pos移動的距離*/ public void moveLeft(int pos) if (selection = null)
10、 selection = Dispatch.get(word, Selection).toDispatch();for (int i = 0; i < pos; i+) Dispatch.call(selection, MoveLeft);/* */* 把選定的內(nèi)容或者插入點(diǎn)向右移動* param pos移動的距離*/ public void moveRight(int pos) if (selection = null) selection = Dispatch.get(word, Selection).toDispatch();for (int i = 0; i < pos;
11、i+) Dispatch.call(selection, MoveRight);/* */* 把插入點(diǎn)移動到文件首位置*/ public void moveStart() if (selection = null) selection = Dispatch.get(word, Selection).toDispatch();Dispatch.call(selection, HomeKey, new Variant(6); public void moveEnd() if (selection = null)selection = Dispatch.get(word, Selection).to
12、Dispatch();Dispatch.call(selection, EndKey, new Variant(6);文案大全實(shí)用標(biāo)準(zhǔn)文檔/* */* 從選定內(nèi)容或插入點(diǎn)開始查找文本* param toFindText 要查找的文本* return boolean true-查找到并選中該文本, false- 未查找到文本*/public boolean find(String toFindText) if (toFindText = null | toFindText.equals()return false;/ 從 selection 所在位置開始查詢 Dispatch find = wo
13、rd.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);/ 全字匹配Dispatch.put(find, MatchWholeWord, True);/ 查找并選中return Dispatch.call(find, Execute).get
14、Boolean();/* */* 把選定選定內(nèi)容設(shè)定為替換文本* param toFindText 查找字符串* param newText 要替換的內(nèi)容* return*/public boolean replaceText(String toFindText, String newText) if (!find(toFindText)return false;Dispatch.put(selection, Text, newText); return true;/* */* 全局替換文本文案大全* param toFindText* param newText查找字符串要替換的內(nèi)容實(shí)用標(biāo)準(zhǔn)
15、文檔*/ public void replaceAllText(String toFindText, String newText) while (find(toFindText) Dispatch.put(selection, Text, newText); Dispatch.call(selection, MoveRight);/* */* 在當(dāng)前插入點(diǎn)插入字符串* param newText 要插入的新字符串*/public void insertText(String newText) Dispatch.put(selection, Text, newText);/* */* para
16、m toFindText要查找的字符串* param imagePath圖片路徑* return*/public boolean replaceImage(String toFindText, String imagePath) if (!find(toFindText)return false;Dispatch.call(Dispatch.get(selection, InLineShapes).toDispatch(),AddPicture, imagePath); return true;/* */* 全局替換圖片* param toFindText查找字符串* param imageP
17、ath圖片路徑*/public void replaceAllImage(String toFindText, String imagePath) while (find(toFindText) Dispatch.call(Dispatch.get(selection,InLineShapes).toDispatch(), AddPicture, imagePath);Dispatch.call(selection, MoveRight);文案大全實(shí)用標(biāo)準(zhǔn)文檔/* */* 在當(dāng)前插入點(diǎn)插入圖片* param imagePath圖片路徑*/ public void insertImage(Str
18、ing imagePath) Dispatch.call(Dispatch.get(selection, InLineShapes).toDispatch(),AddPicture, imagePath); /* */* 合并單元格* param tableIndex* param fstCellRowIdx* param fstCellColIdx* param secCellRowIdx* param secCellColIdx*/public void mergeCell(int tableIndex, int fstCellRowIdx, int fstCellColIdx, int
19、secCellRowIdx, int secCellColIdx) / 所有表格 Dispatch tables = Dispatch.get(doc, Tables).toDispatch();/ 要填充的表格Dispatch table = Dispatch.call(tables, Item, new Variant(tableIndex).toDispatch(); Dispatch fstCell = Dispatch.call(table, Cell,new Variant(fstCellRowIdx), new Variant(fstCellColIdx).toDispatch(
20、);Dispatch secCell = Dispatch.call(table, Cell, new Variant(secCellRowIdx), new Variant(secCellColIdx).toDispatch(); Dispatch.call(fstCell, Merge, secCell);/* */* 在指定的單元格里填寫數(shù)據(jù)文案大全實(shí)用標(biāo)準(zhǔn)文檔* param tableIndex* param cellRowIdx* param cellColIdx* param txt*/public void putTxtToCell(int tableIndex, int cel
21、lRowIdx, int cellColIdx,String txt) / 所有表格Dispatch tables = Dispatch.get(doc, Tables).toDispatch();/ 要填充的表格Dispatch table = Dispatch.call(tables, Item, new Variant(tableIndex).toDispatch();Dispatch cell = Dispatch.call(table, Cell, new Variant(cellRowIdx),new Variant(cellColIdx).toDispatch();Dispatc
22、h.call(cell, Select);Dispatch.put(selection, Text, txt);/* */* 在當(dāng)前文檔拷貝數(shù)據(jù)* param pos*/ public void copy(String toCopyText) moveStart();if (this.find(toCopyText) Dispatch textRange = Dispatch.get(selection, Range).toDispatch();Dispatch.call(textRange, Copy);/* */* 在當(dāng)前文檔粘帖剪貼板數(shù)據(jù)* param pos*/ public void
23、 paste(String pos) moveStart(); if (this.find(pos) Dispatch textRange = Dispatch.get(selection, Range).toDispatch();文案大全實(shí)用標(biāo)準(zhǔn)文檔Dispatch.call(textRange, Paste);/* */* 在當(dāng)前文檔指定的位置拷貝表格* param pos 當(dāng)前文檔指定的位置* param tableIndex 被拷貝的表格在 word 文檔中所處的位置*/ public void copyTable(String pos,int tableIndex) Dispatch
24、 tables = Dispatch.get(doc, Tables).toDispatch();Dispatch table = Dispatch.call(tables, Item, new Variant(tableIndex).toDispatch();Dispatch range = Dispatch.get(table, Range).toDispatch();Dispatch.call(range, Copy);if (this.find(pos) Dispatch textRange = Dispatch.get(selection, Range).toDispatch();D
25、ispatch.call(textRange, Paste);/* */* 在當(dāng)前文檔末尾拷貝來自另一個文檔中的段落* param anotherDocPath另一個文檔的磁盤路徑* param tableIndex被拷貝的段落在另一格文檔中的序號 (從1開始 )*/public void copyParagraphFromAnotherDoc(String anotherDocPath,int paragraphIndex) Content).toDispatch(); /Dispatch wordContent = Dispatch.get(doc, 取得當(dāng)前文檔的內(nèi)容Dispatch.c
26、all(wordContent, InsertAfter, $selection$);/插入特殊符定位插入點(diǎn)copyParagraphFromAnotherDoc(anotherDocPath,paragraphIndex,$selection$);/* */* 在當(dāng)前文檔指定的位置拷貝來自另一個文檔中的段落* param anotherDocPath 另一個文檔的磁盤路徑* param tableIndex 被拷貝的段落在另一格文檔中的序號 ( 從 1開始 )文案大全實(shí)用標(biāo)準(zhǔn)文檔* param pos當(dāng)前文檔指定的位置*/public void copyParagraphFromAnothe
27、rDoc(String anotherDocPath,int paragraphIndex, String pos) Dispatch doc2 = null;try doc2 = Dispatch.call(documents, Open, anotherDocPath).toDispatch()Dispatch paragraphs = Dispatch.get(doc2, Paragraphs).toDispatch();Dispatch paragraph =Dispatch.call(paragraphs, Item,new Variant(paragraphIndex).toDis
28、patch();Dispatch range = Dispatch.get(paragraph,Range).toDispatch();Dispatch.call(range, Copy);if (this.find(pos) Dispatch textRange =Dispatch.get(selection, Range).toDispatch();Dispatch.call(textRange,Paste); catch (Exception e) e.printStackTrace(); finally if (doc2 != null) Dispatch.call(doc2, Clo
29、se, new Variant(saveOnExit);doc2 = null;*/* 在當(dāng)前文檔指定的位置拷貝來自另一個文檔中的表格* param anotherDocPath 另一個文檔的磁盤路徑* param tableIndex 被拷貝的表格在另一格文檔中的序號 ( 從 1開始 ) * param pos 當(dāng)前文檔指定的位置文案大全實(shí)用標(biāo)準(zhǔn)文檔*/public void copyTableFromAnotherDoc(String anotherDocPath, inttableIndex,String pos) Dispatch doc2 = null;try doc2 = Disp
30、atch.call(documents, Open,anotherDocPath).toDispatch()Dispatch tables = Dispatch.get(doc2,Tables).toDispatch();Dispatch table = Dispatch.call(tables, Item,newVariant(tableIndex).toDispatch();Dispatch range = Dispatch.get(table,Range).toDispatch();Dispatch.call(range, Copy);if (this.find(pos) Dispatc
31、h textRange =Dispatch.get(selection, Range).toDispatch();Dispatch.call(textRange,Paste); catch (Exception e) e.printStackTrace(); finally if (doc2 != null) Dispatch.call(doc2, Close,new Variant(saveOnExit);doc2 = null;/* */* 在當(dāng)前文檔指定的位置拷貝來自另一個文檔中的圖片* param anotherDocPath 另一個文檔的磁盤路徑* param shapeIndex
32、被拷貝的圖片在另一格文檔中的位置* param pos */當(dāng)前文檔指定的位置public void copyImageFromAnotherDoc(String anotherDocPath, int文案大全實(shí)用標(biāo)準(zhǔn)文檔shapeIndex, String pos) Dispatch doc2 = null; try doc2 = Dispatch.call(documents, Open, anotherDocPath).toDispatch()Dispatch shapes = Dispatch.get(doc2, InLineShapes).toDispatch();Dispatch
33、shape = Dispatch.call(shapes,Item,new Variant(shapeIndex).toDispatch();Dispatch imageRange = Dispatch.get(shape, Range).toDispatch();Dispatch.call(imageRange, Copy);if (this.find(pos) Dispatch textRange =Dispatch.get(selection, Range).toDispatch();Dispatch.call(textRange, Paste); catch (Exception e)
34、 e.printStackTrace(); finally if (doc2 != null) Dispatch.call(doc2, Close, new Variant(saveOnExit);doc2 = null;Java 操作 Microsoft Word 之 jacob(2)( 接上 )/文案大全 param cols 列數(shù)/* 創(chuàng)建表格* param pos位置實(shí)用標(biāo)準(zhǔn)文檔* param rows 行數(shù)*/public void createT able(int numCols, int numRows)/(String pos, int numCols,int numRows)
35、 /if (!find(pos) Dispatch tables = Dispatch.get(doc,Tables).toDispatch();Dispatch range = Dispatch.get(selection,Range).toDispatch();Dispatch newT able = Dispatch.call(tables,Add, range,newVariant(numRows), new Variant(numCols).toDispatch();Dispatch.call(selection, MoveRight);moveEnd();/ables).toDis
36、patch();Variant(tableIndex).toDispatch();/ 表格的所有行Dispatch rows = Dispatch.get(table, Rows).toDispatch();Dispatch row = Dispatch.call(rows, Item, newVariant(rowIndex)/*/ public void addFirstTableRow(int tableIndex) param tableIndex word文檔中的第 N 張表 (從 1 開始 )/* 在指定行前面增加行* param tableIndex word文件中的第 N 張表
37、(從1 開始)* param rowIndex 指定行的序號 ( 從 1 開始 )*/public void addT ableRow(int tableIndex, int rowIndex) / 所有表格Dispatch tables = Dispatch.get(doc, T / 要填充的表格Dispatch table = Dispatch.call(tables, Item, new.toDispatch();Dispatch.call(rows, Add, new Variant(row);/* */* 在第 1 行前增加一行文案大全實(shí)用標(biāo)準(zhǔn)文檔/ 所有表格Dispatch tab
38、les = Dispatch.get(doc, Tables).toDispatch();/ 要填充的表格Dispatch table = Dispatch.call(tables, Item, newVariant(tableIndex).toDispatch();/ 表格的所有行Dispatch rows = Dispatch.get(table, Rows).toDispatch();Dispatch row = Dispatch.get(rows, First).toDispatch(); Dispatch.call(rows, Add, new Variant(row);/* */*
39、 在最后 1 行前增加一行* param tableIndexword 文檔中的第 N 張表(從 1 開始 )*/ public void addLastTableRow(int tableIndex) / 所有表格Dispatch tables = Dispatch.get(doc, T ables).toDispatch(); / 要填充的表格Dispatch table = Dispatch.call(tables, Item, newVariant(tableIndex).toDispatch();/ 表格的所有行Dispatch rows = Dispatch.get(table,
40、Rows).toDispatch();Dispatch row = Dispatch.get(rows, Last).toDispatch(); Dispatch.call(rows, Add, new Variant(row);/* */* 增加一行* param tableIndex word 文檔中的第 N 張表 (從 1 開始 )*/ public void addRow(int tableIndex) Dispatch tables = Dispatch.get(doc, Tables).toDispatch();/ 要填充的表格Dispatch table = Dispatch.c
41、all(tables, Item, newVariant(tableIndex).toDispatch();/ 表格的所有行Dispatch rows = Dispatch.get(table, Rows).toDispatch();文案大全Variant(tableIndex)實(shí)用標(biāo)準(zhǔn)文檔Dispatch.call(rows, Add);/* */* 增加一列* param tableIndex word 文檔中的第 N 張表 (從 1 開始 )*/public void addCol(int tableIndex) / 所有表格Dispatch tables = Dispatch.get(
42、doc, Tables).toDispatch();/ 要填充的表格Dispatch table = Dispatch.call(tables, Item, newVariant(tableIndex)Variant(colIndex).toDispatch();/ 表格的所有行Dispatch cols = Dispatch.get(table, Columns).toDispatch();Dispatch.call(cols, Add).toDispatch();Dispatch.call(cols, AutoFit);/* */* 在指定列前面增加表格的列* param tableInd
43、ex word 文檔中的第 N 張表 (從 1 開始 )* param colIndex 指定列的序號 ( 從 1 開始 )*/ public void addT ableCol(int tableIndex, int colIndex) / 所有表格Dispatch tables = Dispatch.get(doc, T ables).toDispatch(); / 要填充的表格Dispatch table = Dispatch.call(tables, Item, new.toDispatch();/ 表格的所有行Dispatch cols = Dispatch.get(table, C
44、olumns).toDispatch(); System.out.println(Dispatch.get(cols, Count); Dispatch col = Dispatch.call(cols, Item, new.toDispatch();/ Dispatch col = Dispatch.get(cols, First).toDispatch(); Dispatch.call(cols, Add, col).toDispatch();Dispatch.call(cols, AutoFit);文案大全實(shí)用標(biāo)準(zhǔn)文檔/* */* 在第 1 列前增加一列Variant(tableInde
45、x)* param tableIndex word 文檔中的第 N 張表 (從 1 開始 )*/public void addFirstTableCol(int tableIndex) Dispatch tables = Dispatch.get(doc, Tables).toDispatch();/ 要填充的表格Dispatch table = Dispatch.call(tables, Item, new.toDispatch();/ 表格的所有行Dispatch cols = Dispatch.get(table, Columns).toDispatch();Dispatch col =
46、 Dispatch.get(cols, First).toDispatch();Dispatch.call(cols, Add, col).toDispatch();Dispatch.call(cols, AutoFit);/* */* 在最后一列前增加一列* param tableIndex word 文檔中的第 N 張表 (從 1 開始 )*/public void addLastTableCol(int tableIndex) Dispatch tables = Dispatch.get(doc, Tables).toDispatch();/ 要填充的表格Variant(tableInd
47、ex)Dispatch table = Dispatch.call(tables, Item, new.toDispatch();/ 表格的所有行Dispatch cols = Dispatch.get(table, Columns).toDispatch();Dispatch col = Dispatch.get(cols, Last).toDispatch();Dispatch.call(cols, Add, col).toDispatch();Dispatch.call(cols, AutoFit);/* */* 自動調(diào)整表格*/ public void autoFitTable() D
48、ispatch tables = Dispatch.get(doc, T ables).toDispatch(); int count = Dispatch.get(tables, Count).toInt();for (int i = 0; i < count; i+) Dispatch table = Dispatch.call(tables,文案大全實(shí)用標(biāo)準(zhǔn)文檔Item, new Variant(i + 1) ch();Columns).toDispatch();/* */.toDispatDispatch cols = Dispatch.get(table,Dispatch.call(cols, AutoFit);* 調(diào)用 word 里的宏以調(diào)整表格的寬度 , 其中宏保存在 document 下*/public void callWordMacro() Dispatch tables = Dispatch.get(doc, T ables).toDispatch(); int count = Dispatch.get(tables, Count).toInt();Variant vMacroName = newVariant(Normal.NewMacros.tableFit);Item, new Va
溫馨提示
- 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)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 主播續(xù)約合同范本
- 公路單車出租合同范本
- 與政府物業(yè)合同范本
- 分公司人員合同范本
- 第1單元第5課 《歌聲嘹亮-子程序設(shè)計(jì)和機(jī)器人發(fā)音》教學(xué)設(shè)計(jì) 2023-2024學(xué)年清華大學(xué)版(2012)初中信息技術(shù)九年級下冊
- 個人運(yùn)輸公司合同范本
- 加盟針織合同范本
- 制作平臺合同范本
- 出租婚紗租賃合同范本
- 出售移動混凝土合同范本
- 2024年中國養(yǎng)老產(chǎn)業(yè)商學(xué)研究報(bào)告-銀發(fā)經(jīng)濟(jì)專題
- 高教版2023年中職教科書《語文》(基礎(chǔ)模塊)下冊教案全冊
- 川教版四年級《生命.生態(tài).安全》下冊全冊 課件
- JJG 693-2011可燃?xì)怏w檢測報(bào)警器
- 肺斷層解剖及CT圖像(77頁)
- LeapMotion教程之手勢識別
- 靜脈導(dǎo)管的護(hù)理與固定方法
- word上機(jī)操作題
- 房地產(chǎn)公司管理制度
- O型密封圈標(biāo)準(zhǔn) ISO 3601-12008[E]中文
- 醫(yī)院醫(yī)療服務(wù)價(jià)格管理制度
評論
0/150
提交評論