您的位置:首页 > 运维架构

jacob 实现Office Word文件格式转换 docTotxt,docTopdf,docxTodoc,pdfTotxt ...

2015-05-04 16:44 1171 查看
关于jacob用法,百度一下就会发现几乎都是复制2004年一个代码,那段代码实现的是从一个目录读取所有doc文件,然后把它转html格式。 为了便习学习和使用,我把代码看懂后精简了一下,得出不少新结论,拿出来和大家分享。
 
1、jacob的安装配置
 



 
 
 
与处理其他jar文件一样,将1号文件加载到classpath中(由于我计划把它应用到Web中,所以我它复制到WebRoot\WEB-INF\lib中,但是实践证明,这个jar文件最好不要放到这里,不然很容易出现“jacob x86.dll already loaded in another classloader”的问题,后来参考了别人的意见,把它剪切到tomcat\lib下 ,就解决这个问题了,别忘了在Eclipse引用这个jar包!)
 
 
如果是你的系统是32位的,再将2号文件(即第2个dll文件)复制到%JAVA_HOME%\jdk1.7.0_04\jre\bin下; 当然,如果你是64位系统,要复制第1个dll文件
 
 
2、一个具体的代码示例:
 
 
[java]
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
        *由于我计算机上没有安装这个转换器,所以不清楚此参数代表什么格式
        */
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐