采用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");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值