Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » 技术文章库  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 JAVA 深度控制 WORD 带完整样例
西乡侯





发贴: 11
积分: 1
于 2005-08-10 08:45 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list


========== JAVA 深度控制 WORD

  Java 控制Office 控件是非常麻烦的一件事情。
  自从有了JACOB后,事情变得简单多了。
  但是要实现Java灵活的控制Word还是一件非常麻烦的事情。
  
  下面介绍几个WORD常见的对象以及一些典型的处理过程,希望对大家有帮助。
  (请注意:JDK1.3.2运行 Jacob比较正常,JDK1.4有问题)
  /** WORD对象*/
  private ActiveXComponent word = null;
  /** 文档对象*/
private Dispatch documents = null;
/** selection 对象是比较重要的一个对象 */
  private Dispatch vSelection = null;
  /** 一个WORD文档 */
  private Dispatch wordfile = null;
1,初始化
    word = new ActiveXComponent("Word.Application");
    documents = word.getProperty("Documents").toDispatch();
    (将JACOB 放在 WINNT\system32\ 下比较简单省事)
2,打开文件
      wordfile = Dispatch.invoke(
        documents,
        "Open",
        Dispatch.Method,
          new Object[] {
strFileName,
new Variant(true),//是否进行转换 ConfirmConversions
            new Variant(false)//是否只读
}, new int[1]).toDispatch();
vSelection = word.getProperty("Selection").toDispatch();
在WORD中,选定内容进行转换时,不用象Java对象一样来回的重新取,这个对象一直有效。
3,显示WORD
    word.setProperty("Visible", new Variant(visible));
4,设置WORD的位置
    Dispatch activeWindow = Dispatch.get(word, "Application").toDispatch();
    Dispatch.put(activeWindow, "WindowState", new Variant(0));
    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));

进行将JAVA内的数据和WORD交换,常用的做法是,在WORD上作一些特殊的标记,利用 FIND 和 Replace的方法进行,这个方法不是太好。
个人觉得使用超链接的模式比较方便。
有几大优点:
1,  Hyperlink 有3个区域可以让开发者自己利用
ActiveDocument.Hyperlinks.Add
Anchor:=Selection.Range,
Address:="位置", //地址(可以利用) 有个缺点
SubAddress:="",//子位置(可以利用)
ScreenTip:="", //屏幕提示
TextToDisplay:="显示内容"//最好利用的东西

个人建议使用TextToDisplay。
Address 会在保存时被替换成绝对路径。
比如你录入一个
“AA.BB.CC”
保存时可能会被替换成
C:\Documents and Settings\Administrator \My Documents\AA.BB.CC
2,  可以进行自动定位
利用Hyperlinks 可以将文章中所有的超链接得到。
也可以将指定范围的超链接得到。
3,  可以自由排版
4,  可以拷贝粘贴

添加超链接:
  Dispatch Hyperlinks = Dispatch.get(wordfile, "Hyperlinks").toDispatch();
  Dispatch range = Dispatch.get(vSelection, "Range").toDispatch();
  Dispatch h=Dispatch.invoke(Hyperlinks,
"Add", Dispatch.Method, new Object[]
{ range,
        new Variant("Address"),
new Variant("SubAddress"),
new Variant("{table.fieldName}"),//建议的数据链接处
        new Variant("姓名") }, // 在WORD中显示的内容
new int[4]).toDispatch();
    Dispatch hRange=Dispatch.get(h, "Range").toDispatch();
    Dispatch.call(hRange,"select");
    //设置字体,颜色
    Dispatch font = Dispatch.get(vSelection, "Font").toDispatch();
    Dispatch.put(font,"Underline", new Variant(0));
    Dispatch.put(font,"Color", new Variant(0));
    //取消选择
    Dispatch.call(vSelection,"MoveRight",new Variant(1),new Variant(1));

超链接替换内容:
1,  得到所有的超链接
//选择对象
   Dispatch.call(dObject, "select");
    //得到超链接集合
   Dispatch Hyperlinks = Dispatch.get(vSelection,  "Hyperlinks").toDispatch();
    //得到有多少个超链接
   int nHyperlink = Dispatch.get(Hyperlinks, "count").toInt();
    //得到一个超链接
    Dispatch hyperlink=Dispatch.invoke(Hyperlinks, "item",
      Dispatch.Method, new Object[] { new Integer(i + 1)},
   new int[1]).toDispatch()));
2,  替换内容
Dispatch.put(hyperlink, "TextToDisplay", information);
3,  取消超链接,将超链接变成普通文字。
Dispatch.call(hyperlink, "delete");

如何实现批量数据自动扩展,建议使用表格进行自动扩展,方便简单。
结合使用上面超链接的技术。会非常简单:

比如有如下数据:
    
DataA
DataB

1,  列出所有表格
和列出所有超链接基本一样:
private void getTables01(Dispatch objcet,Vector vTableStore) {
Dispatch tables = Dispatch.get(objcet, "tables").toDispatch();
int nTableAmount = Dispatch.get(tables, "count").toInt();
for (int i = 0; i < nTableAmount; i++) {
Dispatch table =
Dispatch
.invoke(
tables,
"item",
Dispatch.Method,
new Object[] { new Integer(i + 1)},
new int[1])
.toDispatch();
vTableStore.add(new DTable(table));
getTables01(table,vTableStore);//处理表格套用表格的情况
}
}
2,  表格的可以控制的对象
    Dispatch dRows = Dispatch.get(dTable, "rows").toDispatch();//所有行
    int nRows = Dispatch.get(dRows, "count").toInt();
3,  取得一行的内容
  Dispatch dRow =
    Dispatch
      .invoke(
        rows,
        "item",
        Dispatch.Method,
        new Object[] { new Integer(row + 1)},
        new int[1])
      .toDispatch();
       return dRow;
  }catch(ComFailException cfe)
  {
    /** 带有合并行的情况*/
    return null;
  }
4,  得到一行的超链接
DHyperlink dhInRow[] = listHyperlinks(dRow);
5,  将某一行拷贝很多次
    Dispatch.call(dRow, "select");
    Dispatch.call(vSelection, "Copy");
    int nCopyNow = nDataBlockRow - 1;
    for (int nCopys = 0; nCopys < nCopyNow; nCopys++) {
   try   {
Dispatch.call(vSelection, "Paste");
   }catch(Exception e)   {   e.printStackTrace();
     //有时候文档损坏,可以忽略本问题,实际上已经粘贴上了  
   }
    }
6,  替换内容,读到这里就不用介绍了。

打印预览:
Dispatch.call(wordfile,"PrintPreView");

其他的功能发掘
  利用WORD的宏录制,以及VB编辑器,辅助功能,都能发掘出来。

jacob_word.rar (71.23k)



作者 Re:JAVA 深度控制 WORD 带完整样例 [Re:西乡侯]
javadd





发贴: 736
积分: 77
于 2005-08-10 10:09 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不错.



作者 Re:JAVA 深度控制 WORD 带完整样例 [Re:javadd]
xjhlzl





发贴: 1
积分: 0
于 2005-08-18 13:06 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
先谢谢你的资料。
我写了测试结果有些能够实现有些实现不了。
现在很郁闷其中有一行
vTableStore.add(new DTable(table));
怎么又DTable 这种数据类型那。应该是.net中的数据类型。在这里怎么出现了??
还有就是有没有关于这方面的文档呀!给发一份好吗? 谢谢了
邮箱:xjhlzl@263.net



作者 Re:JAVA 深度控制 WORD 带完整样例 [Re:西乡侯]
西乡侯





发贴: 11
积分: 1
于 2005-08-19 13:18 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
vTableStore.add(new DTable(table));

DTable 是自己定义的类,

可以在JAVA 文件中找到。



作者 Re:JAVA 深度控制 WORD 带完整样例 [Re:西乡侯]
西乡侯





发贴: 11
积分: 1
于 2005-08-19 13:21 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
import com.jacob.com.*;
import com.jacob.activeX.*;
public class DTable extends DObject {
public int rows;
public int cols;
/**
* DTable 构造子注解。
* @param d com.jacob.com.Dispatch
*/
public DTable(com.jacob.com.Dispatch d) {
superFood;
Dispatch dRows = Dispatch.get(d, "rows").toDispatch();
Dispatch dColumns = Dispatch.get(d, "Columns").toDispatch();

rows = Dispatch.get(dRows, "count").toInt();
cols = Dispatch.get(dColumns, "count").toInt();
}
public void fixRange() {
super.fixRange();
Dispatch dRows = Dispatch.get(dObject, "rows").toDispatch();
Dispatch dColumns = Dispatch.get(dObject, "Columns").toDispatch();

rows = Dispatch.get(dRows, "count").toInt();
cols = Dispatch.get(dColumns, "count").toInt();
}



作者 Re:JAVA 深度控制 WORD 带完整样例 [Re:西乡侯]
YuLimin

简单就是美

版主


发贴: 866
积分: 123
于 2005-08-19 14:34 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
奇怪为何POI的组件一件不更新也不支持Word等等,现在只支持Excel:(


 当兵不后悔!后悔不当兵! 
超越黎明时空,追逐时代潮流!
人,是要靠自己的!简单就是美!
我的Java:http://www.Java2Class.net
我的Blog:http://YuLimin.ItEye.com
作者 Re:JAVA 深度控制 WORD 带完整样例 [Re:西乡侯]
leticia





发贴: 1
积分: 0
于 2005-09-01 10:02 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我觉得在作文件另存为的时候,使用close 比使用quit好。这是我的想法。

public void close()
  {
    //word.invoke("Quit", new Variant[] {});
    Variant f = new Variant(false);
    Dispatch.call(wordfile, "Close", f);
  }
  
public void quit()
  {
    word.invoke("Quit", new Variant[] {});
  }



作者 Re:JAVA 深度控制 WORD 带完整样例 [Re:西乡侯]
wsfx





发贴: 12
积分: 0
于 2005-09-08 11:43 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
jacob好多年了,还是只支持jdk1.3,是不是没人维护了,有谁在word2003下测试过没有?


作者 Re:JAVA 深度控制 WORD 带完整样例 [Re:西乡侯]
西乡侯





发贴: 11
积分: 1
于 2005-09-08 12:44 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不知道 , 有 C 源代码,编译一下应当没有问题。

上面的例子就是在 WORD 2003 测试的。



作者 Re:JAVA 深度控制 WORD 带完整样例 [Re:西乡侯]
FrankYuan





发贴: 1
积分: 0
于 2005-11-24 10:34 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
侯爷,
你那儿有jacob的使用说明书吗?提供一份吧!我现在需要向页眉页脚里加入表格,并且自由改变每个单元格的内容,请给一些指点吧! 你贴子里的资料已经给我了很大帮助,非常感谢!我的邮箱是:yuanjunzhi@tom.com




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923