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

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


}
### 使用 `com.jacob` 将 Word 文档换为 PDF 为了将 Word 文档换成 PDF 文件,可以利用 `com.jacob.activeX` 库,在 Java 环境下完成这一任务。下面展示了具体的方法以及一段示范性的代码。 #### 方法概述 通过引入 JACOB 作为 Maven 或 Gradle 项目中的依赖项来设置开发环境。JACOB 是一个允许 Java 调用 COM 对象的桥梁工具,因此能够轻松地操控 Microsoft Office 组件,比如 WordExcel[^2]。 对于 Maven 用户来说,可以在项目的 pom.xml 文件里加入如下配置: ```xml <dependencies> <dependency> <groupId>com.jacob</groupId> <artifactId>jacob</artifactId> <version>1.19</version> <scope>system</scope> <systemPath>${basedir}/src/main/resources/lib/jacob.jar</systemPath> </dependency> </dependencies> ``` 这段 XML 片段指定了 jacob.jar 的位置,并将其设为系统的本地路径[^3]。 #### 示例代码 以下是使用 JACOB 实现 Word 至 PDF 换的具体实现方式: ```java import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; public class WordToPdfConverter { public static void convert(String wordFilePath, String pdfFilePath) throws Exception { ActiveXComponent app = new ActiveXComponent("Word.Application"); try { Dispatch documents = app.getProperty("Documents").toDispatch(); Dispatch document = Dispatch.call(documents, "Open", wordFilePath).toDispatch(); File file = new File(pdfFilePath); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); // 创建目录结构 } Dispatch.call(document, "SaveAs", pdfFilePath, 17); // 参数17表示保存为PDF格式 Dispatch.call(document, "Close", false); } catch (Exception e) { throw e; } finally { app.invoke("Quit"); // 关闭应用程序实例 } } } ``` 此段程序定义了一个名为 `convert` 的静态方法接收两个字符串参数分别代表源 Word 文件的位置和目标 PDF 文件应被存储的地方。它创建了一个新的 Word Application 进程并打开了指定的 .doc(x) 文件;接着调用了 SaveAs 函数以 PDF 形式另存新副本到给定的目标地址上;最后关闭了打开过的文档及其所属的应用程序进程。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值