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用poi实现对word读取和修改操作

java编程要实现对word的操作没有vb那种编程语言来得容易,得借助一些开源组件,其中就包括jacob、poi等, 而poi应用得最为广泛,对word2003和2007的读和写word操作都十分方便...

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

  • 2012-11-12 13:37
  • 207KB
  • 下载

java 操作word

  • 2014-07-02 10:33
  • 386KB
  • 下载

Java操作word模板插入多种编号的列表

Java操作Word模板插入列表通过poi操作word,插入各种编号的列表,包括数字,字母,罗马字符,自定义任意字符。下面的示例就用了音乐符♬。1.制作模板,插入列表使用语法*2.使用Poi-tl,添...

java操作word的一个例子

  • 2014-11-04 00:43
  • 784KB
  • 下载

java操作word/excel/pdf等文件技术方案

最近项目中遇到很多对word/excel/pdf等文件的操作,解决方案有好多,开源免费有:利用openoffice组件(需要安装openoffice软件),poi,itext等。也有收费的服务:asp...

java操作word

  • 2012-03-13 15:22
  • 207KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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