第一步 需要下载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);
}
}
}
} }

word,excel,ppt转pdf的更多相关文章

  1. word/excel/ppt 2 PDF

    PHP 实现 word/excel/ppt 转换为 PDF 一般最常见的就是利用OpenOffice来转换,来看看实现的核心代码: class PDFConverter { private $com; ...

  2. word,excel,ppt转Pdf,Pdf转Swf,通过flexpaper+swftools实现在线预览

    其实这是我好几年前的项目,现在再用这种方式我也不建议了,毕竟未来flash慢慢会淘汰,此方式也是因为目测大部分人都装了flash,才这么做的,但是页面展示效果也不好.其实还是考虑收费的控件,毕竟收费的 ...

  3. PHP 实现 word/excel/ppt 转换为 PDF

    前段时间负责公司内部文件平台的设计,其中有一个需求是要能够在线浏览用户上传的 office 文件. 我的思路是先将 office 转换成 PDF,再通过 pdf.js 插件解析 PDF 文件,使其能在 ...

  4. 21.PHP实现Word/Excel/PPT转换为PDF

    参考文档: https://www.cnblogs.com/woider/p/7003481.html http://blog.csdn.net/aoshilang2249/article/detai ...

  5. PDF/WORD/EXCEL/PPT 文档在线阅读

    查资料看了2种解决方法: 1.通过办公软件dll转换,用flans去看 2.通过Aspose转换成pdf格式,在用js前台读pdf(我用的pdf.js) 今天我解决的就是WORD/EXCEL/PPT ...

  6. Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结

    Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word  excel pdf 的web预览要求 ...

  7. 在线文档转换API word,excel,ppt等在线文件转pdf、png

    在线文档转换API提供word,excel,ppt等在线文件转pdf.png等,文档:https://www.juhe.cn/docs/api/id/259 接口地址:http://v.juhe.cn ...

  8. java 如何将 word,excel,ppt如何转pdf --openoffice (1)

    承上启下,可折叠 上一篇说的是:服务器是windows server时,用jacob将msoffice(指的是word,excel,ppt)转换成pdf. 若被部署项目的服务器是centOS等linu ...

  9. 微信小程序云开发-云存储-下载并打开文件文件(word/excel/ppt/pdf)

    一.wxml文件 1.写文本框,用来获取文件链接. 2.按钮,点击下载文件 <!-- 下载文件(word/excel/ppt/pdf等) --> <view class=" ...

随机推荐

  1. Comet OJ 夏季欢乐赛 烤面包片

    烤面包片 https://cometoj.com/contest/59/problem/C?problem_id=2698 题目描述 鸡尾酒最喜欢吃东北的烤面包片了.每次到东北地区的区域赛或者是秦皇岛 ...

  2. 排序算法-桶排序(Java)

    package com.rao.sort; import java.util.*; /** * @author Srao * @className BucketSort * @date 2019/12 ...

  3. Taro-UI 2.0样式在H5上生效,微信小程序不生效?

    答案: https://taro-ui.aotu.io/#/docs/questions taro-ui 自定义样式覆盖小程序组件样式使用到了 globalClass 这个微信小程序特性,由于微信小程 ...

  4. redis主从+redis的哨兵模式

    三台机器分布 192.168.189.129  //  master的角色 192.168.189.130  //  slave1的角色 192.168.189.131  //  salve2的角色 ...

  5. SpreadJS 生成报表

    空了再写个完整的demo吧 //报表控件 输入参数待定 function SpreadObj(response) { var spread = null; //数据列表 var dataArray = ...

  6. BFS实现8数码问题,思考与总结

    BFS实现8数码问题,思考与总结 今天中午学习了二叉树的线索化与线索化遍历,突然有一种想实现八数码问题的冲动,因为它的初级解决方式是BFS(广度优先搜索算法).于是我开始编程. 没想到一编就是一个下午 ...

  7. Spring配置中<bean>的id和name属性

    在BeanFactory的配置中,<bean>是我们最常见的配置项,它有两个最常见的属性,即id和name,最近研究了一下,发现这两个属性还挺好玩的,特整理出来和大家一起分享. 1.id属 ...

  8. Expect Command And How To Automate Shell Scripts Like Magic

    In the previous post, we talked about writing practical shell scripts and we saw how it is easy to w ...

  9. 一台Linux服务器(4C8G配置)可以负载百万个连接?

    一台Linux服务器可以负载多少个连接? 首先我们来看如何标识一个TCP连接?系统是通过一个四元组来识别,(src_ip,src_port,dst_ip,dst_port)即源IP.源端口.目标IP. ...

  10. ElasticSearch 线程池类型分析之 ExecutorScalingQueue

    ElasticSearch 线程池类型分析之 ExecutorScalingQueue 在ElasticSearch 线程池类型分析之SizeBlockingQueue这篇文章中分析了ES的fixed ...