一、怎样用java将特定的数据写入特定word文档中的特定位
使用Java操作word的方法有几个,列出来:IText,Apache的POI包,tm-extractors包,jacob包,java2word包。
IText包比较大,但是功能比较全。而且比较成熟,用的人也多。
POI包是Apache的一个项目,不过现在已经停止更新了,而且我看了里面的Example,感觉调用起来很麻烦。用过之后再也不想用了。
tm-extractors包的API比较简单,调用起来很方便,代码量也少。
jacob包我觉得是最强大的,可以操作word,excel。因为调用的是微软底层的dll文件,需要在system32目录下放置jacob.dll,然后在classpath里面添加jacob.jar文件。推荐使用1.9版本,之前1.8版本调用起来就算你的代码没有写错也会出现一些莫名其妙的异常。
java2word也是调用微软的底层dll文件,有安装包,也有解压包,安装包会将jacob.dll文件装在system32目录下。之后将java2word.jar加到classpath就可以使用了。感觉如果只是操作word文档的话,比jacob用起来要简单和舒服。代码量也少,很清晰。
另外,使用jacob和java2word的话,会在调用WINWORD进程,如果多人并发访问服务器的word文件的话,会造成内存不足。
二、如何利用Java
1. 初始化com的线程,非常重要,否则第二次创建com对象的时候会出现can't co-create object异常 (参见jacob的帮助文档),完成操作com组件后要调用 realease方法 ComThread.InitSTA();// 初始化com的线程,非常重要!!使用结束后要调用 realease方法 2. 初始化word应用程序,新建一个空白文档,取得文档内容对象//Instantiate objWord //Declare word object ActiveXComponent objWord = new ActiveXComponent("Word.Application"); //Assign a local word object Dispatch wordObject = (Dispatch) objWord.getObject(); //Create a Dispatch Parameter to show the document that is opened Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// new Variant(true)表示word应用程序可见 Tip:设置一个对象的属性的时候,利用Dispatch的put方法,给属性赋值。
上面这行语句相当于vb的 wordObject.Visible = true 语句 //Instantiate the Documents Property Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文档窗口,(word是多文档应用程序) //Add a new word document, Current Active Document Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档 Tip:调用一个对象的方法的时候,利用Dispatch的call方法,上面的语句相当于vb的document = documents.Add() 语句。 Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容 Tip:取得一个对象的成员变量(属性)时利用Dispatch的get方法,上面的语句相当于vb的wordContent = document.Content语句 3. 取得word文档的内容后,可以对其内容进行操作 Dispatch.call(wordContent, "InsertAfter", "这里是一个段落的内容");//插入一个段落 4. 设置刚插入的段落的文字格式 Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落 int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数 // 找到刚输入的段落,设置格式 Dispatch lastParagraph = Dispatch.call(paragraphs, "Item", new Variant(paragraphCount)). toDispatch(); // 最后一段 Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range"). toDispatch(); Dispatch font = Dispatch.get(lastParagraphRange, "Font").toDispatch(); Dispatch.put(font, "Bold", new Variant(true)); // 设置为黑体 Dispatch.put(font, "Italic", new Variant(true)); // 设置为斜体 Dispatch.put(font, "Name", new Variant("宋体")); // Dispatch.put(font, "Size", new Variant(12)); //小四 注意:如果想插入一个新的空白行,也需要设置段落的文字格式,否则新插入行的文字格式会于刚插入的段落的格式相同。
5. 将当前文档保存 Dispatch.call(document, "SaveAs", new Variant("C: abc.doc")); // 保存一个新文档 6. 释放COM线程 ComThread.Release();//释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理 完整测试代码:(StudyJacob.java 附件中有本文章和java源文件) import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; import com.jacob.com.ComThread; public class StudyJacob { public static void main(String[] args) { ComThread.InitSTA();// 初始化com的线程,非常重要!!使用结束后要调用 realease方法 //Instantiate objWord //Declare word object ActiveXComponent objWord = new ActiveXComponent("Word.Application"); //Assign a local word object Dispatch wordObject = (Dispatch) objWord.getObject(); //Create a Dispatch Parameter to show the document that is opened Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// new Variant(true)表示word应用程序可见 //Instantiate the Documents Property Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文档窗口,(word是多文档应用程序) //Add a new word document, Current Active Document Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档 Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容 Dispatch.call(wordContent, "InsertAfter", "这里是一个段落的内容");//插入一个段落 Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落 int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数 // 找到刚输入的段落,设置格式 Dispatch lastParagraph = Dispatch.call(paragraphs, "Item", new Variant(paragraphCount)). toDispatch(); // 最后一段 。
三、java中的jacob将word文档转化为HTML文件问题
1、到官网下载Jacob,
2、将压缩包解压后,Jacob.jar添加到Libraries中(先复制到项目目录中,右键单击jar包选择Build Path—>Add to Build Path);
3、将Jacob.dll放至当前项目所用到的“jre\bin”下面(比如Eclipse正在用的Jre路径是C:\Java\jdk1.7.0_17\jre\bin)。
Ps:按照上面的步骤配置的,基本没有问题,但是有些电脑可能还会报错,比如:java.lang.UnsatisfiedLinkError: no jacob in java.library.path,这是系统没有加载到jacob.dll,网上解决方法是将Jacob.dll放至“WINDOWS\SYSTEM32”下面。
Java代码:
public class JacobUtil {
// 8 代表word保存成html
public static final int WORD_HTML = 8;
public static void main(String[] args) {
String docfile = "C:\\Users\\无名\\Desktop\\xxx.doc";
String htmlfile = "C:\\Users\\无名\\Desktop\\xxx.html";
JacobUtil.wordToHtml(docfile, htmlfile);
}
/**
* WORD转HTML
* @param docfile WORD文件全路径
* @param htmlfile 转换后HTML存放路径
*/
public static void wordToHtml(String docfile, String htmlfile)
{
// 启动word应用程序(Microsoft Office Word 2003)
ActiveXComponent app = new ActiveXComponent("Word.Application");
System.out.println("*****正在转换。*****");
try
{
// 设置word应用程序不可见
app.setProperty("Visible", new Variant(false));
// documents表示word程序的所有文档窗口,(word是多文档应用程序)
Dispatch docs = app.getProperty("Documents").toDispatch();
// 打开要转换的word文件
Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { docfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
// 作为html格式保存到临时文件
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(WORD_HTML) }, new int[1]);
// 关闭word文件
Dispatch.call(doc, "Close", new Variant(false));
}catch (Exception e){
e.printStackTrace();
}finally{
//关闭word应用程序
app.invoke("Quit", new Variant[] {});
}
System.out.println("*****转换完毕********");
}
}
四、如何利用Java
. 初始化com的线程,非常重要,否则第二次创建com对象的时候会出现can't co-create object异常 (参见jacob的帮助文档),完成操作com组件后要调用 realease方法 ComThread.InitSTA();// 初始化com的线程,非常重要!!使用结束后要调用 realease方法 2. 初始化word应用程序,新建一个空白文档,取得文档内容对象//Instantiate objWord //Declare word object ActiveXComponent objWord = new ActiveXComponent("Word.Application"); //Assign a local word object Dispatch wordObject = (Dispatch) objWord.getObject(); //Create a Dispatch Parameter to show the document that is opened Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// new Variant(true)表示word应用程序可见 Tip:设置一个对象的属性的时候,利用Dispatch的put方法,给属性赋值。
上面这行语句相当于vb的 wordObject.Visible = true 语句 //Instantiate the Documents Property Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文档窗口,(word是多文档应用程序) //Add a new word document, Current Active Document Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档 Tip:调用一个对象的方法的时候,利用Dispatch的call方法,上面的语句相当于vb的document = documents.Add() 语句。 Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容 Tip:取得一个对象的成员变量(属性)时利用Dispatch的get方法,上面的语句相当于vb的wordContent = document.Content语句 3. 取得word文档的内容后,可以对其内容进行操作 Dispatch.call(wordContent, "InsertAfter", "这里是一个段落的内容");//插入一个段落 4. 设置刚插入的段落的文字格式 Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落 int paragr aphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数 // 找到刚输入的段落,设置格式 Dispatch lastParagraph = Dispatch.call(paragraphs, "Item", new Variant(paragraphCount)). toDispatch(); // 最后一段 Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range"). toDispatch(); Dispatch font = Dispatch.get(lastParagraphRange, "Font").toDispatch(); Dispatch.put(font, "Bold", new Variant(true)); // 设置为黑体 Dispatch.put(font, "Italic", new Variant(true)); // 设置为斜体 Dispatch.put(font, "Name", new Variant("宋体")); // Dispatch.put(font, "Size", new Variant(12)); //小四。
转载请注明出处51数据库 » jacobjava2word