jacob实现word和xml的互转化

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ty497122758/article/details/8883460
使用准备:

         1、jacob-1.15-M3-x86.dll copyc:\\windows\system32

          2、引入jacob.jar

word转xml

         try {
               
                ActiveXComponent app = new ActiveXComponent( "Word.Application"); //啟動word
                String inFile = "E:\\test.docx"; //指定要打開的word文件
               
                app.setProperty("Visible", new Variant(true)); //為false時設置word不可見,為true時是可見要不然看不到Word打開文件的過程
                Dispatch docs = app.getProperty("Documents").toDispatch();
                //打開編輯囂
                Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] {inFile, new Variant(false), new Variant(true)} , new int[1]).toDispatch(); //打開word文檔
                Dispatch.call(doc, "SaveAs", "E:/myxml.xml", 11);//xml文件格式宏11
                Dispatch.call(doc, "Close", false);
                app.invoke("Quit",0);
           }catch (Exception e) {
              e.printStackTrace();

           }

xml转word

		//Word.Application代表COM OLE编程标识,可查询MSDN得到
		ActiveXComponent app = new ActiveXComponent("Word.Application");
		//设置Word不可见
		app.setProperty("Visible",false);
		//调用Application对象的Documents属性,获得Documents对象
		Dispatch docs = app.getProperty("Documents").toDispatch();
		Dispatch doc = Dispatch.call(docs,"Open","E:/c.xml",new Variant(false),new Variant(true)).getDispatch();

		Dispatch.call(doc,//要转换的文档
				     "SaveAS",
				     "E:/default.doc", //要保存的word文件名
				     12
				     );
		//关闭打开的Word文件
		Dispatch.call(doc,
		              "Close",
		               false//设置不保存改变
		               );
		//关闭Word应用程序
		app.invoke("Quit",0);
		System.out.println("完成!");



展开阅读全文

没有更多推荐了,返回首页