jacob-1.15-M3.zip
jacob-1.15-M3-x86.dll copy 到c:\\windows\system32
引入jacob.jar
示例代碼
import java.io.File;
importcom.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
class WordBean {
// 代表一個word 程序
private ActiveXComponent MsWordApp = null;
// 代表進行處理的word 文檔
private Dispatch document = null;
public WordBean() {
// Open Word if we\'ve not done it already
if (MsWordApp == null) {
MsWordApp = new ActiveXComponent("Word.Application");
}
}
// 設置是否在前臺打開 word程序 ,
public void setVisible(boolean visible) {
MsWordApp.setProperty("Visible", new Variant(visible));
// 這一句作用相同
// Dispatch.put(MsWordApp, "Visible", newVariant(visible));
}
// 創建一個新文檔
public void createNewDocument() {
// Find the Documents collection object maintained by Word
// documents表示word的所有文檔窗口,(word是多文檔應用程序)
Dispatch documents = Dispatch.get(MsWordApp,"Documents").toDispatch();
// Call the Add method of the Documents collection to create
// a new document to edit
document = Dispatch.call(documents, "Add").toDispatch();
}
// 打開一個存在的word文檔,並用document 引用 引用它
public void openFile(String wordFilePath) {
// Find the Documents collection object maintained by Word
// documents表示word的所有文檔窗口,(word是多文檔應用程序)
Dispatch documents = Dispatch.get(MsWordApp,"Documents").toDispatch();
document = Dispatch.call(documents, "Open", wordFilePath,
new Variant(true)/* 是否進行轉換ConfirmConversions */,
new Variant(false)/* 是否只讀 */).toDispatch();
// document = Dispatch.invoke(documents, "Open",Dispatch.Method,
// new Object[] { wordFilePath, new Variant(true),
// new Variant(false)
// }, new int[1]).toDispatch();
}
// 向 document 中插入文本內容
public void insertText(String textToInsert) {
// Get the current selection within Word at the moment.
// a new document has just been created then this will be at
// the top of the new doc 獲得選 中的內容,如果是一個新創建的文件,因裏面無內容,則光標應處於文件開頭處
Dispatch selection = Dispatch.get(MsWordApp,"Selection").toDispatch();
// 取消選中,應該就是移動光標 ,否則 新添加的內容會覆蓋選中的內容
Dispatch.call(selection, "MoveRight", new Variant(1), newVariant(1));
// Put the specified text at the insertion point
Dispatch.put(selection, "Text", textToInsert);
// 取消選中,應該就是移動光標
Dispatch.call(selection, "MoveRight", new Variant(1), newVariant(1));
}
// 向文檔中添加 一個圖片,
public void insertJpeg(String jpegFilePath) {
Dispatch selection = Dispatch.get(MsWordApp,"Selection").toDispatch();
Dispatch image = Dispatch.get(selection,"InLineShapes").toDispatch();
Dispatch.call(image, "AddPicture", jpegFilePath);
}
// 段落的處理,插入格式化的文本
public void insertFormatStr(String text) {
Dispatch wordContent = Dispatch.get(document,"Content").toDispatch(); // 取得word文件的內容
Dispatch.call(wordContent, "InsertAfter", text);// 插入一個段落到最後
Dispatch paragraphs = Dispatch.get(wordContent,"Paragraphs")
.toDispatch(); // 所有段落
int paragraphCount = Dispatch.get(paragraphs,"Count").changeType(
Variant.VariantInt).getInt();//一共的段落數
// 找到剛輸入的段落,設置格式
Dispatch lastParagraph = Dispatch.call(paragraphs,"Item",
newVariant(paragraphCount)).toDispatch(); // 最後一段(也就是剛插入的)
// Range 對象表示文檔中的一個連續範圍,由一個起始字符位置和一個終止字符位置定義
Dispatch lastParagraphRange = Dispatch.get(lastParagraph,"Range")
.toDispatch();
Dispatch font = Dispatch.get(lastParagraphRange,"Font").toDispatch();
Dispatch.put(font, "Bold", new Variant(true)); // 設置爲黑體
Dispatch.put(font, "Italic", new Variant(true)); // 設置爲斜體
Dispatch.put(font, "Name", new Variant("宋體")); //
Dispatch.put(font, "Size", new Variant(12)); // 小四
Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
Dispatch.call(selection, "TypeParagraph");// 插入一個空行
Dispatch alignment = Dispatch.get(selection,"ParagraphFormat")
.toDispatch();// 段落格式
Dispatch.put(alignment, "Alignment", "2"); // (1:置中 2:靠右 3:靠左)
}
// word 中在對錶格進行遍歷的時候 ,是先列後行 先column 後cell
// 另外下標從1開始
public void insertTable(String tableTitle, int row, int column) {
Dispatch selection = Dispatch.get(MsWordApp,"Selection").toDispatch(); // 輸入內容需要的對象
Dispatch.call(selection, "TypeText", tableTitle); // 寫入標題內容 // 標題格行
Dispatch.call(selection, "TypeParagraph"); // 空一行段落
Dispatch.call(selection, "TypeParagraph"); // 空一行段落
Dispatch.call(selection, "MoveDown"); // 遊標往下一行
// 建立表格
Dispatch tables = Dispatch.get(document,"Tables").toDispatch();
// int count = Dispatch.get(tables,
// "Count").changeType(Variant.VariantInt).getInt(); //document中的表格數量
// Dispatch table = Dispatch.call(tables, "Item", newVariant(
// 1)).toDispatch();//文檔中第一個表格
Dispatch range = Dispatch.get(selection,"Range").toDispatch();// /當前光標位置或者選中的區域
Dispatch newTable = Dispatch.call(tables, "Add", range,
new Variant(row), newVariant(column), new Variant(1))
.toDispatch(); // 設置row,column,表格外框寬度
Dispatch cols = Dispatch.get(newTable,"Columns").toDispatch(); // 此表的所有列,
int colCount = Dispatch.get(cols, "Count").changeType(
Variant.VariantInt).getInt();//一共有多少列 實際上這個數==column
System.out.println(colCount + "列");
for (int i = 1; i <= colCount; i++) { // 循環取出每一列
Dispatch col = Dispatch.call(cols, "Item", newVariant(i))
.toDispatch();
Dispatch cells = Dispatch.get(col, "Cells").toDispatch();// 當前列中單元格
int cellCount = Dispatch.get(cells, "Count").changeType(
Variant.VariantInt).getInt();//當前列中單元格數 實際上這個數等於row
for (int j = 1; j <= cellCount; j++) {// 每一列中的單元格數
// Dispatch cell =Dispatch.call(cells, "Item", new
// Variant(j)).toDispatch(); //當前單元格
// Dispatch cell =Dispatch.call(newTable, "Cell", new
// Variant(j) , new Variant(i)).toDispatch(); //取單元格的另一種方法
// Dispatch.call(cell,"Select");//選中當前單元格
// Dispatch.put(selection,"Text",
// "第"+j+"行,第"+i+"列");//往選中的區域中填值,也就是往當前單元格填值
putTxtToCell(newTable, j, i,"第" + j +"行,第" + i+ "列");// 與上面四句的作用相同
}
}
}
/** */
/**
* 在指定的單元格里填寫數據
*
* @param tableIndex
* @param cellRowIdx
* @param cellColIdx
* @param txt
*/
public void putTxtToCell(Dispatch table, int cellRowIdx, intcellColIdx,
String txt) {
Dispatch cell = Dispatch.call(table, "Cell", newVariant(cellRowIdx),
newVariant(cellColIdx)).toDispatch();
Dispatch.call(cell, "Select");
Dispatch selection = Dispatch.get(MsWordApp,"Selection").toDispatch(); // 輸入內容需要的對象
Dispatch.put(selection, "Text", txt);
}
/** */
/**
* 在指定的單元格里填寫數據
*
* @param tableIndex
* @param cellRowIdx
* @param cellColIdx
* @param txt
*/
public void putTxtToCell(int tableIndex, int cellRowIdx, intcellColIdx,
String txt) {
// 所有表格
Dispatch tables = Dispatch.get(document,"Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item", newVariant(tableIndex))
.toDispatch();
Dispatch cell = Dispatch.call(table, "Cell", newVariant(cellRowIdx),
newVariant(cellColIdx)).toDispatch();
Dispatch.call(cell, "Select");
Dispatch selection = Dispatch.get(MsWordApp,"Selection").toDispatch(); // 輸入內容需要的對象
Dispatch.put(selection, "Text", txt);
}
// 合併兩個單元格
public void mergeCell(Dispatch cell1, Dispatch cell2) {
Dispatch.call(cell1, "Merge", cell2);
}
public void mergeCell(Dispatch table, int row1, int col1, int row2, intcol2) {
Dispatch cell1 = Dispatch.call(table, "Cell", newVariant(row1),
newVariant(col1)).toDispatch();
Dispatch cell2 = Dispatch.call(table, "Cell", newVariant(row2),
newVariant(col2)).toDispatch();
mergeCell(cell1, cell2);
}
public void mergeCellTest() {
Dispatch tables = Dispatch.get(document,"Tables").toDispatch();
int tableCount = Dispatch.get(tables, "Count").changeType(
Variant.VariantInt).getInt();// document中的表格數量
Dispatch table = Dispatch.call(tables, "Item", newVariant(tableCount))
.toDispatch();// 文檔中最後一個table
mergeCell(table, 1, 1, 1, 2);// 將table 中x=1,y=1 與x=1,y=2的兩個單元格合併
}
/**
* 把選定的內容或光標插入點向上移動
*
* @param pos
* 移動的距離
*/
public void moveUp(int pos) {
Dispatch selection = Dispatch.get(MsWordApp,"Selection").toDispatch(); // 輸入內容需要的對象
for (int i = 0; i < pos; i++) {
// MoveDown MoveLeft moveRight
// moveStart ( Dispatch.call(selection, "HomeKey", new Variant(6));
// )
// moveEnd Dispatch.call(selection, "EndKey", newVariant(6));
Dispatch.call(selection, "MoveUp");
}
}
/** */
/**
* 從選定內容或插入點開始查找文本
*
* @param toFindText
* 要查找的文本
* @return boolean true-查找到並選中該文本,false-未查找到文本
*/
public boolean find(String toFindText) {
if (toFindText == null || toFindText.equals(""))
return false;
Dispatch selection = Dispatch.get(MsWordApp,"Selection").toDispatch(); // 輸入內容需要的對象
// 從selection所在位置開始查詢
Dispatch find = Dispatch.call(selection,"Find").toDispatch();
// 設置要查找的內容
Dispatch.put(find, "Text", toFindText);
// 向前查找
Dispatch.put(find, "Forward", "True");
// 設置格式
Dispatch.put(find, "Format", "True");
// 大小寫匹配
Dispatch.put(find, "MatchCase", "True");
// 全字匹配
Dispatch.put(find, "MatchWholeWord", "True");
// 查找並選中
return Dispatch.call(find, "Execute").getBoolean();
}
/** */
/**
* 把選定選定內容設定爲替換文本
*
* @param toFindText
* 查找字符串
* @param newText
* 要替換的內容
* @return
*/
public boolean replaceText(String toFindText, String newText) {
if (!find(toFindText))
return false;
Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();// 輸入內容需要的對象
Dispatch.put(selection, "Text", newText);
return true;
}
public void printFile() {
// Just print the current document to the default printer
Dispatch.call(document, "PrintOut");
}
// 保存文檔的更改
public void save() {
Dispatch.call(document, "Save");
}
public void saveFileAs(String filename) {
Dispatch.call(document, "SaveAs", filename);
}
public void closeDocument() {
// Close the document without saving changes
// 0 = wdDoNotSaveChanges
// -1 = wdSaveChanges
// -2 = wdPromptToSaveChanges
Dispatch.call(document, "Close", new Variant(0));
document = null;
}
public void closeWord() {
Dispatch.call(MsWordApp, "Quit");
MsWordApp = null;
document = null;
}
// 設置wordApp打開後窗口的位置
public void setLocation() {
Dispatch activeWindow = Dispatch.get(MsWordApp,"Application")
.toDispatch();
Dispatch.put(activeWindow, "WindowState", new Variant(1)); //0=default
// 1=maximize
// 2=minimize
Dispatch.put(activeWindow, "Top", new Variant(0));
Dispatch.put(activeWindow, "Left", new Variant(0));
Dispatch.put(activeWindow, "Height", new Variant(600));
Dispatch.put(activeWindow, "width", new Variant(800));
}
}
public class JacobTest2 {
public static void createANewFileTest() {
WordBean wordBean = new WordBean();
// word.openWord(true);// 打開 word 程序
wordBean.setVisible(true);
wordBean.createNewDocument();// 創建一個新文檔
wordBean.setLocation();// 設置打開後窗口的位置
wordBean.insertText("你好");// 向文檔中插入字符
wordBean.insertJpeg("D:" + File.separator + "a.jpg");// 插入圖片
// 如果 ,想保存文件,下面三句
// word.saveFileAs("d:\\a.doc");
// word.closeDocument();
// word.closeWord();
}
public static void openAnExistsFileTest() {
WordBean wordBean = new WordBean();
wordBean.setVisible(true); // 是否前臺打開word 程序,或者後臺運行
wordBean.openFile("d:\\a.doc");
wordBean.insertJpeg("D:" + File.separator +"a.jpg"); // 插入圖片(注意剛打開的word
// ,光標處於開頭,故,圖片在最前方插入)
wordBean.save();
wordBean.closeDocument();
wordBean.closeWord();
}
public static void insertFormatStr(String str) {
WordBean wordBean = new WordBean();
wordBean.setVisible(true); // 是否前臺打開word 程序,或者後臺運行
wordBean.createNewDocument();// 創建一個新文檔
wordBean.insertFormatStr(str);// 插入一個段落,對其中的字體進行了設置
}
public static void insertTableTest() {
WordBean wordBean = new WordBean();
wordBean.setVisible(true); // 是否前臺打開word 程序,或者後臺運行
wordBean.createNewDocument();// 創建一個新文檔
wordBean.setLocation();
wordBean.insertTable("表名", 3, 2);
wordBean.saveFileAs("d:\\table.doc");
wordBean.closeDocument();
wordBean.closeWord();
}
public static void mergeTableCellTest() {
insertTableTest();//生成d:\\table.doc
WordBean wordBean = new WordBean();
wordBean.setVisible(true); // 是否前臺打開word 程序,或者後臺運行
wordBean.openFile("d:\\table.doc");
wordBean.mergeCellTest();
}
public static void main(String[] args) {
// 進行測試前要保證d:\\a.jpg圖片文件存在
// createANewFileTest();//創建一個新文件
// openAnExistsFileTest();// 打開一個存在的文件
// insertFormatStr("格式 化字符串");//對字符串進行一定的修飾
//insertTableTest();// 創建一個表格
mergeTableCellTest();// 對錶格中的單元格進行合併
}
}
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
class WordBean {
//代表一個word 程序
privateActiveXComponent MsWordApp = null;
//代表進行處理的word 文檔
privateDispatch document = null;
publicWordBean() {
//Open Word if we\'ve not done it already
if(MsWordApp == null) {
MsWordApp= new ActiveXComponent("Word.Application");
}
}
//設置是否在前臺打開 word程序 ,
publicvoid setVisible(boolean visible) {
MsWordApp.setProperty("Visible",new Variant(visible));
//這一句作用相同
//Dispatch.put(MsWordApp, "Visible", new Variant(visible));
}
//創建一個新文檔
publicvoid createNewDocument() {
//Find the Documents collection object maintained by Word
//documents表示word的所有文檔窗口,(word是多文檔應用程序)
Dispatchdocuments = Dispatch.get(MsWordApp, "Documents").toDispatch();
//Call the Add method of the Documents collection to create
//a new document to edit
document= Dispatch.call(documents, "Add").toDispatch();
}
//打開一個存在的word文檔,並用document 引用 引用它
publicvoid openFile(String wordFilePath) {
//Find the Documents collection object maintained by Word
// documents表示word的所有文檔窗口,(word是多文檔應用程序)
Dispatchdocuments = Dispatch.get(MsWordApp, "Documents").toDispatch();
document= Dispatch.call(documents, "Open", wordFilePath,
newVariant(true)/* 是否進行轉換ConfirmConversions*/,
newVariant(false)/* 是否只讀*/).toDispatch();
//document = Dispatch.invoke(documents, "Open", Dispatch.Method,
//new Object[] { wordFilePath, new Variant(true),
//new Variant(false)
//}, new int[1]).toDispatch();
}
//向 document 中插入文本內容
publicvoid insertText(String textToInsert) {
//Get the current selection within Word at the moment.
//a new document has just been created then this will be at
//the top of the new doc 獲得選 中的內容,如果是一個新創建的文件,因裏面無內容,則光標應處於文件開頭處
Dispatchselection = Dispatch.get(MsWordApp, "Selection").toDispatch();
//取消選中,應該就是移動光標 ,否則 新添加的內容會覆蓋選中的內容
Dispatch.call(selection,"MoveRight", new Variant(1), new Variant(1));
//Put the specified text at the insertion point
Dispatch.put(selection,"Text", textToInsert);
//取消選中,應該就是移動光標
Dispatch.call(selection,"MoveRight", new Variant(1), new Variant(1));
}
//向文檔中添加 一個圖片,
publicvoid insertJpeg(String jpegFilePath) {
Dispatchselection = Dispatch.get(MsWordApp, "Selection").toDispatch();
Dispatchimage = Dispatch.get(selection, "InLineShapes").toDispatch();
Dispatch.call(image,"AddPicture", jpegFilePath);
}
//段落的處理,插入格式化的文本
publicvoid insertFormatStr(String text) {
DispatchwordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的內容
Dispatch.call(wordContent,"InsertAfter", text);// 插入一個段落到最後
Dispatchparagraphs = Dispatch.get(wordContent, "Paragraphs")
.toDispatch();// 所有段落
intparagraphCount = Dispatch.get(paragraphs, "Count").changeType(
Variant.VariantInt).getInt();//一共的段落數
//找到剛輸入的段落,設置格式
DispatchlastParagraph = Dispatch.call(paragraphs, "Item",
newVariant(paragraphCount)).toDispatch(); // 最後一段(也就是剛插入的)
//Range 對象表示文檔中的一個連續範圍,由一個起始字符位置和一個終止字符位置定義
DispatchlastParagraphRange = Dispatch.get(lastParagraph, "Range")
.toDispatch();
Dispatchfont = Dispatch.get(lastParagraphRange, "Font").toDispatch();
Dispatch.put(font,"Bold", new Variant(true)); // 設置爲黑體
Dispatch.put(font,"Italic", new Variant(true)); // 設置爲斜體
Dispatch.put(font,"Name", new Variant("宋體")); //
Dispatch.put(font, "Size", newVariant(12)); // 小四
Dispatchselection = Dispatch.get(MsWordApp, "Selection").toDispatch();
Dispatch.call(selection,"TypeParagraph");// 插入一個空行
Dispatchalignment = Dispatch.get(selection, "ParagraphFormat")
.toDispatch();//段落格式
Dispatch.put(alignment,"Alignment", "2"); // (1:置中 2:靠右 3:靠左)
}
//word 中在對錶格進行遍歷的時候 ,是先列後行 先column 後cell
//另外下標從1開始
publicvoid insertTable(String tableTitle, int row, int column) {
Dispatchselection = Dispatch.get(MsWordApp, "Selection").toDispatch(); // 輸入內容需要的對象
Dispatch.call(selection,"TypeText", tableTitle); // 寫入標題內容 // 標題格行
Dispatch.call(selection,"TypeParagraph"); // 空一行段落
Dispatch.call(selection,"TypeParagraph"); // 空一行段落
Dispatch.call(selection,"MoveDown"); // 遊標往下一行
//建立表格
Dispatchtables = Dispatch.get(document, "Tables").toDispatch();
//int count = Dispatch.get(tables,
//"Count").changeType(Variant.VariantInt).getInt(); // document中的表格數量
//Dispatch table = Dispatch.call(tables, "Item", new Variant(
//1)).toDispatch();//文檔中第一個表格
Dispatchrange = Dispatch.get(selection, "Range").toDispatch();// /當前光標位置或者選中的區域
DispatchnewTable = Dispatch.call(tables, "Add", range,
newVariant(row), new Variant(column), new Variant(1))
.toDispatch();// 設置row,column,表格外框寬度
Dispatchcols = Dispatch.get(newTable, "Columns").toDispatch(); // 此表的所有列,
intcolCount = Dispatch.get(cols, "Count").changeType(
Variant.VariantInt).getInt();//一共有多少列 實際上這個數==column
System.out.println(colCount+ "列");
for(int i = 1; i <= colCount; i++) { // 循環取出每一列
Dispatchcol = Dispatch.call(cols, "Item", new Variant(i))
.toDispatch();
Dispatchcells = Dispatch.get(col, "Cells").toDispatch();// 當前列中單元格
intcellCount = Dispatch.get(cells, "Count").changeType(
Variant.VariantInt).getInt();//當前列中單元格數 實際上這個數等於row
for(int j = 1; j <= cellCount; j++) {// 每一列中的單元格數
//Dispatch cell = Dispatch.call(cells, "Item", new
//Variant(j)).toDispatch(); //當前單元格
//Dispatch cell = Dispatch.call(newTable, "Cell", new
//Variant(j) , new Variant(i) ).toDispatch(); //取單元格的另一種方法
//Dispatch.call(cell, "Select");//選中當前單元格
//Dispatch.put(selection, "Text",
//"第"+j+"行,第"+i+"列");//往選中的區域中填值,也就是往當前單元格填值
putTxtToCell(newTable,j, i, "第" + j +"行,第" + i+ "列");// 與上面四句的作用相同
}
}
}
/***/
/**
* 在指定的單元格里填寫數據
*
* @param tableIndex
* @param cellRowIdx
* @param cellColIdx
* @param txt
*/
publicvoid putTxtToCell(Dispatch table, int cellRowIdx, int cellColIdx,
Stringtxt) {
Dispatchcell = Dispatch.call(table, "Cell", new Variant(cellRowIdx),
newVariant(cellColIdx)).toDispatch();
Dispatch.call(cell,"Select");
Dispatchselection = Dispatch.get(MsWordApp, "Selection").toDispatch(); // 輸入內容需要的對象
Dispatch.put(selection,"Text", txt);
}
/***/
/**
* 在指定的單元格里填寫數據
*
* @param tableIndex
* @param cellRowIdx
* @param cellColIdx
* @param txt
*/
publicvoid putTxtToCell(int tableIndex, int cellRowIdx, int cellColIdx,
Stringtxt) {
//所有表格
Dispatchtables = Dispatch.get(document, "Tables").toDispatch();
//要填充的表格
Dispatchtable = Dispatch.call(tables, "Item", new Variant(tableIndex))
.toDispatch();
Dispatchcell = Dispatch.call(table, "Cell", new Variant(cellRowIdx),
newVariant(cellColIdx)).toDispatch();
Dispatch.call(cell,"Select");
Dispatchselection = Dispatch.get(MsWordApp, "Selection").toDispatch(); // 輸入內容需要的對象
Dispatch.put(selection,"Text", txt);
}
//合併兩個單元格
publicvoid mergeCell(Dispatch cell1, Dispatch cell2) {
Dispatch.call(cell1, "Merge",cell2);
}
publicvoid mergeCell(Dispatch table, int row1, int col1, int row2, int col2) {
Dispatchcell1 = Dispatch.call(table, "Cell", new Variant(row1),
newVariant(col1)).toDispatch();
Dispatchcell2 = Dispatch.call(table, "Cell", new Variant(row2),
newVariant(col2)).toDispatch();
mergeCell(cell1,cell2);
}
publicvoid mergeCellTest() {
Dispatchtables = Dispatch.get(document, "Tables").toDispatch();
inttableCount = Dispatch.get(tables, "Count").changeType(
Variant.VariantInt).getInt();// document中的表格數量
Dispatchtable = Dispatch.call(tables, "Item", new Variant(tableCount))
.toDispatch();//文檔中最後一個table
mergeCell(table,1, 1, 1, 2);// 將table 中x=1,y=1 與x=1,y=2的兩個單元格合併
}
//========================================================
/***/
/**
* 把選定的內容或光標插入點向上移動
*
* @param pos
* 移動的距離
*/
publicvoid moveUp(int pos) {
Dispatchselection = Dispatch.get(MsWordApp, "Selection").toDispatch(); // 輸入內容需要的對象
for(int i = 0; i < pos; i++) {
//MoveDown MoveLeft moveRight
//moveStart ( Dispatch.call(selection, "HomeKey", new Variant(6));
//)
//moveEnd Dispatch.call(selection, "EndKey", new Variant(6));
Dispatch.call(selection,"MoveUp");
}
}
/***/
/**
* 從選定內容或插入點開始查找文本
*
* @param toFindText
* 要查找的文本
* @return boolean true-查找到並選中該文本,false-未查找到文本
*/
publicboolean find(String toFindText) {
if(toFindText == null || toFindText.equals(""))
returnfalse;
Dispatchselection = Dispatch.get(MsWordApp, "Selection").toDispatch(); // 輸入內容需要的對象
//從selection所在位置開始查詢
Dispatchfind = Dispatch.call(selection, "Find").toDispatch();
//設置要查找的內容
Dispatch.put(find,"Text", toFindText);
//向前查找
Dispatch.put(find,"Forward", "True");
//設置格式
Dispatch.put(find,"Format", "True");
//大小寫匹配
Dispatch.put(find,"MatchCase", "True");
//全字匹配
Dispatch.put(find,"MatchWholeWord", "True");
//查找並選中
returnDispatch.call(find, "Execute").getBoolean();
}
/***/
/**
* 把選定選定內容設定爲替換文本
*
* @param toFindText
* 查找字符串
* @param newText
* 要替換的內容
* @return
*/
publicboolean replaceText(String toFindText, String newText) {
if(!find(toFindText))
returnfalse;
Dispatchselection = Dispatch.get(MsWordApp, "Selection").toDispatch(); // 輸入內容需要的對象
Dispatch.put(selection,"Text", newText);
returntrue;
}
publicvoid printFile() {
//Just print the current document to the default printer
Dispatch.call(document,"PrintOut");
}
//保存文檔的更改
publicvoid save() {
Dispatch.call(document,"Save");
}
publicvoid saveFileAs(String filename) {
Dispatch.call(document,"SaveAs", filename);
}
publicvoid closeDocument() {
//Close the document without saving changes
//0 = wdDoNotSaveChanges
//-1 = wdSaveChanges
//-2 = wdPromptToSaveChanges
Dispatch.call(document,"Close", new Variant(0));
document= null;
}
publicvoid closeWord() {
Dispatch.call(MsWordApp,"Quit");
MsWordApp= null;
document= null;
}
//設置wordApp打開後窗口的位置
publicvoid setLocation() {
DispatchactiveWindow = Dispatch.get(MsWordApp, "Application")
.toDispatch();
Dispatch.put(activeWindow,"WindowState", new Variant(1)); // 0=default
//1=maximize
//2=minimize
Dispatch.put(activeWindow,"Top", new Variant(0));
Dispatch.put(activeWindow,"Left", new Variant(0));
Dispatch.put(activeWindow,"Height", new Variant(600));
Dispatch.put(activeWindow,"width", new Variant(800));
}
}
public class JacobTest2 {
publicstatic void createANewFileTest() {
WordBeanwordBean = new WordBean();
//word.openWord(true);// 打開 word 程序
wordBean.setVisible(true);
wordBean.createNewDocument();//創建一個新文檔
wordBean.setLocation();//設置打開後窗口的位置
wordBean.insertText("你好");// 向文檔中插入字符
wordBean.insertJpeg("D:"+ File.separator + "a.jpg"); // 插入圖片
//如果 ,想保存文件,下面三句
//word.saveFileAs("d:\\a.doc");
//word.closeDocument();
//word.closeWord();
}
publicstatic void openAnExistsFileTest() {
WordBeanwordBean = new WordBean();
wordBean.setVisible(true);// 是否前臺打開word 程序,或者後臺運行
wordBean.openFile("d:\\a.doc");
wordBean.insertJpeg("D:"+ File.separator + "a.jpg"); // 插入圖片(注意剛打開的word
//,光標處於開頭,故,圖片在最前方插入)
wordBean.save();
wordBean.closeDocument();
wordBean.closeWord();
}
publicstatic void insertFormatStr(String str) {
WordBeanwordBean = new WordBean();
wordBean.setVisible(true);// 是否前臺打開word 程序,或者後臺運行
wordBean.createNewDocument();//創建一個新文檔
wordBean.insertFormatStr(str);//插入一個段落,對其中的字體進行了設置
}
publicstatic void insertTableTest() {
WordBeanwordBean = new WordBean();
wordBean.setVisible(true);// 是否前臺打開word 程序,或者後臺運行
wordBean.createNewDocument();//創建一個新文檔
wordBean.setLocation();
wordBean.insertTable("表名", 3, 2);
wordBean.saveFileAs("d:\\table.doc");
wordBean.closeDocument();
wordBean.closeWord();
}
publicstatic void mergeTableCellTest() {
insertTableTest();//生成d:\\table.doc
WordBeanwordBean = new WordBean();
wordBean.setVisible(true);// 是否前臺打開word 程序,或者後臺運行
wordBean.openFile("d:\\table.doc");
wordBean.mergeCellTest();
}
publicstatic void main(String[] args) {
//進行測試前要保證d:\\a.jpg圖片文件存在
//createANewFileTest();//創建一個新文件
//openAnExistsFileTest();// 打開一個存在 的文件
//insertFormatStr("格式 化字符串");//對字符串進行一定的修飾
//insertTableTest();//創建一個表格
mergeTableCellTest();// 對錶格中的單元格進行合併
}
}