关于jacob用法,百度一下就会发现几乎都是复制2004年一个代码,那段代码实现的是从一个目录读取所有doc文件,然后把它转html格式。 为了便习学习和使用,我把代码看懂后精简了一下,得出不少新结论,拿出来和大家分享。
2、一个具体的代码示例:
 package ccnu; 
import com.jacob.com.*; 
import com.jacob.activeX.*; 
import java.io.*; 
 
public class testCoding 

    /*
     * 作者:郭喜跃/【捂汗县长】
     * 时间:2013-7-20
     * 程序功能:调用jacob包,在Microsoft Office 能够支持打开的文件类型中随意进行格式转换(本程序不是批量转换,一次只能转单个文件)。
     * 由于我电脑上安装的是Office 2013,所以甚至可以实现pdf与txt!用起来很方便,除了注释 代码不算长吧?
     * 
     * */ 
    public static void main(String[] args) 
    { 
        //指定被转换文件的完整路径。 我这里的意图是把pdf转为txt  
        String path = new String("E:\\Jena\\Jena初体验0.pdf"); 
        //根据路径创建文件对象  
        File docFile=new File(path); 
        //获取文件名(包含扩展名)  
        String filename=docFile.getName(); 
        //过滤掉文件名中的扩展名  
        int filenamelength=filename.length(); 
        int dotposition=filename.indexOf("."); 
        filename=filename.substring(0,dotposition); 
         
        //设置输出路径,一定要包含输出文件名(不含输出文件的扩展名)  
        String savepath = new String ("E:\\Jena\\txt\\"+filename);   
         
        //启动Word程序  
        ActiveXComponent app = new ActiveXComponent("Word.Application");         
        //接收输入文件和输出文件的路径  
        String inFile = path; 
        String tpFile = savepath; 
        //设置word不可见  
        app.setProperty("Visible", new Variant(false)); 
        //这句不懂  
        Object docs = app.getProperty("Documents").toDispatch(); 
        //打开输入的doc文档  
        Object doc = Dispatch.invoke((Dispatch) docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch(); 
         
        //另存文件, 其中Variant(n)参数指定另存为的文件类型,详见代码结束后的文字  
        Dispatch.invoke((Dispatch) doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(2)}, new int[1]); 
        //这句也不懂  
        Variant f = new Variant(false); 
        //关闭并退出  
        Dispatch.call((Dispatch) doc, "Close", f); 
        app.invoke("Quit", new Variant[] {}); 
        System.out.println("转换完毕。"); 
    } 
 

 
package ccnu;
import com.jacob.com.*;
import com.jacob.activeX.*;
import java.io.*;
 
public class testCoding
{
/*
* 作者:郭喜跃/【捂汗县长】
* 时间:2013-7-20
* 程序功能:调用jacob包,在Microsoft Office 能够支持打开的文件类型中随意进行格式转换(本程序不是批量转换,一次只能转单个文件)。
* 由于我电脑上安装的是Office 2013,所以甚至可以实现pdf与txt!用起来很方便,除了注释 代码不算长吧?
*
* */
public static void main(String[] args)
{
//指定被转换文件的完整路径。 我这里的意图是把pdf转为txt
String path = new String("E:\\Jena\\Jena初体验0.pdf");
//根据路径创建文件对象
File docFile=new File(path);
//获取文件名(包含扩展名)
String filename=docFile.getName();
//过滤掉文件名中的扩展名
int filenamelength=filename.length();
int dotposition=filename.indexOf(".");
filename=filename.substring(0,dotposition);
 
//设置输出路径,一定要包含输出文件名(不含输出文件的扩展名)
String savepath = new String ("E:\\Jena\\txt\\"+filename);
 
//启动Word程序
ActiveXComponent app = new ActiveXComponent("Word.Application");
//接收输入文件和输出文件的路径
String inFile = path;
String tpFile = savepath;
//设置word不可见
app.setProperty("Visible", new Variant(false));
//这句不懂
Object docs = app.getProperty("Documents").toDispatch();
//打开输入的doc文档
Object doc = Dispatch.invoke((Dispatch) docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();
 
//另存文件, 其中Variant(n)参数指定另存为的文件类型,详见代码结束后的文字
Dispatch.invoke((Dispatch) doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(2)}, new int[1]);
//这句也不懂
Variant f = new Variant(false);
//关闭并退出
Dispatch.call((Dispatch) doc, "Close", f);
app.invoke("Quit", new Variant[] {});
System.out.println("转换完毕。");
}
 
}
 
 
 
        *其中第44行中的 invoke()函数中的Variant(n)参数指定另存为的文件类型(n的取值范围是0-25),他们分别是:
        *Variant(0):doc
        *Variant(1):dot
        *Variant(2-5),Variant(7):txt
        *Variant(6):rft
        *Variant(8),Variant(10):htm
        *Variant(9):mht
        *Variant(11),Variant(19-22):xml
        *Variant(12):docx
        *Variant(13):docm
        *Variant(14):dotx
        *Variant(15):dotm
        *Variant(16)、Variant(24):docx
        *Variant(17):pdf
        *Variant(18):xps
        *Variant(23):odt
        *Variant(25):与Office2003与2007的转换程序相关,执行本程序后弹出一个警告框说是需要更高版本的 Microsoft Works Converter
        *由于我计算机上没有安装这个转换器,所以不清楚此参数代表什么格式
        */

jacob 实现Office Word文件格式转换的更多相关文章

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

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

  2. java使用jacob将office转pdf

    1.此处代码是把office文档转换成pdf的工具类,在BS架构的产品中,我们可以使用基于JS的pdf插件显示pdf文档,但是前提IE需要按照adobe的pdf软件,对于非IE不用安装.2.可以基于f ...

  3. C#下搭建文件格式转换服务器

    文件格式转换,相信很多涉及到office文档在线观看的都会需要,因为浏览器还不能完全支持直接打开office文档,所以很多情况下我们都需要将这些文档转换成flash能够播放的格式,但是另一个问题又来了 ...

  4. C#操作Office.word(三)

    前面两篇博客讲解了怎么通过程序控制word的生成,包括生成文字.添加表格.添加图片等.这篇博客主要说一下怎么把word图片转换成pdf. using System; using System.Coll ...

  5. C#操作Office.word(二)

    在上一篇文章"C#操作Office.word(一)"中我们讲述了如何使用VS2010引用COM中Miscrosoft Word 14.0 Object Library实现创建文档, ...

  6. Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享

    Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享 在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑&qu ...

  7. 如何将WORD表格转换成EXCEL表格

    WORD和EXCEL都可以制作表格,但WORD表格与EXCEL表格之间有着很明显的差距,所以在办公中经常会需要将WORD转换成EXCEL,今天小编就教大家一招将WORD表格转换成EXCEL表格. 操作 ...

  8. Win2008服务启动不能调用Office Word的解决方法

    本文为大家分享一下如何解决Windows Server 2008 服务启动不能调用Office Word的问题,分享这个教程的原因是,今天在Windows server2008上部署一个应用时发现了一 ...

  9. 数据分析:基于Python的自定义文件格式转换系统

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

随机推荐

  1. Gradle学习系列之五——自定义Property

    在本系列的上篇文章中,我们讲到了增量式构建,在本篇文章中,我们将讲到如何自定义Project的Property. 请通过以下方式下载本系列文章的Github示例代码: git clone https: ...

  2. 实现虚拟模式的动态数据加载Windows窗体DataGridView控件 .net 4.5 (一)

    实现虚拟模式的即时数据加载Windows窗体DataGridView控件 .net 4.5 原文地址 :http://msdn.microsoft.com/en-us/library/ms171624 ...

  3. C# winform调用浏览器打开页面方法分享,希望对大家有帮助

    在很多客户端程序中我们都需要调用浏览器打开网页,这里分享一个可以在我winform程序调用浏览器的方法,测试通过了. 声明:这个方法是上万个用户测试通过的,不是我没有测试通过就拿出来分享,那个是自己搬 ...

  4. html 隐藏滚动条

    1. html 标签加属性 <html lang="en" class="no-ie" style="overflow:hidden;" ...

  5. java ftp

    FTPUtil import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import jav ...

  6. Autofac全面解析系列(版本:3.5) – [使用篇(推荐篇):2.解析获取]

    前言 Autofac是一套高效的依赖注入框架. Autofac官方网站:http://autofac.org/ Autofac在Github上的开源项目:https://github.com/auto ...

  7. JavaScript调Java

    1.映射Java对象到JavaScript对象上 MainActivity.java package com.example.jsdemo; import android.os.Bundle; imp ...

  8. 使django与数据库保持长连接

    最近遇到一个很蛋疼的问题,写了一个后台管理系统, 由于是后台管理系统,所以使用频率不是很高,当django程序在闲置一段时间后,再次打开后台系统,就变得很慢,然后又好了.查了很多方面,从模板引擎到请求 ...

  9. CKEditor与CKFinder的配置

    CKEditor与CKFinder的配置使用(一) 将CKEditor 与 CKFinder 的包含在项目中 从http://cksource.com网站上下载CKEditor与CKFinder,并将 ...

  10. 关于ApplicationPoolIdentity

    一直以来IIS中的网站默认都是以network service在运行,但是从IIS7开始,默认会以ApplicationPoolIdentity运行. 这个账户比较特殊,是一种虚拟帐户,你无法在计算机 ...