package com;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class test {
public static void main(String[] args) {
String xlsfile = new String("E:\\1.xls"); //需要转换的excel文件,运行环境为JDK1.8
String htmlfile = new String("E:\\test.htm"); //转换的html文件
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel
try {
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(
excels,
"Open",
Dispatch.Method,
new Object[]{xlsfile, new Variant(false),
new Variant(true)}, new int[1]).toDispatch();
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[]{
htmlfile, new Variant(44)}, new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[]{});
}
System.out.println("转换成功!");
}
}