import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class ExcelToHtml {
public static void excelToHtml(String xlsfile, String htmlfile) {
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("转换成功!");
}
public static void main(String[] args) {
String paths = new String("F:\\test.xlsx"); //需要转换的excel文件
String savepaths = new String ("D:\\test.html"); //转换的html文件放在D盘下
excelToHtml(paths, savepaths);
}
}
转换后的结果: