java 中通过调用jacob1.18 word转xml文件

1.在pom.xml文件中新增配置

<dependency>
<groupId>com.hynnet</groupId>
        <artifactId>jacob</artifactId>
        <version>1.18</version>
     </dependency>

2.网上下载 jacob-1.18.zip dll文件

3.将jacob-1.18-x86.dll文件复制到 C:\WINDOWS\system32目录下

4.将 jacob-1.18-x86.dll文件复制到C:\Program Files\Java\jdk1.8.0_45\jre\bin

      3,4二步如果是64位系统 请将jacob-1.18-x64.dll文件复制到相应目录。

5.代码word转xml

  /**
         * word转xml
         * @author  wfy 2017年10月27日16:15:11
         * @Description: 
         * @param filePath word目录
         * @param xmlFilePath 生成xml存放路径
         * 
         */
        public static void wordToXml(String wordfile,String xmlfile){
        ComThread.InitSTA(); 
        ActiveXComponent app = new ActiveXComponent( "Word.Application"); //启动word  
            try {  
                   app.setProperty("Visible", new Variant(false)); //为false时设置word不可见,为true时是可见要不然看不到Word打打开文件的过程  
                   Dispatch docs = app.getProperty("Documents").toDispatch();  
                   //打开编辑器
                   Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] {wordfile, new Variant(false), new Variant(true)} , new int[1]).toDispatch(); //打开word文档
                   Dispatch.call(doc, "SaveAs", xmlfile, WORD_XML);//xml文件格式宏11  
                   Dispatch.call(doc, "Close", false);  
                   app.invoke("Quit",0); 
                   System.out.println("---------word转换完成--------");
              }catch (Exception e) {  
                 e.printStackTrace();  
              } finally {  
                  app.invoke("Quit", new Variant[] {});  
                  ComThread.Release();  
              }    
        }
       

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值