使用jacob 抛出的异常,哪位大侠遇到过?

coff_cola 2009-08-19 11:44:03
java.lang.NoClassDefFoundError: Could not initialize class com.jacob.activeX.ActiveXComponent
at com.word.util.Document.<init>(Document.java:28)
at com.word.util.WordBean.newDoce(WordBean.java:10)
at com.word.action.WordAction.savewords(WordAction.java:56)
at com.word.action.WordAction.saveword(WordAction.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:274)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
...全文
2116 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
coff_cola 2009-08-23
  • 打赏
  • 举报
回复
我现在刚开始学jacob,有些方法只是知道大概做什么用的,并不知道确切是怎么实现的,所以现在遇到问题很难搞定,虽然也有调试,但是调试没有发现问题,实际是不知道从哪里去分析程序出错的原因,就像楼上说的出错的原因是映射的问题,但是是什么映射?怎么映射的?现在项目很赶,但是我现在卡在这地方了,很郁闷~~
tidelgl 2009-08-23
  • 打赏
  • 举报
回复
com.jacob.com.ComFailException: Can't map name to dispid: SaveAs

居然是映射的问题,楼主怎么找不出的,楼主应该多加调试,调试可以解决大部分问题.
coff_cola 2009-08-22
  • 打赏
  • 举报
回复
谢谢楼上的哈,我改了一下程序,换了一下jacob的jar为1.15版本(jdk 1.5,tomcat 6.0),卸掉office2007只用2003,现在上面的问题排除了,但是又出现了新的问题,我认真的检查了一下程序确实麽看出来哪里错,我把源码贴出来请大家帮我看看,谢谢先!

public class WordAction extends DispatchAction
{
// 请求入口

public ActionForward saveword(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception
{
String filemodule="D:/bb.doc";
PrivilegeUser pr=new PrivilegeUser();
pr.setName(request.getParameter("name"));
pr.setSex(request.getParameter("sex"));
this.savewords(filemodule,pr.getName(),pr);
request.setAttribute("pr", pr);
return mapping.findForward("showxx");
}
public void savewords(String filemodule,String username,PrivilegeUser pu)
{
try
{
System.out.println("-------------");
WordBean wb=new WordBean();
wb.newDoce(filemodule, "d:/", username);//根据用户名创建一个word文件
String infile="d:/"+username+"个人资料.doc";
wb.replaceWord(infile,pu);//根据
}
catch (Exception e)
{
e.printStackTrace();
}
}
}



public class WordBean // extends Panel
{
Document docc =null;
public void newDoce(String modulefile, String newfilepath, String username) {
try {
docc = new Document();
docc.newDoc(modulefile, newfilepath + username + "个人资料.doc");
docc.close(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
public void replaceWord(String inFile, PrivilegeUser pu) {
try {
boolean flag = true;
String[] module = { "name", "sex" };
String[] pulist = { pu.getName(), pu.getSex(), pu.getPhoto() };
for (int i = 0; i < module.length; i++) {
if (flag) {
docc = new Document();
docc.open(inFile);
if (module[i].equals("pic")) {
File file = new File(pulist[i]);
flag = docc.insertimageAtBookmark(module[i], file);
} else
flag = docc.insertAtBookmark(module[i], pulist[i]);
docc.close(true);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

主要的处理方法

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

// Referenced classes of package com.heavenlake.wordapi:
// StyleWarning, TableStyleWarning

public class Document
{

private Dispatch doc;

private Dispatch documents;

// 选定的范围或插入点
private Dispatch selection;

private static String newFileName;

private ActiveXComponent word;

public Document()
{

word = new ActiveXComponent("Word.Application");
// word.setProperty("Visible", new Variant(false)); //不可见打开word
documents = word.getProperty("Documents").toDispatch();
// doc=Dispatch.call(documents, "Add").toDispatch();// create
}

private void close(Dispatch doc, boolean saveOnExit)
{
if (doc != null)
{
Dispatch.call(doc, "Close", new Variant(saveOnExit));
word.invoke("Quit", new Variant[0]);
ComThread.Release();
}
}

public void close(boolean saveOnExit) throws Exception
{
if (newFileName!=null&&newFileName.length() > 0)
{
if (saveOnExit)
{
saveAs(newFileName);
close(doc, false);
}
}
else
{
close(doc, saveOnExit);
}
}

private boolean find(Dispatch selection, String toFindText)
{
ActiveXComponent _tmp = word;
if (toFindText == null || toFindText.equals("")){

return false;

}
Dispatch find = ActiveXComponent.call(selection, "Find").toDispatch();
Dispatch.put(find, "Text", toFindText);
Dispatch.put(find, "Forward", "True");
Dispatch.put(find, "Format", "True");
Dispatch.put(find, "MatchCase", "True");
Dispatch.put(find, "MatchWholeWord", "False");
return Dispatch.call(find, "Execute").getBoolean();
}

public boolean find(String toFindText)
{
boolean flag = find(selection, toFindText);
return flag;
}

public void insert(File imageFile)
{
if (imageFile.exists())
{
replaceImage(selection, imageFile.getAbsolutePath());
}
}

public void insertAtBookmark(String bookmarkName, File imageFile)
{
if (imageFile.exists())
{
Dispatch bookmarkRange = getBookmark(bookmarkName);
replaceImage(bookmarkRange, imageFile.getAbsolutePath());
insertBookmark(bookmarkRange, bookmarkName);
}
else
{
System.out.println("图片文件不存在");
}
}
public void insertln()
{
Dispatch range = Dispatch.get(selection, "Range").toDispatch();
Dispatch.call(range, "InsertParagraphAfter");
moveDown(1);
}

public void newDoc(String template, String fileName)
{
File file = new File(template);
if (file.exists())
{
doc = Dispatch.call(documents, "Open", template).toDispatch();
newFileName = fileName;//命名文件,但没把文件保存.
}
else
{
System.out.println("模板文件不存在");
}
}

public void newDoc(String fileName) throws Exception
{
doc = Dispatch.call(documents, "Open").toDispatch();
selection = Dispatch.get(word, "Selection").toDispatch();
newFileName = fileName;
}

public void open(String fileName)
{
File file = new File(fileName);
if (file.exists())
{
doc = Dispatch.call(documents, "Open", fileName).toDispatch();
selection = Dispatch.get(word, "Selection").toDispatch();
}
else
{
System.out.println("文件不存在,不能打开");
}
}

public List readTable(int tbIndex, int fromRow) throws Exception
{
ArrayList tableData = new ArrayList();
if (fromRow < 1)
{
fromRow = 1;
}
if (tbIndex < 1)
{
tbIndex = 1;
}
Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
int tablesCount = Dispatch.get(tables, "Count").getInt();
if (tablesCount < tbIndex)
{
throw new Exception(
"\u6307\u5B9A\u7684\u8868\u683C\u4E0D\u5B58\u5728\u3002\u6253\u5F00\u7684\u6587\u6863"
+ "\u4E2D\u6CA1\u6709\u8868\u683C\u3002");
}
if (tablesCount < tbIndex)
{
throw new Exception(
"\u6307\u5B9A\u7684\u8868\u683C\u4E0D\u5B58\u5728\u3002\u6253\u5F00\u7684\u6587\u6863"
+ "\u4E2D\u53EA\u6709" + tablesCount + "\u5F20\u8868\u3002");
}
Dispatch table = Dispatch.call(tables, "Item", new Variant(tbIndex)).toDispatch();
Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
int rowsCount = Dispatch.get(rows, "Count").getInt();
Dispatch Columns = Dispatch.get(table, "Columns").toDispatch();
int Columnscount = Dispatch.get(Columns, "Count").toInt();
for (int i = fromRow; i < rowsCount + 1; i++)
{
ArrayList rowData = new ArrayList();
for (int j = 1; j < Columnscount + 1; j++)
{
Dispatch cell = Dispatch.call(table, "Cell", i + "", j + "").toDispatch();
Dispatch Range = Dispatch.get(cell, "Range").toDispatch();
String text = Dispatch.get(Range, "Text").toString();
rowData.add(text.substring(0, text.length() - 1));
}
tableData.add(rowData);
}
return tableData;
}

public boolean insertAtBookmark(String bookmarkName, String newText)
{
boolean flag = insertAtBookmark(bookmarkName, newText, null);

return flag;
}

public boolean insertAtBookmark(String bookmarkName, String newText, String styleName)
{
try
{
Dispatch bookmarkRange;
bookmarkRange = getBookmark(bookmarkName);
replace(newText);
insertBookmark(bookmarkRange, bookmarkName);
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
private Dispatch getBookmark(String bookmarkName)
{
//获取bookmarkName的标签
Dispatch bookmark = Dispatch.call(doc, "Bookmarks", bookmarkName).toDispatch();
bookmark = Dispatch.get(bookmark, "Range").toDispatch();//在全局的范围内获取
return bookmark;
}
private void replace( String newText)
{
Dispatch.put(selection, "Text", newText);
}

private void insertBookmark(Dispatch selection, String bookmarkName)
{//往dispatch对象添加一个书签对象.
Dispatch Bookmarks = Dispatch.get(doc, "Bookmarks").toDispatch();
Dispatch.call(Bookmarks, "Add", bookmarkName, selection);
}

public boolean replace(String toFindText, String newText)
{
if (!find(toFindText))

return false;

boolean leaf = false;
boolean flag = find(toFindText);
if (flag)
{
replace( newText);
leaf = true;
}
return leaf;
}

/**
* 全局替换文本
*
* @param toFindText
* 查找字符串
* @param newText
* 要替换的内容
*/


private void replaceAll(Dispatch selection, String oldText, String newText)
{
moveStart(selection);
for (; find(selection, oldText); Dispatch.call(selection, "MoveRight"))
{
replace( newText);
}
}

public void replaceAll(String oldText, String newText)
{
replaceAll(selection, oldText, newText);
}
public void saveAs(String outputFile)
{
Dispatch.call(doc, "SaveAs", outputFile);

}

public boolean insertimageAtBookmark(String bookmarkName, File imageFile) throws Exception
{
//插入图片
if (imageFile.exists())
{
Dispatch bookmarkRange = getBookmark(bookmarkName);//获取指定的标签对象
replaceImage(bookmarkRange, imageFile.getAbsolutePath());
insertBookmark(bookmarkRange, bookmarkName);
return true;
}else
{
return false;
}
}
private void replaceImage(Dispatch selection, String imagePath)
{//入word里边添加图片s
Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(), "AddPicture", imagePath);
}


}



问题就出在这个方法:
public void saveAs(String outputFile)
{
Dispatch.call(doc, "SaveAs", outputFile);

}
异常信息:
com.jacob.com.ComFailException: Can't map name to dispid: SaveAs
tidelgl 2009-08-22
  • 打赏
  • 举报
回复
tidelgl 2009-08-22
  • 打赏
  • 举报
回复
楼主,变量定义时没有对吧...
该初始化的就是初始化
coff_cola 2009-08-21
  • 打赏
  • 举报
回复
换了jdk版本之后这个问题解决了,可是又出了另外一个问题

com.jacob.com.ComFailException: Invoke of: Add
Source: Microsoft Word
Description: Word

at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:804)
at com.jacob.com.Dispatch.callN(Dispatch.java:440)
at com.jacob.com.Dispatch.call(Dispatch.java:520)
at com.word.util.Document.newDoc(Document.java:176)
at com.word.util.WordBean.newDoce(WordBean.java:11)
at com.word.action.WordAction.savewords(WordAction.java:56)
at com.word.action.WordAction.saveword(WordAction.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:274)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)
java.lang.NullPointerException

coff_cola 2009-08-20
  • 打赏
  • 举报
回复
有可能,我jdk 用的1.6的,我改了试试先,谢谢楼上的!
jinchun1234 2009-08-20
  • 打赏
  • 举报
回复
估计是包的版本不对应

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧