如下是一个测试操做word的实例:仅供参考java
import com.jacob.activeX.ActiveXComponent; import com.jacob.com.ComException; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class JacobTest { // 声明一个word对象 private ActiveXComponent objWord; // 声明四个word组件 private Dispatch custDocprops; private Dispatch builtInDocProps; private Dispatch document; private Dispatch wordObject; public JacobTest() { } /** * 打开word文挡 */ public void open(String filename) { // 建立一个word对象 objWord = new ActiveXComponent("Word.Application"); // 为wordobject组件附值 wordObject = (Dispatch) (objWord.getObject()); // 改了这里 // 生成一个只读方式的word文挡组件 Dispatch.put(wordObject, "Visible", new Variant(false)); // 获取文挡属性 Dispatch documents = objWord.getProperty("Documents").toDispatch(); // 打开激活文挡 document = Dispatch.call(documents, "Open", filename).toDispatch(); } public void selectCustomDocumentProperitiesMode() { custDocprops = Dispatch.get(document, "CustomDocumentProperties").toDispatch(); } public void selectBuiltinPropertiesMode() { builtInDocProps = Dispatch.get(document, "BuiltInDocumentProperties").toDispatch(); } /** * 关闭文挡 */ public void close() { Dispatch.call(document, "Close"); } public String getCustomProperty(String cusPropName) { try { cusPropName = Dispatch.call((Dispatch)custDocprops, "Item",cusPropName).toString(); } catch (ComException e) { cusPropName = null; } return cusPropName; } public String getBuiltInProperty(String builtInPropName) { try { builtInPropName = Dispatch.call((Dispatch)builtInDocProps,"Item", builtInPropName).toString(); } catch (ComException e) { builtInPropName = null; } return builtInPropName; } public static void main(String[] args) { try { JacobTest jacTest = new JacobTest(); jacTest.open("c:/s.doc"); jacTest.selectCustomDocumentProperitiesMode(); jacTest.selectBuiltinPropertiesMode(); String custValue = jacTest.getCustomProperty("Information Source"); String builtInValue = jacTest.getBuiltInProperty("Author"); jacTest.close(); System.out.println("Document Val One:" + custValue); System.out.println("Document Author:" + builtInValue); } catch (Exception e) { System.out.println(e); } } }
jacob1.1.5-M4.zip下载地址:http://115.com/file/beeqxg90#jacob-1.15-M4.zip(附带一个操做word示例代码和一个操做文档)c#
结束语: 第一次在开源中国发文,若有错误之处,你们能够尽情留言进行探讨。
服务器