Chinaunix首页 | 论坛 | 博客
  • 博客访问: 34509
  • 博文数量: 12
  • 博客积分: 610
  • 博客等级: 上士
  • 技术积分: 155
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-12 23:09
文章分类
文章存档

2008年(12)

我的朋友
最近访客

分类: Java

2008-03-30 11:38:12

导入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.");
 }
}
阅读(914) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~