免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 974 | 回复: 0
打印 上一主题 下一主题

通过Jacob抽取Word简单例子 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-30 11:38 |只看该作者 |倒序浏览
导入jacob.jar,再将jacob.dll复制到jdk/bin目录下

import com.jacob.activeX.*;
import com.jacob.com.*;
public class WordBean extends java.awt.Panel {
private ActiveXComponent MsWordApp = null;
private Dispatch document = null;
public WordBean() {
  super();
}
public void openWord(boolean makeVisible) {
  // Open Word if we've not done it already
  if (MsWordApp == null) {
   MsWordApp = new ActiveXComponent("Word.Application");
  }
  // Set the visible property as required.
  Dispatch.put(MsWordApp, "Visible", new Variant(makeVisible));
}
public void createNewDocument() {
  // Find the Documents collection object maintained by Word
  System.out.println("sss") ;
  Dispatch documents = Dispatch.get(MsWordApp, "Documents").toDispatch();
  // Call the Add method of the Documents collection to create
  // a new document to edit
  document = Dispatch.call(documents, "Add").toDispatch();
}
public void insertText(String textToInsert) {
  // Get the current selection within Word at the moment. If
  // a new document has just been created then this will be at
  // the top of the new doc
  Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
  // Put the specified text at the insertion point
  Dispatch.put(selection, "Text", textToInsert);
}
public void saveFileAs(String filename) {
  Dispatch.call(document, "SaveAs", filename);
}
public void printFile() {
  // Just print the current document to the default printer
  Dispatch.call(document, "PrintOut");
}
public void closeDocument() {
  // Close the document without saving changes
  // 0 = wdDoNotSaveChanges
  // -1 = wdSaveChanges
  // -2 = wdPromptToSaveChanges
  Dispatch.call(document, "Close", new Variant(0));
  document = null;
}
public void closeWord() {
  Dispatch.call(MsWordApp, "Quit");
  MsWordApp = null;
  document = null;
}
}


import java.io.File;
import com.jacob.com.*;
import com.jacob.activeX.*;
public class WordTest {
public static void main(String[] args) {
  WordBean word = new WordBean();
  word.openWord(true);
  word.createNewDocument();
  word.insertText("Hello word.");
}
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/63390/showart_511829.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP