java *** 作word

java *** 作word,第1张

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

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

import jacob activeX ActiveXComponent

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

{

this app = new ActiveXComponent( Word Application )

this app setProperty( Visible new Variant(false))    // 设置word不可见

words = this app getProperty( Documents ) toDispatch()

this doc = null

this cursor = null

this readOnly = true

unt =

}

public boolean open(String fileName boolean readOnly) throws Exception

{

if (doc != null)

{

System out println( 当前文件未关闭 )

return false

}

this doc = Dispatch invoke(this words Open Dispatch Method new Object[] {fileName new Variant(false) new Variant(readOnly)} new int[ ]) toDispatch()

this cursor = app getProperty( Selection ) toDispatch()

this tables = Dispatch get(this doc Tables ) toDispatch()

this readOnly = readOnly

unt = Dispatch get(Dispatch get(this doc Words ) toDispatch() Count ) getInt()

System out println( 打开文件 + fileName + (readOnly ? ReadOnly : Writable ))

return true

}

public boolean newFile() throws Exception

{

if (doc != null)

{

System out println( 当前文件未关闭 )

return false

}

this doc = Dispatch call(this words Add ) toDispatch()

this readOnly = false

this cursor = app getProperty( Selection ) toDispatch()

this tables = Dispatch get(this doc Tables ) toDispatch()

System out println( 新建word文档 )

return true

}

public boolean close()

{

String fileName = null

if (this doc != null)

{

try

{

fileName = Dispatch get(this doc Name ) getString()

Dispatch call(this doc Close new Variant(false))

}

catch (Exception e)

{

e printStackTrace()

}

finally

{

this doc = null

}

}

System out println( 关闭文件 + fileName)

return true

}

public boolean quit()

{

try

{

this app invoke( Quit new Variant[] {})

}

catch (Exception e)

{

e printStackTrace()

}

System out println( 退出word )

return true

}

public boolean saveAs(String fileName) throws Exception

{

if (this doc == null)

{

System out println( 当前无文件 )

return false

}

else

{

Dispatch call(this doc SaveAs fileName)

System out println( 另存为 + fileName)

return true

}

}

public boolean save() throws Exception

{

if (this doc == null)

{

System out println( 当前无文档 无法保存 )

return false

}

else

{

if (this readOnly)

{

System out println( 只读文档 保存失败 )

return false

}

Dispatch call(this doc Save )

System out println( 保存完成 )

return true

}

}

public boolean moveRight(int steps) throws Exception

{

//int start = Dispatch get(this cursor Start ) getInt()

//Dispatch put(this cursor Start start + steps)

for (int i= i<stepsi++)

{

Dispatch call(cursor MoveRight )

}

return true

}

public boolean moveLeft(int steps) throws Exception

{

for (int i= i<stepsi++)

{

Dispatch call(cursor MoveLeft )

}

return true

}

public int search(String str) throws Exception

{

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

Dispatch find = Dispatch call(this cursor Find ) toDispatch()

// 设置要查找的内容

Dispatch put(find Text str)

// 向前查找

Dispatch put(find Forward True )

// 设置格式

Dispatch put(find Format True )

// 大小写匹配

Dispatch put(find MatchCase True )

// 全字匹配

Dispatch put(find MatchWholeWord True )

// 查找

if (!Dispatch call(find Execute ) getBoolean())

return

else

{

return Dispatch get(this cursor Start ) getInt()

}

}

public int searchOnly(String str) throws Exception

{

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

Dispatch find = Dispatch call(this cursor Find ) toDispatch()

// 设置要查找的内容

Dispatch put(find Text str)

// 向前查找

Dispatch put(find Forward True )

// 大小写匹配

Dispatch put(find MatchCase True )

// 全字匹配

Dispatch put(find MatchWholeWord True )

if (!Dispatch call(find Execute ) getBoolean())

return

else

{

int start = Dispatch get(this cursor Start ) getInt()

Dispatch put(this cursor End unt)

//System out println(start)

return start

}

}

public String getBeeen(int start int end) throws Exception

{

Dispatch range = Dispatch get(this cursor Range ) toDispatch()

Dispatch call(range SetRange start end)

return Dispatch get(range Text ) getString()

}

public String getLineAfter(int start) throws Exception

{

Dispatch put(this cursor Start start)

int length = Dispatch call(this cursor EndKey ) getInt() + start

return getBeeen(start length)

}

public String getLine(int position) throws Exception

{

Dispatch put(this cursor Start position)

Dispatch call(this cursor SelectRow )

int start = Dispatch get(this cursor Start ) getInt()

int end = Dispatch get(this cursor End ) getInt()

return getBeeen(start start + end)

}

public boolean gotoPage(int index) throws Exception

{

Dispatch invoke(this cursor Goto Dispatch Method new Object[] { String valueOf(index)} new int[ ])

//Dispatch call(this cursor GoTo wdGoToLine wdGoToNext String valueOf(index) null)

return true

}

public int getCurrentCursor() throws Exception

{

return Dispatch get(this cursor Start ) getInt()

}

public boolean setCursorMode() throws Exception

{

Dispatch put(this cursor End Dispatch get(this cursor Start ) getInt())

return true

}

public boolean gotoHome() throws Exception

{

Dispatch put(this cursor Start )

return true

}

public boolean insert(int steps String str) throws Exception

{

int start = Dispatch get(this cursor Start ) getInt() + steps

Dispatch put(this cursor Start start)

Dispatch call(this cursor InsertBefore str)

//this getCount()

Dispatch put(this cursor Start start + str length())

//System out println(Dispatch get(this cursor Start ) getInt() +    + (Dispatch get(this cursor Start ) getInt()+Dispatch get(this cursor End ) getInt()))

return true

}

public boolean replace(String str) throws Exception

{

Dispatch put(this cursor Text str)

return true

}

public int getTableNum() throws Exception

{

return Dispatch get(this tables Count ) getInt()

}

public boolean setCurrentTable(int index) throws Exception

{

this table = Dispatch call(this tables Item new Variant(index)) toDispatch()

Dispatch call(this table Select )

return true

}

public String getCell(int row int col) throws Exception

{

Dispatch cell = Dispatch call(table Cell Integer toString(row) Integer toString(col)) toDispatch()

Dispatch call(cell Select )

String tmp = Dispatch get(this cursor Text ) getString()

//System out println( + tmp)

if (tmp length() >)

{

return tmp substring( tmp length() )

}

else

return

}

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

{

Dispatch cell = Dispatch call(table Cell Integer toString(row) Integer toString(col)) toDispatch()

Dispatch call(cell Select )

Dispatch put(this cursor Text str)

return true

}

public static void main(String args[])

{

WordHandle word = new WordHandle()

try

{

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

System out println(word getTableNum())

word setCurrentTable( )

word replaceCell( old Name )

word replaceCell( 经营范围 )

word replaceCell( )

word replaceCell( )

word replaceCell( )

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

word close()

word quit()

}

catch (Exception e)

{

}

}

}

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

lishixinzhi/Article/program/Java/hx/201311/26729

如果没有特殊需求,可以直接使用jacob_*.zip中提供的jacob.jar和jacob.dll。把jacob.dll文件放在系统可以找得到的

路径上,一般放c:/windows/system32下就行了,注意你用的jacob.dll文件和你的jacob.jar包要匹配,否则会报错哦! 

如果想自己编译也很简单,把jacob_*_src.zip解开,建个工程,在build.xml中稍作配置即可: 

<property name="JDK" value="D:\Java\j2sdk1.4.2_13"/> 

<property name="MSDEVDIR" value="D:\Microsoft Visual Studio\VC98"/>  

<property name="version" value="1.12"/>

看出来了吗,你的机器上需要有JDK和VC环境,VC是用来生成jacob.dll文件的,如果编译时说找不到MSPDB60.DLL,那就在你的

Microsoft Visual Studio目录下搜索一下,拷贝到D:\Microsoft Visual

Studio\VC98\Bin下就行了。

如果需要对jacob里的jar包改名,(虽然通常不会发生这种情况,但如果你需要两个版本的jacob同时使用,改名可能是一种选择),这时你的工作就多一些:

(1)package改名是必须的了,比如我们把src下的com.jacob.activeX改为com.test.jacob.activeX,把

com.jacob.com改为com.test.jacob.com,打包时只有这两个包是有用的,所以只改它们就够了。

(2)然后修改build.xml中src.java.jacob.mainpackage的value为com.test.jacob,修改java.class.main的value为com.test.jacob.com.Jacob。

(3)别忘了javaJarBin中打包的源码路径也要改,<include name="com/**/*.class" />改为<include name="com/test/**/*.class" />。

(4)build.xml中对生成的dll和jar包也要改个名,比如我们把这两个文件改为jacob_test.dll和

jacob_test.jar。修改build.xml中的enerated.filename.dll和generated.filename.jar

的value为你新改的名字。

(5)com.test.jacob.com.LibraryLoader中,System.loadLibrary("jacob")改成

System.loadLibrary("jacob_test")

(6)另外,很重要的,在jni中*.cpp和*.h中com_jacob_com统一改为com_test_jacob_com,com/jacob

/com统一改为com/test/jacob/com。

(7)ant编译,编译好的文件在release目录下。

(8)最后把编译好的jacob_test.dll文件放在windows/system32下就大功告成了。

现在该用到jacob.jar了,如果你自己修改过jar包的名字,用新改的jar包,如jacob_test.jar,这里统一称为jacob.jar。

首先在classpath中引入jacob.jar包,如果是web应用,WEB-INF的lib中也要加入jacob.jar包。

下面给一个例子:

类ReplaceWord.java

import com.jacob.com.*

import com.jacob.activeX.*

public class ReplaceWord {

public static void main(String[] args) {

ActiveXComponent app = new ActiveXComponent("Word.Application") //启动word

String inFile = "C:\\test.doc" //要替换的word文件

try {

app.setProperty("Visible", new Variant(false)) //设置word不可见  

Dispatch docs = app.getProperty("Documents").toDispatch()

Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new

Object[] { inFile, new Variant(false),new Variant(false) }, new

int[1]).toDispatch()

//打开word文件,注意这里第三个参数要设为false,这个参数表示是否以只读方式打开,因为我们要保存原文件,所以以可写方式打开。

Dispatch

selection=app.getProperty("Selection").toDispatch()//获得对Selection组件

Dispatch.call(selection, "HomeKey", new Variant(6))//移到开头

Dispatch find = Dispatch.call(selection, "Find").toDispatch()//获得Find组件

Dispatch.put(find, "Text", "name") //查找字符串"name"

Dispatch.call(find, "Execute") //执行查询

Dispatch.put(selection, "Text", "张三") //替换为"张三"

Dispatch.call(doc, "Save") //保存

Dispatch.call(doc, "Close", new Variant(false))

} catch (Exception e) {

e.printStackTrace()

} finally {

app.invoke("Quit", new Variant[] {})

app.safeRelease()

}

}

}

也许你会问,我怎么知道要调用哪个方法传哪些参数来进行 *** 作?别忘了,word还有宏呢!自己录制一个宏,编辑这个宏就可以看到代码了!用哪个对象的哪个方法就看你的了。

我总结了一下:

document下的组件都用Dispatch selection=app.getProperty("Selection").toDispatch()这种方法获得;

再往下的组件就需要调用selection的方法来获取,如 Dispatch find = Dispatch.call(selection, "Find").toDispatch();

如果某个方法需要参数,Dispatch doc =

Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { inFile, new

Variant(false),new Variant(false) }, new

int[1]).toDispatch()是一个例子,这是调用docs的Open方法,Object[]数组里就是它需要的参数了;

如果要修改某个组件的属性呢,用Dispatch.put(find, "Text", "name")这种形式,"Text"是属性名,"name"是值。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/tougao/8018279.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-12
下一篇2023-04-12

发表评论

登录后才能评论

评论列表(0条)

    保存