使用本地office安装包进行文件转换操作 ,word\excel\ppt转换为html

一、把jacob-1.19.jar放在lib

二、jacob-1.19-x64.dll放在jdk的bin、jre的bin(一共三个地方)

三、把JacobUtils.java放在工具类,直接调用就行。

JacobUtils.java  如下:

package com.fh.util;


import java.io.IOException;
import java.util.Scanner;

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

/**
 * 使用本地office安装包进行文件转换操作
 * @author Administrator
 *
 */
public class JacobUtils {
    private JacobUtils(){}
    
    /**
     * 将word转为HTML
     * @param filename
     * @param htmlFilename
     * @return
     */
    public static boolean wordToHtml(String filename, String htmlFilename) {
          ActiveXComponent xl = null;
          boolean rel = false;
          //打开一个word,不显示窗口      
          try {
               xl = new ActiveXComponent("Word.Application");
               Dispatch.put(xl, "Visible", new Variant(false));
               Object workbooks = xl.getProperty("Documents").toDispatch();
               Object workbook = Dispatch.call((Dispatch) workbooks, "Open",
                      filename).toDispatch();

               Dispatch.invoke((Dispatch) workbook, "SaveAs", Dispatch.Method,
                     new Object[] { htmlFilename, new Variant(8) }, new int[1]);

               Variant f = new Variant(false);
               //Close关闭文件,不关闭窗口
               Dispatch.call((Dispatch) workbooks, "Close", f);
               rel = true;
           } catch (Exception e) {
               e.printStackTrace();
           } finally {
               // 调用office关闭方法,关闭窗口和word进程         
               xl.invoke("Quit", new Variant[] {});                 
               xl = null;
           }
           return rel;
    }
    
    /**
     * 将excel转为HTML
     * @param filename
     * @param htmlFilename
     * @return
     */
    public static boolean excelToHtml(String filename, String htmlFilename) {
          ActiveXComponent xl = null;
          boolean rel = false;
          try {
               xl = new ActiveXComponent("Excel.Application");
               Dispatch.put(xl, "Visible", new Variant(false));
               //打开一个Excel,不显示窗口
               Object workbooks = xl.getProperty("workbooks").toDispatch();
               Object workbook = Dispatch.call((Dispatch) workbooks, "Open",
                      filename).toDispatch();

               Dispatch.invoke((Dispatch) workbook, "SaveAs", Dispatch.Method,
                     new Object[] { htmlFilename, new Variant(44) }, new int[1]);
               Dispatch.call((Dispatch) workbooks, "Close");
               rel = true;
           } catch (Exception e) {
               e.printStackTrace();
           } finally {
               xl.invoke("Quit", new Variant[] {});
               xl = null;
               Process process;
              int pid = 0;
              try {
                  process = Runtime.getRuntime().exec("tasklist");
                  Scanner in = new Scanner(process.getInputStream());
                  while (in.hasNextLine()) {
                      String p = in.nextLine();
                      // 打印所有进程
                      System.out.println(p);
                      if (p.contains("EXCEL.EXE")) {
                         StringBuffer buf = new StringBuffer();
                         for (int i = 0; i < p.length(); i++) {
                            char ch = p.charAt(i);
                            if (ch != ' ') {
                                buf.append(ch);
                             }
                         }
                         // 打印pid,根据pid关闭进程
                         System.out.println(buf.toString().split("Console")[0]
                                .substring("EXCEL.EXE".length()));
                         pid = Integer.parseInt(buf.toString().split("Console")[0]
                                .substring("EXCEL.EXE".length()));
                         Runtime.getRuntime().exec("tskill"+" "+pid);
                      }
                  }

               } catch (IOException e) {
                   rel = false;
                  e.printStackTrace();
               }
           }
          return rel;
    }
    
    /**
     * 将ppt转为HTML
     * @param filename
     * @param htmlFilename
     * @return
     */
    public static boolean pptToHtml(String filename, String htmlFilename) {
          ActiveXComponent xl = null;
          boolean rel = false;
          try {
               xl = new ActiveXComponent("Powerpoint.Application");
               Dispatch.put(xl, "Visible", new Variant(true));
               //打开一个PPT,显示窗口,PPT的不显示就会报错,狂晕!
               Object workbooks = xl.getProperty("Presentations").toDispatch();
               Object workbook = Dispatch.call((Dispatch) workbooks, "Open",
                      filename).toDispatch();
               Dispatch.invoke((Dispatch) workbook, "SaveAs", Dispatch.Method,
                     new Object[] { htmlFilename, new Variant(20) }, new int[1]);

               //Variant f = new Variant(false);
               //Dispatch.call((Dispatch) workbooks, "Close",f);
               //PPT的加这两行会报错,干脆注释上,反正在下面也关闭进程
               rel = true;
           } catch (Exception e) {
               e.printStackTrace();
           } finally {
               xl.invoke("Quit", new Variant[] {});
               xl = null;
               Process process;
               int pid = 0;
               try {
                  process = Runtime.getRuntime().exec("tasklist");
                  Scanner in = new Scanner(process.getInputStream());
                  while (in.hasNextLine()) {
                      String p = in.nextLine();
                      // 打印所有进程
                      System.out.println(p);
                     if (p.contains("POWERPNT.EXE")) {
                         StringBuffer buf = new StringBuffer();
                         for (int i = 0; i < p.length(); i++) {
                            char ch = p.charAt(i);
                            if (ch != ' ') {
                                buf.append(ch);
                             }
                         }
                         // 打印pid,根据pid关闭进程
                         System.out.println(buf.toString().split("Console")[0]
                                .substring("POWERPNT.EXE".length()));
                         pid = Integer
                                .parseInt(buf.toString().split("Console")[0]
                                       .substring("POWERPNT.EXE".length()));
                         Runtime.getRuntime().exec("tskill" + " " + pid);
                      }
                  }
               } catch (IOException e) {
                   rel = false;
                   e.printStackTrace();
               }
           }
          return rel;
     }
}
 

四、调用:

@RequestMapping(value="/tohtml")
    public ModelAndView tohtml()throws Exception{
        ModelAndView mv = this.getModelAndView();
        String filename="D:\\11.doc";
        String htmlFilename="D:\\test.html";
        JacobUtils.wordToHtml(filename, htmlFilename);
        return mv;
    }

 

五、三个附件:https://download.csdn.net/download/weixin_38948287/10760100

链接: https://pan.baidu.com/s/10AWA7olu2ar-sY-Z5AQhag

提取码: jnqs 复制这段内容后打开百度网盘手机App,操作更方便哦

Office2003通用兼容包(office2003兼容office2007、office2010)在装有Microsoft Office 2000、Office XP或Office 2003的计算机上安装该兼容包之后,就可以采用WordExcel 和 PowerPoint 2007/2010新增的文件格式打开、编辑和保存文件。该兼容包还可以与Microsoft Office Word Viewer 2003、Excel Viewer 2003和PowerPoint Viewer 2003配合使用,用来查看以这些新格式保存的文件。有关该兼容包的更多信息,请参阅知识库文章923505。 Microsoft Office XP 和 2003 系统 WordExcel 或 PowerPoint 程序的用户 — 首先从 Microsoft Update 安装所有高优先级的更新,然后下载兼容包。 注意:如果您使用 Microsoft Word 2000 或 Microsoft Word 2002 阅读或编写包含复杂文种的文档,了解如何在您的 Word 版本中正确显示 Word 2007/2010文档。 管理员:可以下载该兼容包中包含的WordExcel 和 PowerPoint转换器管理模板。 Office2003通用兼容包(office2007兼容包,office2010兼容包)支持的操作系统: Windows 2000 Service Pack 4; Windows Server 2003; Windows XP Service Pack 1 Office2003通用兼容包推荐的 Microsoft Office 程序: Microsoft Word 2000 Service Pack 3、Microsoft Excel 2000 Service Pack 3 和 Microsoft PowerPoint 2000 Service Pack 3 Microsoft Word 2002 Service Pack 3、Microsoft Excel 2002 Service Pack 3 和 Microsoft PowerPoint 2002 Service Pack 3 Microsoft Office Word 2003 Service Pack 1 或更高版本、Microsoft Office Excel 2003 Service Pack 1 或更高版本、Microsoft Office PowerPoint 2003 Service Pack 1 或更高版本 Microsoft Office Word Viewer 2003 Microsoft Office Excel Viewer 2003 Microsoft Office PowerPoint Viewer 2003
注册说明: name:any key:75011162191391210808 All Office Converter 是一款易于使用和专业的文件转换工具。它可以高质量的支持批量转换文件,网页和图像,以改善工作效率。有了这个强大的转换器,您可以创建PDF文件的通用格式,并转换PDF文件到其他通用格式,超级输出质量和效益。更多信息,您可以转换不同的Office文档格式,网页,图片。它可以支持全面的格式: Word(doc, docm, docx), Excel(xls .xlsx.xlsm),PowerPoint(ppt, pptc,pptm),PDF,XLS,RTF,TXT,HTM/HTML,Website,JPG,BMP,GIF,TIF,WMF,EMF,TGA, RLE,PNG etc.   特征   *提供个性化服务的命令行服务器/开发。   *它可以同时转换不同的格式,以一个特定格式一次。   *它可以转换的网页上的网站或您的计算机。   *易于使用转换的功能。   *更多的设置选项,让您控制输出文件更准确。   *保存导入的文件清单。   *支持加密的PDF文件。   *创建PDF和图像质量高。   *打开*. HTM或网址如下框架转换。   支持格式:   批量转换Word (doc,docm,docx) Excel (xls.xlsx.xlsm), PowerPoint (ppt,pptc,pptm), RTF, TXT, HTM, HTML, Website, JPG, BMP, GIF, TIF, WMF, EMF to PDF (as default format, as image format, as text format)   批量转换: PDF to DOC, RTF, XLS, HTM, TXT, JPG, BMP, GIF, TIF, TGA, RLE, PNG, EMF, WMF   批量转换Word (doc,docm,docx) to PDF (as default format, as image format, as text format), XLS, TXT, HTM, JPG, BMP, GIF, TIF, TGA, RLE, PNG, EMF, WMF.   批量转换: PDF, Excel (xls.xlsx.xlsm), TXT, HTM, HTML, JPG, BMP, GIF, TIF, EMF, WMF to DOC.   批量转换: PowerPoint (ppt,pptc,pptm) to JPG, BMP, GIF, TIF, TGA, RLE, PNG, EMF, WMF, DOC,   XLS, RTF, TXT, PDF (as default format, as image format, as text format).   批量转换: HTM, HTML, Website to DOC, PDF (as default format, as image format, as text   format), TXT, RTF, XLS, JPG, BMP, GIF, TIF, TGA, RLE, PNG, EMF, WMF.   注:此软件官方内置了对简体中文、繁体中文的支持,安装后在语言(Language)选项那里选择简体中文(Simplified Chinese或者Chinese GB)!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值