java中的jacob将word文档转化为HTML文件问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中的jacob将word文档转化为HTML文件问题相关的知识,希望对你有一定的参考价值。
2015-04-29 10:17:32 DEBUG FileManager.java:315 | 计算上传路径:201504/
2015-04-29 10:17:32 DEBUG FileManager.java:184 | 开始写入文件:E:\Tomcat6\webapps\hurocms\data\doc\201504\2a92d777fbe741d6a5dc.xls
2015-04-29 10:17:32 DEBUG htmlCreator.java:27 | 文档转换,srcDoc:E:/Tomcat6/webapps/hurocms/data/doc/201504/2a92d777fbe741d6a5dc.xls,targetHtml:E:/Tomcat6/webapps/hurocms/data/html/201504/2a92d777fbe741d6a5dc.html
2015-04-29 10:17:32 INFO DocToHtmlUtil.java:77 | Start to convert Excel to Html..., path : E:/Tomcat6/webapps/hurocms/data/doc/201504/2a92d777fbe741d6a5dc.xls
2015-04-29 10:17:32 ERROR FileManager.java:202 | 写入文件20130925实际传给数据平台数据.xls异常
com.jacob.com.ComFailException: Can't co-create object
问题描述:当用tomcat启动以后,运行30分钟以后会不能执行;流程,上传一个word文档,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
参考技术A 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]使用jacob进行服务端word转html pdf
参照:http://www.cnblogs.com/qingxinblog/articles/3399454.html
1,必要条件: 服务器为windows,必须安装office 使用jacob进行中间件调用com组件打开word进程 另存文档为其他格式
2,注意条件:使用eclipse必须将jacob的jar包添加到项目 并add build path ,同时将jacob的dll 文件放置到JAVA_HOME\jre\bin中,如果为部署服务 将jar包放置到WEB_INF下的Lib目录
代码如下:
1 package demo; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import com.jacob.com.*; 9 import com.jacob.activeX.*; 10 11 public class hdzServlet extends HttpServlet{ 12 13 protected void service (HttpServletRequest req, HttpServletResponse res) 14 throws ServletException, IOException { 15 res.getWriter().println("ni hao hdz!"); 16 String docfile = "F:\\123.doc"; 17 String htmlfile = "F:\\123.pdf"; 18 hdzServlet.wordToHtml(docfile, htmlfile); 19 20 } 21 22 public static final int WORD_HTML = 5; 23 24 /** 25 * WORD转HTML 26 * @param docfile WORD文件全路径 27 * @param htmlfile 转换后HTML存放路径 28 */ 29 public static void wordToHtml(String docfile, String htmlfile) 30 { 31 // 启动word应用程序(Microsoft Office Word 2003) 32 ActiveXComponent app = new ActiveXComponent("Word.Application"); 33 System.out.println("*****正在转换...*****"); 34 try 35 { 36 // 设置word应用程序不可见 37 app.setProperty("Visible", new Variant(false)); 38 // documents表示word程序的所有文档窗口,(word是多文档应用程序) 39 Dispatch docs = app.getProperty("Documents").toDispatch(); 40 // 打开要转换的word文件 41 Dispatch doc = Dispatch.invoke( 42 docs, 43 "Open", 44 Dispatch.Method, 45 new Object[] { docfile, new Variant(false), 46 new Variant(true) }, new int[1]).toDispatch(); 47 // 作为html格式保存到临时文件 48 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { 49 htmlfile, new Variant(WORD_HTML) }, new int[1]); 50 // 关闭word文件 51 Dispatch.call(doc, "Close", new Variant(false)); 52 } 53 catch (Exception e) 54 { 55 e.printStackTrace(); 56 } 57 finally 58 { 59 //关闭word应用程序 60 app.invoke("Quit", new Variant[] {}); 61 } 62 System.out.println("*****转换完毕********"); 63 } 64 65 66 }
以上是关于java中的jacob将word文档转化为HTML文件问题的主要内容,如果未能解决你的问题,请参考以下文章
Java 使用 jacob 将 word 文档转换为 pdf 文件
Java 使用 jacob 将 word 文档转换为 pdf 文件