jacob 另存word ,excel 为html,xml格式

本文介绍了一个使用Java编写的工具类,能够将Microsoft Word和Excel文件转换为HTML或XML格式。该工具利用了Jacob库来操作Office应用,实现不同格式间的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.Random;


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


public class JacobUtil {

public static final int WORD_HTML = 8;
public static final int WORD_TXT = 7;
public static final int EXCEL_HTML = 44;
public static final int EXCEL_XML = 46;
public static final int EXCEL_43 = 43; // Excel 2003 测试可用 


/**
* WORD转HTML

* @param docfile
*            WORD文件全路径
* @param htmlfile
*            转换后HTML存放路径
*/
public static void wordToHtml(String docfile, String htmlfile) {
// 初始化
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
try {
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { docfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(WORD_HTML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
}


/**
* EXCEL转HTML

* @param xlsfile
*            EXCEL文件全路径
* @param htmlfile
*            转换后HTML存放路径
*/
public static void excelToHtml(String xlsfile, String htmlfile) {
// 初始化
ComThread.InitSTA();
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(EXCEL_HTML) }, new int[1]);
Dispatch.call(excel, "Close", new Variant(false));
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
}

/**
* EXCEL转XML

* @param xlsfile
*            EXCEL文件全路径
* @param xmlfile
*            转换后XML存放路径
*/
public static void excelToXml(String xlsfile, String xmlfile) {
// 初始化
ComThread.InitSTA();
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[] {
xmlfile, new Variant(EXCEL_XML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
}

public static void main(String[] args) {
excelToHtml(
"E:\\test.xls",
"E:\\" + new Random().nextInt(1000) + ".html");
}


}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值