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();  
              }    
        }
       

猜你喜欢

转载自blog.csdn.net/builderwfy/article/details/78582511
今日推荐