第一步 需要下载jar包和jacob-1.14.3-x64.dll
* <dependency>
* <groupId>net.sf.jacob-project</groupId>
* <artifactId>jacob</artifactId>
* <version>1.14.3</version>
* </dependency>
*
第二步 把jacob-1.14.3-x64.dll 放到java/bin目录下


电脑环境 win10 office2016
支持文件类型 doc,docx,xls,xlsx,ppt,pptx


下面是源代码
package com.example.demo.xs;

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

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* 需要jar包和jacob-1.14.3-x64.dll
* <dependency>
* <groupId>net.sf.jacob-project</groupId>
* <artifactId>jacob</artifactId>
* <version>1.14.3</version>
* </dependency>
*
* 把jacob-1.14.3-x64.dll 放到java/bin目录下
*
* 电脑环境 win10 office2016
* @author 苑庆涛
* @create 2019-07-22 16:22
*/
public class Test {
private static Logger logger = Logger.getLogger(WordApp.class.getName());
public static void main(String[] args) {

}

/**
* 示例 toPdf("d:\\testold\\e3.xls","d:\\testnew\\e3.pdf","xlsx")
* @param src 文件路径
* @param tar pdf文件路径
* @param type 文件类型
*/
public static void toPdf(String src,String tar,String type){
if(type.startsWith("doc")){
wordToPdf(src,tar);
}else if(type.startsWith("xls")){
excelToPdf(src,tar);
}else if(type.startsWith("ppt")){
pptToPdf(src,tar);
}
}
public static void wordToPdf(String src, String tar) {
ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
app.setProperty("AutomationSecurity", new Variant(3));
Dispatch docs = app.getProperty("Documents").toDispatch();
doc = Dispatch.call(docs, "Open",
src, false, true)
.toDispatch();
File target = new File(tar);
if (target.exists()) {
target.delete();
}
Dispatch.call(doc, "SaveAs", tar,17);
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
} finally {
if (doc != null) {
try {
Dispatch.call(doc, "Close", false);
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
}
}
if (app != null) {
try {
app.invoke("Quit",0);
app.safeRelease();
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
}
}

}
}

public static void excelToPdf(String src, String tar) {
ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("Excel.Application");
app.setProperty("Visible", new Variant(false));
app.setProperty("AutomationSecurity", new Variant(3));
Dispatch docs = app.getProperty("Workbooks").toDispatch();
doc = Dispatch.call(docs, "Open", src, false, true)
.toDispatch();
File target = new File(tar);
if (target.exists()) {
target.delete();
}
Dispatch.call(doc, "ExportAsFixedFormat", 0, tar );
} finally {
if (doc != null) {
try {
Dispatch.call(doc, "Close", false);
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
}
}
if (app != null) {
try {
app.invoke("Quit");
app.safeRelease();
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
}
}
}
}

public static void pptToPdf(String src, String tar) {
ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("PowerPoint.Application");
app.setProperty("AutomationSecurity", new Variant(3));
Dispatch docs = app.getProperty("Presentations").toDispatch();
doc = Dispatch.call(
docs,
"Open", src, true, true,false).toDispatch();
File target = new File(tar);
if (target.exists()) {
target.delete();
}
Dispatch.call(doc, "SaveAs",tar,32);
} finally {
if (doc != null) {
try {
Dispatch.call(doc, "Close");
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
}
}
if (app != null) {
try {
app.invoke("Quit");
app.safeRelease();
} catch (Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
}
}
}
}


}

版权声明:本文为yuanqt原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yuanqt/p/11227020.html