1.下载jacob.jar包  网址:https://sourceforge.net/projects/jacob-project/files/jacob-project/

2.导入到本地仓库:mvn install:install-file -Dfile=[jar包名 ] -DgroupId=[目录a] -DartifactId=[目录b]-Dversion=[版本] -Dpackaging=jar

3.

将红色框文件拷贝到jdk目录下的jre/bin目录中去同时  C:\Windows\System32目录页也要拷贝一份

4.开始编码 工具类

package com.example.mybatis.util;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PDFConvert {

private static final Logger logger = LoggerFactory.getLogger(PDFConvert.class);

private static final Integer WORD_TO_PDF_OPERAND = 17;
private static final Integer EXCEL_TO_PDF_OPERAND = 0;

/**
* @Description excel转pdf
* @Param [excelFile, pdfPath]
* @return boolean
**/
public static void excel2PDF(String excelFile, String pdfPath) {
ActiveXComponent ax = null;
Dispatch excel = null;
try {
ComThread.InitSTA();
ax = new ActiveXComponent("Excel.Application");
ax.setProperty("Visible", new Variant(false));
ax.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
Dispatch excels = ax.getProperty("Workbooks").toDispatch();

Object[] obj = new Object[]{
excelFile,
new Variant(false),
new Variant(false)
};
excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch();

// 转换格式
Object[] obj2 = new Object[]{
new Variant(EXCEL_TO_PDF_OPERAND), // PDF格式=0
pdfPath,
new Variant(0) //0=标准 (生成的PDF图片不会变模糊) ; 1=最小文件
};
Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method,obj2, new int[1]);
} catch (Exception es) {
logger.error("excel转pdf异常:"+excelFile,es);
} finally {
if (excel != null) {
Dispatch.call(excel, "Close", new Variant(false));
}
if (ax != null) {
ax.invoke("Quit", new Variant[] {});
ax = null;
}
ComThread.Release();
}
}

/**
* word转pdf
*
* @param wordlFile
* @param pdfPath
* @return
*/
public static void word2pdf(String wordlFile, String pdfPath) {
ActiveXComponent app = null;
Dispatch doc = null;
try {
ComThread.InitSTA();
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", false);
Dispatch docs = app.getProperty("Documents").toDispatch();
Object[] obj = new Object[]{
wordlFile,
new Variant(false),
new Variant(false),//是否只读
new Variant(false),
new Variant("pwd")
};
doc = Dispatch.invoke(docs, "Open", Dispatch.Method, obj, new int[1]).toDispatch();
// Dispatch.put(doc, "Compatibility", false); //兼容性检查,为特定值false不正确
Dispatch.put(doc, "RemovePersonalInformation", false);
Dispatch.call(doc, "ExportAsFixedFormat", pdfPath, WORD_TO_PDF_OPERAND); // word保存为pdf格式宏,值为17
}catch (Exception es) {
logger.error("word转pdf异常:"+wordlFile,es);
} finally {
if (doc != null) {
Dispatch.call(doc, "Close", false);
}
if (app != null) {
app.invoke("Quit", 0);
}
ComThread.Release();
}
}

}

5.测试类
@Test
public void contextLoads() {

String wordFile = "C:\\Users\\intasect\\Desktop\\测试\\测试.xlsx";
String pdfFile = "C:\\Users\\intasect\\Desktop\\测试\\测试.pdf";
System.out.println("开始转换...");
// 开始时间
long start = System.currentTimeMillis();
PDFConvert.excel2PDF(wordFile,pdfFile );

long end = System.currentTimeMillis();
System.out.println("转换成功,用时:" + (end - start) + "ms");

}
6.结果
 

最新文章

  1. oracle实例恢复之检查点队列
  2. 【POJ 3321】Apple Tree
  3. spring cache
  4. CI框架SESSION重写
  5. MANIFEST.INF!JAR规范中
  6. 如果jsp提交到action为空指针的话
  7. Java多线程-新特性-线程池
  8. 如何开启MYSQL远程连接权限
  9. Mysql 应该选择什么引擎
  10. svn笔记4属性Properties
  11. 转载:21个免费的UI界面设计工具、资源及网站
  12. Vue - 使用命令行搭建单页面应用
  13. 用eclipes写第一个HelloWorld
  14. requestAnimationFrame Web中写动画的另一种选择
  15. 20160208.CCPP体系详解(0018天)
  16. jquery父元素和子元素点击事件传递问题_不可把父元素的事件传递给子元素_事件无限循环传递
  17. CCF CSP 201803-3 URL映射
  18. iOS笔记之网络
  19. 【转载】你真的会浮点数与整型数的"互转"吗?
  20. 【刷题】BZOJ 2753 [SCOI2012]滑雪与时间胶囊

热门文章

  1. java入门与进阶-P1.5+P1.6
  2. VS针对Linux远程调试步骤
  3. Grafana 系列文章(九):开源云原生日志解决方案 Loki 简介
  4. 数据湖Hudi与对象存储Minio及Hive\Spark\Flink的集成
  5. immutable.js 学习笔记(三)----- Map
  6. 1月3日内容总结——bbs项目登陆页面和主页、个人站点页的搭建
  7. Appium资源汇总
  8. HashMap简要介绍
  9. centos7 在线或离线安装python3
  10. Opengl ES之踩坑记