我正在尝试使用jacob运行excel,但是它一直在抛出异常,一直在搜索这样的异常原因,但没有好的
package com.se.jaexcel;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class JExcel {
/**
* @param args
*/
public static void main(String[] args) {
ActiveXComponent xl = new ActiveXComponent("Excel.Application");
}
}
例外是
Exception in thread "main" com.jacob.com.ComFailException: Can't QI object for IDispatch
at com.jacob.com.Dispatch.createInstanceNative(Native Method)
at com.jacob.com.Dispatch.<init>(Dispatch.java:99)
at com.jacob.activeX.ActiveXComponent.<init>(ActiveXComponent.java:58)
at com.se.jaexcel.JExcel.main(JExcel.java:14)
答案 0 :(得分:0)
您没有加载任何本机dll。在下面的示例中,c:\ myapp \ lib包含jacob-1.18-M2-x64.dll和jacob-1.18-M2-x86.dll。如果您不想从静态位置加载它们,请参阅http://www.javaquery.com/2013/12/getting-started-with-jacob-example-with.html以了解如何从资源加载DLL。
private static void loadLibrary(final String appDir) throws IOException {
final String libFile = "amd64".equals(System.getProperty("os.arch")) ?
"/jacob-1.18-M2-x64.dll" : "/jacob-1.18-M2-x86.dll";
System.setProperty(LibraryLoader.JACOB_DLL_PATH,
Paths.get(appDir, "lib", libFile).toString());
LibraryLoader.loadJacobLibrary();
}
public static void main(String[] args) {
loadLibrary("c:\\myapp");
ActiveXComponent xl = new ActiveXComponent("Excel.Application");
}