java 操作 word

转载 2006年06月02日 22:45:00

参考:
http://danadler.com/jacob/
http://jakarta.apache.org/poi/
http://www.onjava.com/pub/a/onjava/2003/01/22/poi.html
http://www.csdn.net/develop/article/15/15311.shtm
http://forum.java.sun.com/thread.jsp?forum=40&thread=382666&tstart=0&trange=15

先下载jacob并把里面的jacob.jar加入到classpath中
把jacob.dll放入windows/system32下面放入到path中点击我的电脑 属性 高级 系统变量
jacob操作Word的例子
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.");
}
}

以下是WordBean类
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
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;
}
}

java操作word需要的jar和dll文件

  • 2015年08月04日 15:04
  • 62KB
  • 下载

java word操作总结

java word操作总结

java 操作word

  • 2014年07月02日 10:33
  • 387KB
  • 下载

java 操作word文档加图片水印

package com.tomb.web.admin; import com.jacob.activeX.ActiveXComponent;   import com.jacob.com.Dispa...

java操作word(内部资料).doc

  • 2013年03月01日 10:30
  • 207KB
  • 下载

java操作xml导出word

java导出word[xml方式] xml方式导出word优缺点: 优点:          1、代码量少,样式、内容容易控制,打印不变形,符合office标准;          2、支...

JAVA POI(操作Excel、word)

  • 2015年03月06日 21:06
  • 15.91MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:java 操作 word
举报原因:
原因补充:

(最多只允许输入30个字)