zoukankan      html  css  js  c++  java
  • word,excel,ppt转pdf

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


    }
  • 相关阅读:
    如何在获取celery中的任务执行情况
    python flask接口开发和入参的三种方式args、values、json
    python3 redis数据库写入方法和json格式的坑
    python3 封装好的时间戳转换函数,可直接使用
    python3 *args 、 **args 在函数定义和调用中的应用
    python中剔除字典重复项,可以使用集合(set)。
    python3 文件读写操作中的文件指针seek()使用
    jmeter数据库连接配置
    Python自动化培训第一周学习总结
    jmeter分布式压测
  • 原文地址:https://www.cnblogs.com/yuanqt/p/11227020.html
Copyright © 2011-2022 走看看