`
andylu521
  • 浏览: 17151 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

采用Jacob生成Word文档

阅读更多
Jacob1.11版本下载地址:
http://nchc.dl.sourceforge.net/sourceforge/jacob-project/jacob_1.11.1.zip

下载完以后,解压缩出来,把里面的jacob.jar添加到classpath中
把jacob.dll放入windows/system32下面
添加到系统环境变量中:右键我的电脑->属性->高级->系统变量

类一:WordService.java
来源:(http://blog.sina.com.cn/s/blog_48ca8187010007wy.html) - 采用Jacob生成Word文档_别问我是谁_新浪博客

import com.jacob.activeX.*;
import com.jacob.com.*;
/**
* 采用Jacob1.11操作Word
*  
* @author liuxt
*
*/
public class WordService extends java.awt.Panel {
private static final long serialVersionUID=-1L;

public ActiveXComponent MsWordApp = null;
private Dispatch document = null;
public WordService() {
  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));
}
/**
  * 打开已知的word文档
  * @param markVisible 可视化状态
  * @param docPath 文档路径
  */
public void openWord(boolean markVisible,final String docPath) {
  if (MsWordApp == null) {
   MsWordApp = new ActiveXComponent("Word.Application");
   }
   //Set the visible property as required.
  Dispatch.put(MsWordApp, "Visible", new Variant(markVisible));
        Dispatch docs = MsWordApp.getProperty("Documents").toDispatch();
        document = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{docPath}, new int[1]).toDispatch();
}
/**
  * 创建Word文档
  *
  */
public void createNewDocument() {
  //Find the Documents collection object maintained by Word
  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();
}
/**
  * 插入文本
  * @param textToInsert
  */
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");
}
/**
  * 查找替换文本
  * @param searchText 需要查找的关键字
  * @param replaceText 要替换成的关键字
  */
public void searchReplace(final String searchText,final String replaceText) {
  Dispatch selection=MsWordApp.getProperty("Selection").toDispatch();
  Dispatch find = ActiveXComponent.call(selection, "Find").toDispatch();
        //查找什么文本
        Dispatch.put(find, "Text", searchText);
        //替换文本
        Dispatch.call(find,"ClearFormatting");
        Dispatch.put(find, "Text", searchText);
        Dispatch.call(find, "Execute");
        Dispatch.put(selection, "Text", replaceText);
}
/**
  * 把指定的值设置到指定的标签中去
  * @param bookMarkKey
  */
public void setBookMark(final String bookMarkKey,final String bookMarkValue) {
       Dispatch activeDocument=MsWordApp.getProperty("ActiveDocument").toDispatch();
       Dispatch bookMarks = ActiveXComponent.call(activeDocument, "Bookmarks").toDispatch();
       boolean bookMarkExist1=Dispatch.call(bookMarks,"Exists",bookMarkKey).getBoolean();
       if(bookMarkExist1==true){
         Dispatch rangeItem = Dispatch.call(bookMarks, "Item",bookMarkKey).toDispatch();
         Dispatch range = Dispatch.call(rangeItem, "Range").toDispatch();
         //取标签的值
         //String bookMarkValue=Dispatch.get(range,"Text").toString();
         //bookMarkValue="test";
         if(bookMarkValue!=null){
                Dispatch.put(range, "Text",new Variant(bookMarkValue));
         }       
       }else{
         System.out.println("not exists bookmark!");
       }
}
/**
  * 保存Word文档到指定的目录(包括文件名)
  * @param filePath
  */
public void saveWordFile(final String filePath) {
    //保存文件
       Dispatch.invoke(document, "SaveAs", Dispatch.Method, new Object[] {filePath, new Variant(0)}                      , new int[1]);
       //作为word格式保存到目标文件
       Variant f = new Variant(false);
       Dispatch.call(document, "Close", f);
}
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;
}
}

类二:WordExample.java
import com.suypower.yxsj.common.WordService;
public class WordExample {
public static void main(String[] args) {
    WordService word=new WordService();
    word.openWord(true);
    word.createNewDocument();
    word.insertText("Hello word.");
    word.searchReplace("Hello", "哈喽");
    word.setBookMark("bookMarkKey","bookMarkValue");
    //word.MsWordApp.setProperty("UserName","xiantongsky");
    //word.saveWordFile("D:/xiantong.doc");
}
}
分享到:
评论
1 楼 aguai0 2010-07-09  
word是被保存到服务器端了吧,那客户端怎么拿到呢

相关推荐

Global site tag (gtag.js) - Google Analytics