最近在做項目的時候需要這麼一個功能
下面是借鑒了別人已經包裝好了的代碼
import com
import
import
public class WordHandle{
//運行時的Word程序
private ActiveXComponent app;
//word對象
private Dispatch words;
//當前的word文檔
private Dispatch doc;
//當前光標位置
private Dispatch cursor;
//當前文檔是否只讀
private boolean readOnly;
//當前文檔中所有表格
private Dispatch tables;
//當前所在表格
private Dispatch table;
private int count;
public WordHandle()
{
this
this
words = this
this
this
this
unt =
}
public boolean open(String fileName
{
if (doc != null)
{
System
return false;
}
this
this
this
this
unt = Dispatch
System
return true;
}
public boolean newFile() throws Exception
{
if (doc != null)
{
System
return false;
}
this
this
this
this
System
return true;
}
public boolean close()
{
String fileName = null;
if (this
{
try
{
fileName = Dispatch
Dispatch
}
catch (Exception e)
{
e
}
finally
{
this
}
}
System
return true;
}
public boolean quit()
{
try
{
this
}
catch (Exception e)
{
e
}
System
return true;
}
public boolean saveAs(String fileName) throws Exception
{
if (this
{
System
return false;
}
else
{
Dispatch
System
return true;
}
}
public boolean save() throws Exception
{
if (this
{
System
return false;
}
else
{
if (this
{
System
return false;
}
Dispatch
System
return true;
}
}
public boolean moveRight(int steps) throws Exception
{
//int start = Dispatch
//Dispatch
for (int i=
{
Dispatch
}
return true;
}
public boolean moveLeft(int steps) throws Exception
{
for (int i=
{
Dispatch
}
return true;
}
public int search(String str) throws Exception
{
// 從cursor所在位置開始查詢
Dispatch find = Dispatch
// 設置要查找的內容
Dispatch
// 向前查找
Dispatch
// 設置格式
Dispatch
// 大小寫匹配
Dispatch
// 全字匹配
Dispatch
// 查找
if (!Dispatch
return
else
{
return Dispatch
}
}
public int searchOnly(String str) throws Exception
{
// 從cursor所在位置開始查詢
Dispatch find = Dispatch
// 設置要查找的內容
Dispatch
// 向前查找
Dispatch
// 大小寫匹配
Dispatch
// 全字匹配
Dispatch
if (!Dispatch
return
else
{
int start = Dispatch
Dispatch
//System
return start;
}
}
public String getBetween(int start
{
Dispatch range = Dispatch
Dispatch
return Dispatch
}
public String getLineAfter(int start) throws Exception
{
Dispatch
int length = Dispatch
return getBetween(start
}
public String getLine(int position) throws Exception
{
Dispatch
Dispatch
int start = Dispatch
int end = Dispatch
return getBetween(start
}
public boolean gotoPage(int index) throws Exception
{
Dispatch
//Dispatch
return true;
}
public int getCurrentCursor() throws Exception
{
return Dispatch
}
public boolean setCursorMode() throws Exception
{
Dispatch
return true;
}
public boolean gotoHome() throws Exception
{
Dispatch
return true;
}
public boolean insert(int steps
{
int start = Dispatch
Dispatch
Dispatch
//this
Dispatch
//System
return true;
}
public boolean replace(String str) throws Exception
{
Dispatch
return true;
}
public int getTableNum() throws Exception
{
return Dispatch
}
public boolean setCurrentTable(int index) throws Exception
{
this
Dispatch
return true;
}
public String getCell(int row
{
Dispatch cell = Dispatch
Dispatch
String tmp = Dispatch
//System
if (tmp
{
return tmp
}
else
return
}
public boolean replaceCell(int row
{
Dispatch cell = Dispatch
Dispatch
Dispatch
return true;
}
public static void main(String args[])
{
WordHandle word = new WordHandle();
try
{
word
System
word
word
word
word
word
word
word
word
word
}
catch (Exception e)
{
}
}
}
當然要運行上面的代碼需要下載jacob
還需要將jacob
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26729.html