关于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
        *由于我计算机上没有安装这个转换器,所以不清楚此参数代表什么格式
        */

最新文章

  1. CSS 3学习——边框
  2. Linux下 JDK安装
  3. 多线程JAVA篇(一)
  4. Windows环境中Openfire与Spark安装与配置指南
  5. HDU--杭电--1195--Open the Lock--深搜
  6. 浏览器插件 - Chrome 对 UserScript 的声明头(metadata)兼容性一览
  7. 未完待续的JAVA基础知识
  8. 2016031901 - U盘安装ubuntu系统
  9. PHPExcell单元格中某些时间格式的内容不能正确获得的处理办法
  10. poj--1517
  11. PHP和JS在循环、条件判断中的不同之处
  12. MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client
  13. learning makefile static model
  14. ASP.NET MVC用户登录(Memcache存储用户登录信息)
  15. sqlplus 执行 sql 文件
  16. 【iCore4 双核心板_FPGA】例程十一:FSMC总线通信实验——独立地址模式
  17. https方式下 git push 每次都要输入密码的解决办法
  18. jq 按回车键 登陆
  19. C++11_ Lambda
  20. 【二分】【三分】【计算几何】XIII Open Championship of Y.Kupala Grodno SU Grodno, Saturday, April 29, 2017 Problem L. Lines and Polygon

热门文章

  1. sql date()函数
  2. C#开发的高性能EXCEL导入、导出工具DataPie(支持MSSQL、ORACLE、ACCESS,附源码下载地址)[转]
  3. 【iOS】FMDB封装,查询自动mapping
  4. CSS行高line-height的理解
  5. 【转】Aspose.Cells读取excel文件
  6. C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
  7. asp.net.mvc 中form表单提交控制器的2种方法和控制器接收页面提交数据的4种方法
  8. T-SQL的回车和换行符(SQL)
  9. 干净的停止tomcat/java应用程序
  10. winform(数据导出、TreeView的使用)