教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 高等教育 >

java - jacob - 操作word - 文档,进行写操作(5)

来源:网络收集 时间:2026-07-17
导读: Dispatch documents = Dispatch.get(MsWordApp, \ // Call the Add method of the Documents collection to create // a new document to edit document = Dispatch.call(documents, \ } // 打开一个存在的word文档

Dispatch documents = Dispatch.get(MsWordApp, \ // Call the Add method of the Documents collection to create // a new document to edit

document = Dispatch.call(documents, \ }

// 打开一个存在的word文档,并用document 引用 引用它 public void openFile(String wordFilePath) {

// Find the Documents collection object maintained by Word

// documents表示word的所有文档窗口,(word是多文档应用程序) Dispatch documents = Dispatch.get(MsWordApp, \ document = Dispatch.call(documents, \ new Variant(true)/* 是否进行转换ConfirmConversions */, new Variant(false)/* 是否只读 */).toDispatch();

// document = Dispatch.invoke(documents, \ // new Object[] { wordFilePath, new Variant(true), // new Variant(false) // }, new int[1]).toDispatch(); }

// 向 document 中插入文本内容

public void insertText(String textToInsert) {

// Get the current selection within Word at the moment. // a new document has just been created then this will be at

// the top of the new doc 获得选 中的内容,如果是一个新创建的文件,因里面无内容,则光标应处于文件开头处

Dispatch selection = Dispatch.get(MsWordApp, \ // 取消选中,应该就是移动光标 ,否则 新添加的内容会覆盖选中的内容 Dispatch.call(selection, \ // Put the specified text at the insertion point Dispatch.put(selection, \

// 取消选中,应该就是移动光标

Dispatch.call(selection, \ }

// 向文档中添加 一个图片,

public void insertJpeg(String jpegFilePath) {

Dispatch selection = Dispatch.get(MsWordApp, \ Dispatch image = Dispatch.get(selection, \

Dispatch.call(image, \ }

// 段落的处理,插入格式化的文本 public void insertFormatStr(String text) {

Dispatch wordContent = Dispatch.get(document, \取得word文件的内容

Dispatch.call(wordContent, \插入一个段落到最后

Dispatch paragraphs = Dispatch.get(wordContent, \ .toDispatch(); // 所有段落

int paragraphCount = Dispatch.get(paragraphs, \ Variant.VariantInt).getInt();// 一共的段落数

// 找到刚输入的段落,设置格式

Dispatch lastParagraph = Dispatch.call(paragraphs, \

new Variant(paragraphCount)).toDispatch(); // 最后一段(也就是刚插入的) // Range 对象表示文档中的一个连续范围,由一个起始字符位置和一个终止字符位置定义

Dispatch lastParagraphRange = Dispatch.get(lastParagraph, \ .toDispatch();

Dispatch font = Dispatch.get(lastParagraphRange, \ Dispatch.put(font, \设置为黑体 Dispatch.put(font, \设置为斜体 Dispatch.put(font, \宋体\ Dispatch.put(font, \小四

Dispatch selection = Dispatch.get(MsWordApp, \ Dispatch.call(selection, \插入一个空行 Dispatch alignment = Dispatch.get(selection, \ .toDispatch();// 段落格式 …… 此处隐藏:290字,全部文档内容请下载后查看。喜欢就下载吧 ……

java - jacob - 操作word - 文档,进行写操作(5).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/607438.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)