我正在使用jacob
库处理Excel。我正试图通过基本操作与excel交互。我正在执行下面的代码并收到错误。我该怎么办?
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.jacob.com.Dispatch.createInstance(Ljava/lang/String;)V
at com.jacob.com.Dispatch.createInstance(Native Method)
at com.jacob.com.Dispatch.<init>(Dispatch.java)
at com.jacob.activeX.ActiveXComponent.<init>(ActiveXComponent.java)
at DispatchTest.main(DispatchTest.java:7)
public class DispatchTest {
public static void main(String[] args)
{
ActiveXComponent xl = new ActiveXComponent("Excel.Application");
Object xlo = xl.getObject();
try {
System.out.println("version="+xl.getProperty("Version"));
System.out.println("version="+Dispatch.get(xlo, "Version"));
xl.setProperty("Visible", new Variant(true));
Object workbooks = xl.getProperty("Workbooks").toDispatch();
Object workbook = Dispatch.get(workbooks,"Add").toDispatch();
Object sheet = Dispatch.get(workbook,"ActiveSheet").toDispatch();
Object a1 = Dispatch.invoke(sheet, "Range", Dispatch.Get,
new Object[] {"A1"},
new int[1]).toDispatch();
Object a2 = Dispatch.invoke(sheet, "Range", Dispatch.Get,
new Object[] {"A2"},
new int[1]).toDispatch();
Dispatch.put(a1, "Value", "123.456");
Dispatch.put(a2, "Formula", "=A1*2");
System.out.println("a1 from excel:"+Dispatch.get(a1, "Value"));
System.out.println("a2 from excel:"+Dispatch.get(a2, "Value"));
Variant f = new Variant(false);
Dispatch.call(workbook, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
xl.invoke("Quit", new Variant[] {});
}
}
}
答案 0 :(得分:0)
我以这种方式使用它,但仅限于ExportAsPdfFormat
final String application = "Excel.Application";
app = new ActiveXComponent(application);
final ActiveXComponent workbooks = app.getPropertyAsComponent("Workbooks");
app.setProperty("visible", false);
app.setProperty("DisplayAlerts", false);
app.setProperty("AutomationSecurity", 3);
d = Dispatch.call(workbooks, "Open", this.getFileName(), ExcelConverter.UPDATELINKS, Variant.VT_FALSE, Variant.VT_MISSING, Variant.VT_MISSING, Variant.VT_MISSING, Variant.VT_TRUE).toDispatch();
Dispatch.call(d, "ExportAsFixedFormat", ExcelConverter.XLTYPEPDF, this.getConvertedFileName(), ExcelConverter.XLQUALITYSTANDARD, Variant.VT_TRUE, Variant.VT_FALSE);
答案 1 :(得分:0)
我正在使用Jacob lib 1.18版,它需要在其他任何东西之前进行初始化。它附带两个dll文件,每个文件用于不同的系统版本&#34; jacob-1.18-x64.dll&#34;和&#34; jacob-1.18-x86.dll&#34;。
String libFile = System.getProperty("os.arch").equals("amd64") ? "/jacob-1.18-x64.dll" : "/jacob-1.18-x86.dll";
/* Read DLL file*/
InputStream inputStream = load_the_file_as_stream();
/**
* Step 1: Create temporary file under <%user.home%>\AppData\Local\Temp\jacob.dll
* Step 2: Write contents of `inputStream` to that temporary file.
*/
File temporaryDll = File.createTempFile("jacob", ".dll");
FileOutputStream outputStream = new FileOutputStream(temporaryDll);
byte[] array = new byte[8192];
for (int i = inputStream.read(array); i != -1; i = inputStream.read(array)) {
outputStream.write(array, 0, i);
}
outputStream.close();
System.setProperty(LibraryLoader.JACOB_DLL_PATH, temporaryDll.getAbsolutePath());
LibraryLoader.loadJacobLibrary();
/* Temporary file will be removed after terminating-closing-ending the application-program */
temporaryDll.deleteOnExit();