java

位置:IT落伍者 >> java >> 浏览文章

java操作word


发布日期:2024年01月12日
 
java操作word

最近在做项目的时候需要这么一个功能客户有一大堆word格式的模板需要我们用程序向模板里面填充一些数据如果是直接重新写一个Word用POI或Itext都可以搞定关键是读取并解析而且Word里有表格图片等其他东西这两个框架要解析就比较麻烦然而用jacob却可以轻松搞定

下面是借鑒了别人已经包装好了的代码

import comjacobactiveXActiveXComponent;

import Dispatch;

import Variant;

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()

{

thisapp = new ActiveXComponent(WordApplication);

thisappsetProperty(Visible new Variant(false)); // 设置word不可见

words = thisappgetProperty(Documents)toDispatch();

thisdoc = null;

thiscursor = null;

thisreadOnly = true;

unt = ;

}

public boolean open(String fileName boolean readOnly) throws Exception

{

if (doc != null)

{

Systemoutprintln(当前文件未关闭);

return false;

}

thisdoc = Dispatchinvoke(thiswords Open DispatchMethod new Object[] {fileName new Variant(false) new Variant(readOnly)} new int[])toDispatch();

thiscursor = appgetProperty(Selection)toDispatch();

thistables = Dispatchget(thisdocTables)toDispatch();

thisreadOnly = readOnly;

unt = Dispatchget(Dispatchget(thisdoc Words)toDispatch() Count)getInt() ;

Systemoutprintln(打开文件 + fileName + (readOnly ? ReadOnly : Writable));

return true;

}

public boolean newFile() throws Exception

{

if (doc != null)

{

Systemoutprintln(当前文件未关闭);

return false;

}

thisdoc = Dispatchcall(thiswords Add)toDispatch();

thisreadOnly = false;

thiscursor = appgetProperty(Selection)toDispatch();

thistables = Dispatchget(thisdocTables)toDispatch();

Systemoutprintln(新建word文档);

return true;

}

public boolean close()

{

String fileName = null;

if (thisdoc != null)

{

try

{

fileName = Dispatchget(thisdoc Name)getString();

Dispatchcall(thisdoc Close new Variant(false));

}

catch (Exception e)

{

eprintStackTrace();

}

finally

{

thisdoc = null;

}

}

Systemoutprintln(关闭文件 + fileName);

return true;

}

public boolean quit()

{

try

{

thisappinvoke(Quit new Variant[] {});

}

catch (Exception e)

{

eprintStackTrace();

}

Systemoutprintln(退出word);

return true;

}

public boolean saveAs(String fileName) throws Exception

{

if (thisdoc == null)

{

Systemoutprintln(当前无文件);

return false;

}

else

{

Dispatchcall(thisdoc SaveAs fileName);

Systemoutprintln(另存为 + fileName);

return true;

}

}

public boolean save() throws Exception

{

if (thisdoc == null)

{

Systemoutprintln(当前无文档无法保存);

return false;

}

else

{

if (thisreadOnly)

{

Systemoutprintln(只读文档保存失败);

return false;

}

Dispatchcall(thisdoc Save);

Systemoutprintln(保存完成);

return true;

}

}

public boolean moveRight(int steps) throws Exception

{

//int start = Dispatchget(thiscursor Start)getInt();

//Dispatchput(thiscursor Start start + steps);

for (int i=; i<steps; i++)

{

Dispatchcall(cursor MoveRight);

}

return true;

}

public boolean moveLeft(int steps) throws Exception

{

for (int i=; i<steps; i++)

{

Dispatchcall(cursor MoveLeft);

}

return true;

}

public int search(String str) throws Exception

{

// 从cursor所在位置开始查询

Dispatch find = Dispatchcall(thiscursor Find)toDispatch();

// 设置要查找的内容

Dispatchput(find Text str);

// 向前查找

Dispatchput(find Forward True);

// 设置格式

Dispatchput(find Format True);

// 大小写匹配

Dispatchput(find MatchCase True);

// 全字匹配

Dispatchput(find MatchWholeWord True);

// 查找

if (!Dispatchcall(findExecute)getBoolean())

return ;

else

{

return Dispatchget(thiscursor Start)getInt();

}

}

public int searchOnly(String str) throws Exception

{

// 从cursor所在位置开始查询

Dispatch find = Dispatchcall(thiscursor Find)toDispatch();

// 设置要查找的内容

Dispatchput(find Text str);

// 向前查找

Dispatchput(find Forward True);

// 大小写匹配

Dispatchput(find MatchCase True);

// 全字匹配

Dispatchput(find MatchWholeWord True);

if (!Dispatchcall(findExecute)getBoolean())

return ;

else

{

int start = Dispatchget(thiscursor Start)getInt();

Dispatchput(thiscursor End unt);

//Systemoutprintln(start);

return start;

}

}

public String getBetween(int start int end) throws Exception

{

Dispatch range = Dispatchget(thiscursor Range)toDispatch();

Dispatchcall(rangeSetRange start end);

return Dispatchget(range Text)getString();

}

public String getLineAfter(int start) throws Exception

{

Dispatchput(thiscursor Start start);

int length = Dispatchcall(thiscursor EndKey)getInt() + start;

return getBetween(start length);

}

public String getLine(int position) throws Exception

{

Dispatchput(thiscursor Start position);

Dispatchcall(thiscursor SelectRow);

int start = Dispatchget(thiscursor Start)getInt();

int end = Dispatchget(thiscursor End)getInt();

return getBetween(start start + end);

}

public boolean gotoPage(int index) throws Exception

{

Dispatchinvoke(thiscursor Goto DispatchMethod new Object[] { StringvalueOf(index)} new int[]);

//Dispatchcall(thiscursor GoTo wdGoToLine wdGoToNext StringvalueOf(index) null);

return true;

}

public int getCurrentCursor() throws Exception

{

return Dispatchget(thiscursor Start)getInt();

}

public boolean setCursorMode() throws Exception

{

Dispatchput(thiscursor End Dispatchget(thiscursor Start)getInt());

return true;

}

public boolean gotoHome() throws Exception

{

Dispatchput(thiscursor Start );

return true;

}

public boolean insert(int steps String str) throws Exception

{

int start = Dispatchget(thiscursor Start)getInt() + steps;

Dispatchput(thiscursor Start start);

Dispatchcall(thiscursor InsertBefore str);

//thisgetCount();

Dispatchput(thiscursor Start start + strlength());

//Systemoutprintln(Dispatchget(thiscursor Start)getInt() + + (Dispatchget(thiscursor Start)getInt()+Dispatchget(thiscursor End)getInt()));

return true;

}

public boolean replace(String str) throws Exception

{

Dispatchput(thiscursor Text str);

return true;

}

public int getTableNum() throws Exception

{

return Dispatchget(thistables Count)getInt();

}

public boolean setCurrentTable(int index) throws Exception

{

thistable = Dispatchcall(thistables Item new Variant(index))toDispatch();

Dispatchcall(thistable Select);

return true;

}

public String getCell(int row int col) throws Exception

{

Dispatch cell = Dispatchcall(table Cell IntegertoString(row) IntegertoString(col))toDispatch();

Dispatchcall(cellSelect);

String tmp = Dispatchget(thiscursor Text)getString();

//Systemoutprintln( + tmp);

if (tmplength() > )

{

return tmpsubstring( tmplength() );

}

else

return ;

}

public boolean replaceCell(int row int col String str) throws Exception

{

Dispatch cell = Dispatchcall(tableCell IntegertoString(row) IntegertoString(col))toDispatch();

Dispatchcall(cell Select);

Dispatchput(thiscursor Text str);

return true;

}

public static void main(String args[])

{

WordHandle word = new WordHandle();

try

{

wordopen(D://doc//tax//开业登记合并事项实地调查表doc false);

Systemoutprintln(wordgetTableNum());

wordsetCurrentTable();

wordreplaceCell( old Name);

wordreplaceCell( 经营范围);

wordreplaceCell( );

wordreplaceCell( );

wordreplaceCell( );

wordsaveAs(D://开业登记合并事项实地调查表doc);

wordclose();

wordquit();

}

catch (Exception e)

{

}

}

}

当然要运行上面的代码需要下载jacob下载地址为project

还需要将jacobdll放入C:\WINDOWS\systemjacobjar放在classpath下

               

上一篇:现在企业流行的java框架技术

下一篇:Java编程风格与命名规范整理