• jacob 实现Office Word文件格式转换


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

  • 相关阅读:
    [HNOI2002]营业额统计
    HDU 1374
    HDU 3345
    HDU 2089
    Graham扫描法
    Codeforces 1144D Deduction Queries 并查集
    Codeforces 916E Jamie and Tree 线段树
    Codeforces 1167F Scalar Queries 树状数组
    Codeforces 1167E Range Deleting
    Codeforces 749E Inversions After Shuffle 树状数组 + 数学期望
  • 原文地址:https://www.cnblogs.com/tomcattd/p/3544537.html
Copyright © 2020-2023  润新知