JAVA操作Word(jacob调用com)

package Beans;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class WordOperate {
 
 private ActiveXComponent wordApp = null;
 private Dispatch docs = null;
 private Dispatch selection = null;
 private Dispatch align = null;
 private Dispatch font = null;
 private Dispatch  document = null;
 private String tpFile =null;
 private String SpecifyContent = null;
 private Variant userName = null;
 private int testNo;
 
 public WordOperate(){
  wordApp = new ActiveXComponent("Word.Application");
  Dispatch.put(wordApp, "Visible", new Variant(true));
  docs = wordApp.getProperty("Documents").toDispatch(); 
  userName = wordApp.getProperty("Username");   
  document = Dispatch.call(docs, "Add").toDispatch(); 
  selection = Dispatch.get(wordApp, "Selection").toDispatch();
  align = Dispatch.get(selection, "ParagraphFormat").toDispatch();
  font = Dispatch.get(selection, "Font").toDispatch();
  testNo = 1;
 }
 
 public void createDocument(){
  document = Dispatch.call(docs, "Add").toDispatch(); 
 }
 
 public void insertTitle(String Title){
  Dispatch.put(align, "Alignment", "1"); // 1:置中 2:靠右 3:靠左
  Dispatch.put(font, "Bold", "1");
  Dispatch.put(font, "Color", "0,0,0,0"); // 黑色
  Dispatch.put(font,"Name","宋体");
  Dispatch.call(selection, "TypeText", Title);
  Dispatch.call(selection, "TypeParagraph"); // 空一行段落 
  Dispatch.call(selection,"TypeParagraph");
  moveDown(1);
 }
 
 public void insertSelection(String Content,String keyA,String keyB , String keyC , String keyD){
  Dispatch.put(align,"Alignment","3");
  Dispatch.put(font,"Bold","0");  
  SpecifyContent = "" + testNo + ".  " + Content;
  Dispatch.call(selection, "TypeText",SpecifyContent);
  Dispatch.call(selection,"TypeParagraph");
  Dispatch.call(selection,"TypeText","A:  " + keyA);
  Dispatch.call(selection, "TypeParagraph");
  Dispatch.call(selection,"TypeText","B:  " + keyB);
  Dispatch.call(selection, "TypeParagraph");
  Dispatch.call(selection,"TypeText","C:  " + keyC);
  Dispatch.call(selection, "TypeParagraph");
  Dispatch.call(selection,"TypeText","D:  " + keyD);
  Dispatch.call(selection,"TypeParagraph");
  Dispatch.call(selection,"TypeParagraph");
  moveDown(1);
  testNo ++ ;
 }
 
 public void insertOthers(String Content){
  Dispatch.put(align,"Alignment","3");
  Dispatch.put(font,"Bold","0");
  SpecifyContent = "" + testNo + ".  " + Content;  
  Dispatch.call(selection, "TypeText",SpecifyContent);
  Dispatch.call(selection,"TypeParagraph");
  Dispatch.call(selection,"TypeParagraph");
  moveDown(1);
  testNo ++ ;
 }
 
 public void insertPara(String Content){
  Dispatch.put(align,"Alignment","3");
  Dispatch.put(font,"Bold","1");
  Dispatch.call(selection, "TypeText",Content);
  Dispatch.call(selection,"TypeParagraph");
  Dispatch.call(selection,"TypeParagraph");
  moveDown(1);
 }
 
    private void moveUp(int pos){
     if (selection == null)
      selection = Dispatch.get(document, "Selection").toDispatch();
     for (int i = 0; i < pos; i++)
      Dispatch.call(selection, "MoveUp");
    }
 
    private void moveDown(int pos){
     if (selection == null)
      selection = Dispatch.get(document, "Selection").toDispatch();
     for (int i = 0; i < pos; i++)
      Dispatch.call(selection, "MoveDown");
    }
 
    private void moveLeft(int pos){
     if (selection == null)
      selection = Dispatch.get(document, "Selection").toDispatch();
     for (int i = 0; i < pos; i++){
      Dispatch.call(selection, "MoveLeft");
     }
    }

    private void moveRight(int pos) {
     if (selection == null)
      selection = Dispatch.get(document, "Selection").toDispatch();
     for (int i = 0; i < pos; i++)
      Dispatch.call(selection, "MoveRight");
    }
 
    private void moveStart() {
     if (selection == null)
      selection = Dispatch.get(document, "Selection").toDispatch();
     Dispatch.call(selection, "HomeKey", new Variant(6));
    }
    
    private void moveEnd() {
     if (selection == null)
      selection = Dispatch.get(document, "Selection").toDispatch();
     Dispatch.call(selection, "EndKey", new Variant(6));
    }
   
    public void openDocument(String docPath){
     closeDocument();
     Dispatch doc = Dispatch.invoke(docs, "Open",
    Dispatch.Method,new Object[] { docPath, new Variant(false), new Variant(false)},
    new int[1]).toDispatch();
    }   
 
 public void saveAs(){
  Dispatch.call(document, "SaveAs", tpFile);
 }
 
 public void save(){
  Dispatch.call(document,"Save");
 }
 
 public void setFilePath(String file){
  tpFile = file;
 }
 
    public void closeDocument() {
     if (document != null) {      
      Dispatch.call(document, "Close", new Variant(0));
      document = null;
     }
    }
   
    public void close() {
     closeDocument();
     if (wordApp != null) {
      Dispatch.call(wordApp, "Quit");
      wordApp = null;
     }
     selection = null;
     docs = null;
    }

 public int getTestNo() {
  return testNo;
 }

 public void setTestNo(int testNo) {
  this.testNo = testNo;
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值