最近要做关于关于操作word的东西,因此找到了jacob,但是不会用,google找到了下面的这个类,感谢这个类的作者,感谢google,感谢jacob。
研究了两天也只是勉强能输出个简单的word文档,其中的一些经验需要记录下
环境:jdk1.5.0,tomcat5.024,jacob1.7,jacob1.9
为什么又有1.7又有1.9呢,是因为1.7的jacob.dll不好用,1.9的jacob.dll配合1.7的jacob.jar就好用了,我也不知道为什么,呵呵,关于java的一些开源jar的版本问题一直很困扰我
安装:将jacob.dll放到jdk1.5的bin目录下,jacob.jar放到classpath下就可以了
使用:下面的类提供了一些基本的操作,但还是缺少一定的灵活性。
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class WordBean {
// word文档
private Dispatch doc;
// word运行程序对象
private ActiveXComponent word;
// 所有word文档集合
private Dispatch documents;
// 选定的范围或插入点
private Dispatch selection;
private boolean saveOnExit = true;
public WordBean() {
if (word == null) {
word = new
ActiveXComponent("Word.Application");
word.setProperty("Visible",
new Variant(false));
}
if (documents == null)
documents =
word.getProperty("Documents").toDispatch();
}
public void setSaveOnExit(boolean saveOnExit)
{
this.saveOnExit = saveOnExit;
}
public void createNewDocument() {
doc = Dispatch.call(documents,
"Add").toDispatch();
selection = Dispatch.get(word,
"Selection").toDispatch();
}
public void openDocument(String docPath) {
closeDocument();
doc = Dispatch.call(documents, "Open",
docPath).toDispatch();
selection = Dispatch.get(word,
"Selection").toDispatch();
}
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");
}
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");
}
public void moveLeft(int pos) {
if (selection == null)
selection = Dispatch.get(word,
"Selection").toDispatch();
for (int i = 0; i < pos; i++)
{
Dispatch.call(selection,
"MoveLeft");
}
}
public void moveRight(int pos) {
if (selection == null)
selection = Dispatch.get(word,
"Selection").toDispatch();
for (int i = 0; i < pos;
i++)
Dispatch.call(selection,
"MoveRight");
}
public void moveStart() {
if (selection == null)
selection = Dispatch.get(word,
"Selection").toDispatch();
Dispatch.call(selection, "HomeKey", new
Variant(6));
}
public boolean find(String toFindText) {
if (toFindText == null ||
toFindText.equals(""))
return false;
// 从selection所在位置开始查询
Dispatch find = word.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();
}
public boolean replaceText(String toFindText,
String newText) {
if (!find(toFindText))
return false;
Dispatch.put(selection, "Text", newText);
return true;
}
public void replaceAllText(String toFindText,
String newText) {
while (find(toFindText)) {
Dispatch.put(selection,
"Text", newText);
Dispatch.call(selection,
"MoveRight");
}
}
public void insertText(String newText) {
Dispatch.put(selection, "Text", newText);
}
public boolean replaceImage(String toFindText,
String imagePath) {
if (!find(toFindText))
return false;
Dispatch.call(Dispatch.get(selection,
"InLineShapes").toDispatch(),
"AddPicture", imagePath);
return true;
}
public void replaceAllImage(String toFindText,
String imagePath) {
while (find(toFindText)) {
Dispatch.call(Dispatch.get(selection,
"InLineShapes").toDispatch(),
"AddPicture", imagePath);
Dispatch.call(selection,
"MoveRight");
}
}
public void insertImage(String imagePath) {
Dispatch.call(Dispatch.get(selection,
"InLineShapes").toDispatch(),
"AddPicture", imagePath);
}
public void mergeCell(int tableIndex, int
fstCellRowIdx, int fstCellColIdx,
int 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();
Dispatch secCell = Dispatch.call(table,
"Cell",
new
Variant(secCellRowIdx), new Variant(secCellColIdx))
.toDispatch();
Dispatch.call(fstCell, "Merge", secCell);
}
public void putTxtToCell(int tableIndex, int
cellRowIdx, 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();
Dispatch.call(cell, "Select");
Dispatch.put(selection, "Text", txt);
}
public void
pasteExcelSheet(String pos){
moveStart();
if (this.find(pos)) {
Dispatch textRange =
Dispatch.get(selection, "Range").toDispatch();
Dispatch.call(textRange,
"Paste");
}
}
public void copyTable(String pos, int tableIndex)
{
// 所有表格
Dispatch 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();
Dispatch.call(textRange,
"Paste");
}
}
public void copyTableFromAnotherDoc(String
anotherDocPath, int tableIndex,
String pos) {
Dispatch doc2 = null;
try {
doc2 =
Dispatch.call(documents, "Open", anotherDocPath)
.toDispatch();
// 所有表格
Dispatch tables =
Dispatch.get(doc2, "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();
Dispatch.call(textRange, "Paste");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (doc2 != null) {
Dispatch.call(doc2, "Close", new Variant(saveOnExit));
doc2 =
null;
}
}
}
public void
copyImageFromAnotherDoc(String anotherDocPath,int shapeIndex,String
pos){
Dispatch doc2 = null;
try {
doc2 =
Dispatch.call(documents, "Open", anotherDocPath)
.toDispatch();
Dispatch shapes =
Dispatch.get(doc2, "InLineShapes").toDispatch();
Dispatch 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) {
e.printStackTrace();
} finally {
if (doc2 != null) {
Dispatch.call(doc2, "Close", new Variant(saveOnExit));
doc2 =
null;
}
}
}
public void createTable(String pos, int numCols,
int numRows) {
if (find(pos)) {
Dispatch tables =
Dispatch.get(doc, "Tables").toDispatch();
Dispatch range =
Dispatch.get(selection, "Range").toDispatch();
Dispatch newTable =
Dispatch.call(tables, "Add", range,
new Variant(numRows), new Variant(numCols)).toDispatch();
Dispatch.call(selection,
"MoveRight");
}else{
Dispatch
tables = Dispatch.get(doc, "Tables").toDispatch();
Dispatch
range = Dispatch.get(selection, "Range").toDispatch();
Dispatch
newTable = Dispatch.call(tables, "Add", range,
new Variant(numRows), new Variant(numCols)).toDispatch();
Dispatch.call(selection, "MoveRight");
}
}
public void addTableRow(int tableIndex, int
rowIndex) {
// 所有表格
Dispatch tables = Dispatch.get(doc,
"Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item",
new Variant(tableIndex))
.toDispatch();
// 表格的所有行
Dispatch rows = Dispatch.get(table,
"Rows").toDispatch();
Dispatch row = Dispatch.call(rows, "Item", new
Variant(rowIndex))
.toDispatch();
Dispatch.call(rows, "Add", new
Variant(row));
}
public void addFirstTableRow(int tableIndex)
{
// 所有表格
Dispatch tables = Dispatch.get(doc,
"Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item",
new Variant(tableIndex))
.toDispatch();
// 表格的所有行
Dispatch rows = Dispatch.get(table,
"Rows").toDispatch();
Dispatch row = Dispatch.get(rows,
"First").toDispatch();
Dispatch.call(rows, "Add", new
Variant(row));
}
public void addLastTableRow(int tableIndex)
{
// 所有表格
Dispatch tables = Dispatch.get(doc,
"Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item",
new Variant(tableIndex))
.toDispatch();
// 表格的所有行
Dispatch rows = Dispatch.get(table,
"Rows").toDispatch();
Dispatch row = Dispatch.get(rows,
"Last").toDispatch();
Dispatch.call(rows, "Add", new
Variant(row));
}
public void addRow(int tableIndex) {
Dispatch tables = Dispatch.get(doc,
"Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item",
new Variant(tableIndex))
.toDispatch();
// 表格的所有行
Dispatch rows = Dispatch.get(table,
"Rows").toDispatch();
Dispatch.call(rows, "Add");
}
public void addCol(int tableIndex) {
// 所有表格
Dispatch tables = Dispatch.get(doc,
"Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item",
new Variant(tableIndex))
.toDispatch();
// 表格的所有行
Dispatch cols = Dispatch.get(table,
"Columns").toDispatch();
Dispatch.call(cols, "Add").toDispatch();
Dispatch.call(cols, "AutoFit");
}
public void addTableCol(int tableIndex, int
colIndex) {
// 所有表格
Dispatch tables = Dispatch.get(doc,
"Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item",
new Variant(tableIndex))
.toDispatch();
// 表格的所有行
Dispatch cols = Dispatch.get(table,
"Columns").toDispatch();
System.out.println(Dispatch.get(cols,
"Count"));
Dispatch col = Dispatch.call(cols, "Item", new
Variant(colIndex))
.toDispatch();
// Dispatch col = Dispatch.get(cols,
"First").toDispatch();
Dispatch.call(cols, "Add",
col).toDispatch();
Dispatch.call(cols, "AutoFit");
}
public void addFirstTableCol(int tableIndex)
{
Dispatch tables = Dispatch.get(doc,
"Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item",
new Variant(tableIndex))
.toDispatch();
// 表格的所有行
Dispatch cols = Dispatch.get(table,
"Columns").toDispatch();
Dispatch col = Dispatch.get(cols,
"First").toDispatch();
Dispatch.call(cols, "Add",
col).toDispatch();
Dispatch.call(cols, "AutoFit");
}
public void addLastTableCol(int tableIndex)
{
Dispatch tables = Dispatch.get(doc,
"Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item",
new Variant(tableIndex))
.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");
}
public void
autoFitTable(){
Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
int count = Dispatch.get(tables, "Count").toInt();
for(int i=0;i
Dispatch table = Dispatch.call(tables, "Item",
new Variant(i+1))
.toDispatch();
Dispatch cols = Dispatch.get(table, "Columns").toDispatch();
Dispatch.call(cols, "AutoFit");
}
}
public void
callWordMacro(){
Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
int count = Dispatch.get(tables, "Count").toInt();
Variant vMacroName = new
Variant("Normal.NewMacros.tableFit");
Variant vParam = new Variant("param1");
Variant para[]=new Variant[]{vMacroName};
for(int
i=0;i
Variant(i+1))
.toDispatch();
Dispatch.call(table, "Select");
Dispatch.call(word,"Run","tableFitContent");
}
}
public void setFont(boolean bold, boolean italic,
boolean underLine,
String colorSize, String size,
String name) {
Dispatch font = Dispatch.get(selection,
"Font").toDispatch();
Dispatch.put(font, "Name", new
Variant(name));
Dispatch.put(font, "Bold", new
Variant(bold));
Dispatch.put(font, "Italic", new
Variant(italic));
Dispatch.put(font, "Underline", new
Variant(underLine));
Dispatch.put(font, "Color", colorSize);
Dispatch.put(font, "Size", size);
}
public void save(String savePath) {
Dispatch.call(Dispatch.call(word,
"WordBasic").getDispatch(),
"FileSaveAs", savePath);
}
public void closeDocument(int val) {
Dispatch.call(doc, "Close", new
Variant(val));
doc = null;
}
public void closeDocument() {
if (doc != null) {
Dispatch.call(doc,
"Save");
Dispatch.call(doc, "Close",
new Variant(saveOnExit));
doc = null;
}
}
public void close() {
//closeDocument();
if (word != null) {
Dispatch.call(word,
"Quit");
word = null;
}
selection = null;
documents = null;
}
public void printFile() {
if (doc != null) {
Dispatch.call(doc,
"PrintOut");
}
}
public static void main(String args[]) {
WordBean msWordManager = new WordBean();
try {
msWordManager.openDocument("c:\\asdf1.doc");
msWordManager.addRow(1);
msWordManager.putTxtToCell(1,
4, 1, "gaga");
} catch (Exception e) {
e.printStackTrace();
}finally{
msWordManager.close();
}
}
}