配置环境

下载jacob【下载链接】

解压jacob,将jacob-1.18-x64.dll放在C:\Windows\System32下面

转换代码

package com.lx.convert.service;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;import java.io.*;
import java.util.Date;/*** @ProjectName: convert* @Package: com.lx.convert.service* @ClassName: SingletonConvert* @Author: 柳絮飞祭奠* @Description: 柳絮飞祭奠* @Date: 2019/12/12 21:00* @Version: 1.0*/
public class SingletonConvert {private static Logger logger = LoggerFactory.getLogger(SingletonConvert.class);private static ActiveXComponent app;private static ActiveXComponent PPT;private static ActiveXComponent EXECL;public static final int EXCEL_HTML = 44;/*** 单例模式* 实例化word*/public static synchronized  ActiveXComponent getWordInstance(){if (app == null) {app =  new ActiveXComponent("KWPS.Application");app.setProperty("Visible", false);}return app;}public static synchronized ActiveXComponent getPPtInstance(){if (PPT == null) {PPT =  new ActiveXComponent("KWPP.Application");}return PPT;}public static synchronized ActiveXComponent getexeclInstance(){if (EXECL == null) {EXECL =  new ActiveXComponent("Excel.Application");}return EXECL;}/*** 转换word*/public static void wordConvertSingleton(String docPath, String fileName) {try {app = getWordInstance();Dispatch documents = app.getProperty("Documents").toDispatch();Dispatch doc = Dispatch.call(documents, "Open", docPath).toDispatch();Dispatch.invoke(doc, "SaveAs", Dispatch.Method,new Object[] { fileName, new Variant(8) }, new int[1]);Dispatch.call(doc, "Close", new Variant(0));//没有调用Quit命令doc = null;} catch (Exception e) {e.printStackTrace();}}public int word2PDF(String srcFilePath, String pdfFilePath) {
//        ActiveXComponent app = null;Dispatch doc = null;try {
//            ComThread.InitSTA();//打开Word应用程序
//            app = new ActiveXComponent("Word.Application"); //office
//            app = new ActiveXComponent("KWPS.Application");  //wpsapp=getWordInstance();//TODOlogger.debug("开始转化Word为PDF...");long date = System.currentTimeMillis();// 设置Word不可见// 获得Word中所有打开的文档,返回documents对象Dispatch docs = app.getProperty("Documents").toDispatch();Object[] obj = new Object[]{srcFilePath,//是否只读new Variant(true),new Variant(true),new Variant(false),new Variant("pwd")};
//            Object[] obj = new Object[]{
//                    srcFilePath,
//                    //是否只读
//                    new Variant(8)
//            };// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Documentdoc = Dispatch.invoke(docs, "Open", Dispatch.Method, obj, new int[1]).toDispatch();//兼容性检查,为特定值false不正确// Dispatch.put(doc, "Compatibility", false);Dispatch.put(doc, "RemovePersonalInformation", false);// word保存为pdf格式宏,值为17Dispatch.call(doc, "ExportAsFixedFormat", pdfFilePath, FileConstants.WORD_TO_PDF_OPERAND);//TODOlogger.debug("doc: " + doc);long date2 = System.currentTimeMillis();int time = (int) ((date2 - date) / 1000);logger.debug("用时:" + time);return time;} catch (Exception e) {e.printStackTrace();// throw e;logger.debug("Exception" + e);return -1;} finally {if (doc != null) {// 关闭文档Dispatch.call(doc, "Close", false);doc=null;}
//            if (app != null) {
//                // 关闭Word应用程序
//                app.invoke("Quit", 0);
//            }
//            ComThread.Release();}}public static int ppt2pdf(String srcFilePath, String pdfFilePath) {
//        ActiveXComponent app = null;Dispatch ppt = null;try {
//            ComThread.InitSTA();// app = new ActiveXComponent("PowerPoint.Application"); //Office
//            app = new ActiveXComponent("KWPP.Application");  //WPSPPT =  getPPtInstance();//TODOlogger.debug("开始转化PPT为PDF...");long date = System.currentTimeMillis();Dispatch ppts = PPT.getProperty("Presentations").toDispatch();ppt = Dispatch.call(ppts, "Open", srcFilePath,// ReadOnlytrue,// Untitled指定文件是否有标题true,// WithWindow指定文件是否可见false).toDispatch();// ppSaveAsPDF为特定值32// Dispatch.call(ppt, "SaveAs", pdfFilePath, PPT_TO_PDF_OPERAND);Object[] obj = new Object[]{pdfFilePath,new Variant(FileConstants.PPT_TO_PDF_OPERAND)};Dispatch.invoke(ppt, "SaveAs", Dispatch.Method, obj, new int[1]);long date2 = System.currentTimeMillis();int time = (int) ((date2 - date) / 1000);logger.debug("用时:" + time);return time;} catch (Exception e) {e.printStackTrace();// throw e;logger.debug("Exception" + e);return -1;} finally {if (ppt != null) {Dispatch.call(ppt, "Close");ppt=null;}
//            if (app != null) {
//                app.invoke("Quit");
//            }
//            ComThread.Release();}}public int excelToHtml(String xlsfile, String htmlfile) {long date = new Date().getTime();EXECL = getexeclInstance(); // 启动Excel
//      ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动ExcelDispatch excel=null;try {EXECL.setProperty("Visible", new Variant(false));Dispatch excels = EXECL.getProperty("Workbooks").toDispatch();excel = Dispatch.invoke(excels,"Open",Dispatch.Method,new Object[] { xlsfile, new Variant(false),new Variant(true) },new int[1]).toDispatch();
//          Dispatch sheet = Dispatch.invoke(excel, "sheet(0)", arg2, arg3, arg4)Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {htmlfile, new Variant(EXCEL_HTML) }, new int[1]);Variant f = new Variant(false);Dispatch.call(excel, "Close", f);excel=null;} catch (Exception e) {e.printStackTrace();} finally {if (excel != null) {Dispatch.call(excel, "Close");excel=null;}
//          app.invoke("Quit", new Variant[] {});}long date2 = new Date().getTime();int time = (int) ((date2 - date) / 1000);System.out.println(time+"s");return time;}public int txtToHtml(String xlsfile, String htmlfile) {try {InputStreamReader nInputStreamReader = new InputStreamReader(new FileInputStream(new File(xlsfile)), "GBK");@SuppressWarnings("resource")BufferedReader nBufferedReader = new BufferedReader(nInputStreamReader);String nHtmlPath =htmlfile;File nFile=new File(nHtmlPath);if(nFile.exists()){nFile.createNewFile();}FileOutputStream nFileOutputStream = new FileOutputStream(nFile);OutputStreamWriter nOutputStreamWriter = new OutputStreamWriter(nFileOutputStream, "UTF-8");BufferedWriter nBufferedWriter = new BufferedWriter(nOutputStreamWriter);String lineTxt = null;while ((lineTxt = nBufferedReader.readLine()) != null) {nBufferedWriter.write(lineTxt + "</br>");}nBufferedWriter.close();nOutputStreamWriter.close();nOutputStreamWriter.close();nFileOutputStream.close();
//
//          org.jsoup.nodes.Document nJsoupDocument = Jsoup.parse(new File(nHtmlPath), "UTF-8");
//          if(nFile.exists() && nFile.isFile()){
//              nFile.delete();
//          }} catch (Exception e) {e.printStackTrace();}return 0;}public static void main(String[] args) {SingletonConvert SingletonConvert=new SingletonConvert();for (int i=1;i<=11;i++){SingletonConvert.excelToHtml("D:\\iWork\\execl\\"+i+".xlsx","D:\\iWork\\execl\\"+i+".html");
//            SingletonConvert.wordConvertSingleton("D:\\iWork\\file.cache\\"+i+".doc","D:\\iWork\\file.cache\\"+i+".html");}}
}

jacob实现文档转换相关推荐

  1. java将WORD文档转换成pdf文件

    总结对jacob和Itext学习总结.本文试验的是将WORD转换成PDF文件. 实现思路 一.先将WORD文档转换成HMTL文件格式(参阅我的前一文<JAVA操作WORD文档). 二.用流读取H ...

  2. Office文档转换成PDF文档

    本方案是我在网络上收集前人智慧,以及辛苦奋斗探索几周完成的成果. 本文从一个只会Java零基础的开发者的角度,全面.琐碎的进行总结. 具体方案: 方案一.使用OpenOffice.org的系统服务+j ...

  3. 文件转换html文件失败,pandoc文档转换失败,出现错误67

    我,使用R 3.3.2和Rstudio版本1.0.44,这是我RMarkdown文件pandoc文档转换失败,出现错误67 --- title: "Sentiment Analysis&qu ...

  4. 智能媒体管理产品文档转换/预览功能介绍(4)--快速搭建

    摘要: 智能媒体管理提供了 Cloud Native 架构的文档转换/预览服务,本文介绍快速搭建的示例,让您用 DIY 的体验方式实现文档预览功能. 一.导语 智能媒体管理 提供了 Cloud Nat ...

  5. 如何将CSDN文档转换成IPYNB格式的文档?

    简 介: ※IPYNB格式的文档可以很方便在Jupyter Notebook环境内方便研究人员对于算法.数据进行操作.Markdown文本则便于形成各种网络文档.本文介绍了利用notedown实现将C ...

  6. 如何利用 nbconvert将 IPYNB文档转换 Markdown文档?

    简 介: IPYNB文件是现在基于IPython开发工程人员记录和分析处理算法和数据记录文档.将该文档转换成其它格式可以方便面进行思想共享,工作汇报等.利用 nbconvert可以很方便将ipynb的 ...

  7. xml 文本转json java_java将XML文档转换成json格式数据

    本篇文章主要介绍了java将XML文档转换成json格式数据的示例,希望能帮助到大家. 功能 将xml文档转换成json格式数据 说明 依赖包: 1. jdom-2.0.2.jar : xml解析工具 ...

  8. openoffice转换过程中遇到繁体字文档转换失败的问题

    今天发现上线的文档转换功能中存在一个文档转换不成功,查看后台日志标志文档无法加载成功,提示日志如下: INFO: connected Jul 08, 2015 2:50:33 PM com.artof ...

  9. 文档转换乱码异常解决:unoconv openoffice libreoffice

    文档转换乱码异常解决:unoconv openoffice libreoffice Windows上拷贝相应字体 将Windows下的Fonts,如:C:\Windows\Fonts,压缩成Fonts ...

最新文章

  1. python协程池操作mysql_在python中使用aiomysql异步操作mysql
  2. 分布式领域架构师要掌握的技术
  3. [转载] Python基础:用Python统计列表中每个单词出现的次数(split 的使用,for双重循环)
  4. Delphi编程 -- 如何实现一个支持Visual Basic的For Each调用的COM对象
  5. php 类中输出所有属性,PHP基于反射获取一个类中所有属性
  6. 决策树之CART 算法
  7. 自由软件运动与GNU项目
  8. c语言 16 16 字库,16位汉字显示屏的字库软件(点阵字库)
  9. Win11控制面板里没有高清音频管理器怎么办?
  10. JDK类库创建一张简单图片
  11. python Requests+正则表达式爬取猫眼电影top100
  12. 高考数学考用计算机求函数吗,高考数学52种快速做题方法整理!再也不用担心考试时间不够了...
  13. 电子书各种格式的转换常见问题
  14. 工行华虹U盾在Vista和IE7下的解决方案
  15. FCKEditor 使用
  16. Linux 有/无设备树下 platform_driver 驱动框架
  17. json字符串,JSONObject对象,JavaBean对象互转。
  18. Dos脚本调用打印机用户界面及配置
  19. Nginx直接访问redis,lua访问redis
  20. Uber Shader

热门文章

  1. Android入门第60天-MVVM中的Databinding与ListView结合使用
  2. 【摄影】湖州安吉梅花沟
  3. Unity Shader 贴花效果(一)
  4. android 检验助手,英腾检验助手APP
  5. mysql创建唯一性降序索引_MySQL 8 新特性之降序索引底层实现
  6. 使用IOS快捷指令打开任意支付宝小程序
  7. ASP.NET 母版页,内容页之间如何传参数
  8. 本地使用element ui 图标不显示或显示错乱的问题
  9. 如何在excel表格中批量添加内容?
  10. ICP网站备案查询接口