「java配置jacob」java配置成功后怎么打开

博主:adminadmin 2022-11-22 05:28:11 55

本篇文章给大家谈谈java配置jacob,以及java配置成功后怎么打开对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java中的jacob将word文档转化为HTML文件问题

配置:

(1)将解压包中的jacob.dll(x86常用,x64)拷到jdk安装目录下的jre\bin文件夹或windows安装路径下的WINDOWS\system32文件夹下

(2)将jacob.jar文件拷到classpath下即可

常见问题解决:

对于”java.lang.UnsatisfiedLinkError: C:\WINDOWS\system32\jacob-1.14.3-x86.dll: 由于应用程序配置不正确,应用程序未能启动。重新安装应用程序可能会纠正”这个问题,可以通过

重新下载Jacob的jar及dll文件(最好版本比现在的低,如1.11)解决

实例制作(主要功能:标题制作,表格制作,合并表格,替换文本,页眉页脚,书签处理):

import com.jacob.activeX.ActiveXComponent;

import com.jacob.com.Dispatch;

import com.jacob.com.Variant;

public class WordOperate {

 public static void main(String args[]) {

  ActiveXComponent wordApp = new ActiveXComponent("Word.Application"); // 启动word

  // Set the visible property as required.

  Dispatch.put(wordApp, "Visible", new Variant(true));// //设置word可见

  Dispatch docs = wordApp.getProperty("Documents").toDispatch();

  // String inFile = "d:\\test.doc";

  // Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method,

  // new Object[] { inFile, new Variant(false), new Variant(false)},//参数3,false:可写,true:只读

  // new int[1]).toDispatch();//打开文档

  Dispatch document = Dispatch.call(docs, "Add").toDispatch();// create new document

  String userName = wordApp.getPropertyAsString("Username");// 显示用户信息

  System.out.println("用户名:" + userName);

  // 文档对齐,字体设置////////////////////////

  Dispatch selection = Dispatch.get(wordApp, "Selection").toDispatch();

  Dispatch align = Dispatch.get(selection, "ParagraphFormat")

    .toDispatch(); // 行列格式化需要的对象

  Dispatch font = Dispatch.get(selection, "Font").toDispatch(); // 字型格式化需要的对象

  // 标题处理////////////////////////

  Dispatch.put(align, "Alignment", "1"); // 1:置中 2:靠右 3:靠左

  Dispatch.put(font, "Bold", "1"); // 字型租体

  Dispatch.put(font, "Color", "1,0,0,0"); // 字型颜色红色

  Dispatch.call(selection, "TypeText", "Word文档处理"); // 写入标题内容

  Dispatch.call(selection, "TypeParagraph"); // 空一行段落

  Dispatch.put(align, "Alignment", "3"); // 1:置中 2:靠右 3:靠左

  Dispatch.put(selection, "Text", "        ");

  Dispatch.call(selection, "MoveDown"); // 光标标往下一行

  //表格处理////////////////////////

  Dispatch tables = Dispatch.get(document, "Tables").toDispatch();

  Dispatch range = Dispatch.get(selection, "Range").toDispatch();

  Dispatch table1 = Dispatch.call(tables, "Add", range, new Variant(3),

    new Variant(2), new Variant(1)).toDispatch(); // 设置行数,列数,表格外框宽度

  // 所有表格

  Variant tableAmount = Dispatch.get(tables, "count");

  System.out.println(tableAmount);

  // 要填充的表格

  Dispatch t1 = Dispatch.call(tables, "Item", new Variant(1))

    .toDispatch();

  Dispatch t1_row = Dispatch.get(t1, "rows").toDispatch();// 所有行

  int t1_rowNum = Dispatch.get(t1_row, "count").getInt();

  Dispatch.call(Dispatch.get(t1, "columns").toDispatch(), "AutoFit");// 自动调整

  int t1_colNum = Dispatch.get(Dispatch.get(t1, "columns").toDispatch(),

    "count").getInt();

  System.out.println(t1_rowNum + " " + t1_colNum);

  for (int i = 1; i = t1_rowNum; i++) {

   for (int j = 1; j = t1_colNum; j++) {

    Dispatch cell = Dispatch.call(t1, "Cell", new Variant(i),

      new Variant(j)).toDispatch();// 行,列

    Dispatch.call(cell, "Select");

    Dispatch.put(selection, "Text", "cell" + i + j); // 写入word的内容

    Dispatch.put(font, "Bold", "0"); // 字型租体(1:租体 0:取消租体)

    Dispatch.put(font, "Color", "1,1,1,0"); // 字型颜色

    Dispatch.put(font, "Italic", "1"); // 斜体 1:斜体 0:取消斜体

    Dispatch.put(font, "Underline", "1"); // 下划线

    Dispatch Range = Dispatch.get(cell, "Range").toDispatch();

    String cellContent = Dispatch.get(Range, "Text").toString();

    System.out.println((cellContent.substring(0, cellContent

      .length() - 1)).trim());

   }

   Dispatch.call(selection, "MoveDown"); // 光标往下一行(才不会输入盖过上一输入位置)

  }

  //合并单元格////////////////////////

  Dispatch.put(selection, "Text", "        ");

  Dispatch.call(selection, "MoveDown"); // 光标标往下一行

  Dispatch range2 = Dispatch.get(selection, "Range").toDispatch();

  Dispatch table2 = Dispatch.call(tables, "Add", range2, new Variant(8),

    new Variant(4), new Variant(1)).toDispatch(); // 设置行数,列数,表格外框宽度

  Dispatch t2 = Dispatch.call(tables, "Item", new Variant(2))

    .toDispatch();

  Dispatch beginCell = Dispatch.call(t2, "Cell", new Variant(1),

    new Variant(1)).toDispatch();

  Dispatch endCell = Dispatch.call(t2, "Cell", new Variant(4),

    new Variant(4)).toDispatch();

  Dispatch.call(beginCell, "Merge", endCell);

  for (int row = 1; row = Dispatch.get(

    Dispatch.get(t2, "rows").toDispatch(), "count").getInt(); row++) {

   for (int col = 1; col = Dispatch.get(

     Dispatch.get(t2, "columns").toDispatch(), "count").getInt(); col++) {

    if (row == 1) {

     Dispatch cell = Dispatch.call(t2, "Cell", new Variant(1),

       new Variant(1)).toDispatch();// 行,列

     Dispatch.call(cell, "Select");

     Dispatch.put(font, "Color", "1,1,1,0"); // 字型颜色

     Dispatch.put(selection, "Text", "merge Cell!");

    } else {

     Dispatch cell = Dispatch.call(t2, "Cell", new Variant(row),

       new Variant(col)).toDispatch();// 行,列

     Dispatch.call(cell, "Select");

     Dispatch.put(font, "Color", "1,1,1,0"); // 字型颜色

     Dispatch.put(selection, "Text", "cell" + row + col);

    }

   }

   Dispatch.call(selection, "MoveDown");

  }

  //Dispatch.call(selection, "MoveRight", new Variant(1), new Variant(1));// 取消选择

  // Object content = Dispatch.get(doc,"Content").toDispatch();

  // Word文档内容查找及替换////////////////////////

  Dispatch.call(selection, "TypeParagraph"); // 空一行段落

  Dispatch.put(align, "Alignment", "3"); // 1:置中 2:靠右 3:靠左

  Dispatch.put(font, "Color", 0);

  Dispatch.put(selection, "Text", "欢迎,Hello,world!");

  Dispatch.call(selection, "HomeKey", new Variant(6));// 移到开头

  Dispatch find = Dispatch.call(selection, "Find").toDispatch();// 获得Find组件

  Dispatch.put(find, "Text", "hello"); // 查找字符串"hello"

  Dispatch.put(find, "Forward", "True");// 向前查找

  // Dispatch.put(find, "Format", "True");// 设置格式

  Dispatch.put(find, "MatchCase", "false");// 大小写匹配

  Dispatch.put(find, "MatchWholeWord", "True"); // 全字匹配

  Dispatch.call(find, "Execute"); // 执行查询

  Dispatch.put(selection, "Text", "你好");// 替换为"你好"

  //使用方法传入的参数parameter调用word文档中的MyWordMacro宏//

  //Dispatch.call(document,macroName,parameter);

  //Dispatch.invoke(document,macroName,Dispatch.Method,parameter,new int[1]);

  //页眉,页脚处理////////////////////////

  Dispatch ActiveWindow = wordApp.getProperty("ActiveWindow")

    .toDispatch();

  Dispatch ActivePane = Dispatch.get(ActiveWindow, "ActivePane")

    .toDispatch();

  Dispatch View = Dispatch.get(ActivePane, "View").toDispatch();

  Dispatch.put(View, "SeekView", "9"); //9是设置页眉

  Dispatch.put(align, "Alignment", "1"); // 置中

  Dispatch.put(selection, "Text", "这里是页眉"); // 初始化时间

  Dispatch.put(View, "SeekView", "10"); // 10是设置页脚

  Dispatch.put(align, "Alignment", "2"); // 靠右

  Dispatch.put(selection, "Text", "这里是页脚"); // 初始化从1开始

  //书签处理(打开文档时处理)////////////////////////

  //Dispatch activeDocument = wordApp.getProperty("ActiveDocument").toDispatch();

  Dispatch bookMarks = Dispatch.call(document, "Bookmarks").toDispatch();

  boolean isExist = Dispatch.call(bookMarks, "Exists", "bookMark1")

    .getBoolean();

  if (isExist == true) {

   Dispatch rangeItem1 = Dispatch.call(bookMarks, "Item", "bookMark1")

     .toDispatch();

   Dispatch range1 = Dispatch.call(rangeItem1, "Range").toDispatch();

   Dispatch.put(range1, "Text", new Variant("当前是书签1的文本信息!"));

   String bookMark1Value = Dispatch.get(range1, "Text").toString();

   System.out.println(bookMark1Value);

  } else {

   System.out.println("当前书签不存在,重新建立!");

   Dispatch.call(bookMarks, "Add", "bookMark1", selection);

   Dispatch rangeItem1 = Dispatch.call(bookMarks, "Item", "bookMark1")

   .toDispatch();

   Dispatch range1 = Dispatch.call(rangeItem1, "Range").toDispatch();

   Dispatch.put(range1, "Text", new Variant("当前是书签1的文本信息!"));

   String bookMark1Value = Dispatch.get(range1, "Text").toString();

   System.out.println(bookMark1Value); 

  }

  //保存操作////////////////////////

  Dispatch.call(document, "SaveAs", "D:/wordOperate.doc");

  //Dispatch.invoke((Dispatch) doc, "SaveAs", Dispatch.Method, new Object[]{htmlPath, new Variant(8)}, new int[1]);   //生成html文件

  // 0 = wdDoNotSaveChanges

  // -1 = wdSaveChanges

  // -2 = wdPromptToSaveChanges

  //Dispatch.call(document, "Close", new Variant(0));

  // // worddoc.olefunction("protect",2,true,"");

  // // Dispatch bookMarks = wordApp.call(docs,"Bookmarks").toDispatch();

  // // System.out.println("bookmarks"+bookMarks.getProgramId());

  // //Dispatch.call(doc, "Save"); //保存

  // // Dispatch.call(doc, "Close", new Variant(true));

  // //wordApp.invoke("Quit",new Variant[]{});

  // wordApp.safeRelease();//Finalizers call this method

 }

}

「java配置jacob」java配置成功后怎么打开

java使用jacob客户端需要安装office,服务端需要安装吗?如果需要,服务器是Linux该如何解决?

您好,使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。

1、确保使用JACOB的服务器安装Microsoft的Office文件。

2、把jacob-1.14.3-x86.dll加入到环境变量path,最简单的方式是直接把这个文件拷贝到WINDOWS\system32目录下。

3、dll文件只会被classloader加载一次,因此一般情况下把jacob.jar放入WEB-INF/lib即可;但若有多个使用jacob的应用部署在同一个服务器,如tomcat,则需要把jacob.jar放在common/lib目录下。

常见异常处理:

1、java.lang.UnsatisfiedLinkError:no jacob in java.library.path

加载不到加载的jacob-1.14.3-x86.dll文件;可设置正确的path或者直接放入到WINDOWS\system32目录下。

2、java.lang.NoClassDefFoundError: Could not initialize class com.jacob.activeX.ActiveXComponent

在classpath找不到jacob.jar;可采用的方法是把jacob.jar放在common/lib目录下。

3、”java.lang.UnsatisfiedLinkError: C:\WINDOWS\system32\jacob-1.14.3-x86.dll: 由于应用程序配置不正确,应用程序未能启动。重新安装应用程序可能会纠正”这个问题。

如何利用Java-JACOB操作WORD文档

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(); // 最后一段

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)); //小四

Dispatch.call(document, "SaveAs", new Variant("C:

abc.doc")); // 保存一个新文档

ComThread.Release();//释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理

}

}

java 调用 ocx 使用jacob方法

需要通过Jacob这个开源组件与OCX控件进行通讯,Jacob下载地址:引用相应的包后就能ActiveXComponent app = new ActiveXComponent("Word.Application");以这种方式访问ActiveX对象

关于java配置jacob和java配置成功后怎么打开的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。