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

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

来源:网络收集 时间:2026-07-17
导读: 198. // moveStart ( Dispatch.call(selection, \ew Variant(6)); 199. // ) 200. // moveEnd Dispatch.call(selection, \riant(6)); 201. Dispatch.call(selection, \); 202. } 203. } 204. /** */ 205. /** 206.

198. // moveStart ( Dispatch.call(selection, \ew Variant(6)); 199. // ) 200. // moveEnd Dispatch.call(selection, \riant(6)); 201. Dispatch.call(selection, \); 202. } 203. } 204. /** */ 205. /** 206. * 从选定内容或插入点开始查找文本 207. * 208. * @param toFindText 209. * 要查找的文本 210. * @return boolean true-查找到并选中该文本,false-未查找到文本 211. */ 212. public boolean find(String toFindText) { 213. if (toFindText == null || toFindText.equals(\)) 214. return false; 215. Dispatch selection = Dispatch.get(MsWordApp, \).toDispatch(); // 输入内容需要的对象 216. // 从selection所在位置开始查询 217. Dispatch find = Dispatch.call(selection, \).toDispatch(); 218. // 设置要查找的内容 219. Dispatch.put(find, \, toFindText); 220. // 向前查找 221. Dispatch.put(find, \, \); 222. // 设置格式 223. Dispatch.put(find, \, \); 224. // 大小写匹配 225. Dispatch.put(find, \, \); 226. // 全字匹配 227. Dispatch.put(find, \, \); 228. // 查找并选中 229. return Dispatch.call(find, \).getBoolean(); 230. } 231. /** */ 232. /** 233. * 把选定选定内容设定为替换文本 234. * 235. * @param toFindText 236. * 查找字符串 237. * @param newText 238. * 要替换的内容 239. * @return 240. */ 241. public boolean replaceText(String toFindText, String newText) { 242. if (!find(toFindText)) 243. return false; 244. Dispatch selection = Dispatch.get(MsWordApp, \).toDispatch(); // 输入内容需要的对象 245. Dispatch.put(selection, \, newText); 246. return true; 247. } 248. public void printFile() { 249. // Just print the current document to the default printer 250. Dispatch.call(document, \); 251. } 252. // 保存文档的更改 253. public void save() { 254. Dispatch.call(document, \); 255. } 256. public void saveFileAs(String filename) { 257. Dispatch.call(document, \, filename); 258. } 259. public void closeDocument() { 260. // Close the document without saving changes 261. // 0 = wdDoNotSaveChanges 262. // -1 = wdSaveChanges 263. // -2 = wdPromptToSaveChanges 264. Dispatch.call(document, \, new Variant(0)); 265. document = null; 266. } 267. public void closeWord() { 268. Dispatch.call(MsWordApp, \); 269. MsWordApp = null; 270. document = null; 271. } 272. // 设置wordApp打开后窗口的位置 273. public void setLocation() { 274. Dispatch activeWindow = Dispatch.get(MsWordApp, \tion\) 275. .toDispatch(); 276. Dispatch.put(activeWindow, \, new Variant(1)); // 0=default 277. // 1=maximize 278. // 2=minimize 279. Dispatch.put(activeWindow, \, new Variant(0)); 280. Dispatch.put(activeWindow, \, new Variant(0)); 281. Dispatch.put(activeWindow, \, new Variant(600)); 282. Dispatch.put(activeWindow, \, new Variant(800)); 283. } 284. } 285. public class JacobTest2 { 286. public static void createANewFileTest() { 287. WordBean wordBean = new WordBean(); 288. // word.openWord(true);// 打开 word 程序 289. wordBean.setVisible(true); 290. wordBean.createNewDocument();// 创建一个新文档 291. wordBean.setLocation();// 设置打开后窗口的位置 292. wordBean.insertText(\你好\);// 向文档中插入字符 293. wordBean.insertJpeg(\ + File.separator + \); // 插入图片 294. // 如果 ,想保存文件,下面三句 295. // word.saveFileAs(\ 296. // word.closeDocument(); 297. // word.closeWord(); 298. } 299. public static void openAnExistsFileTest() { 300. WordBean wordBean = new WordBean(); 301. wordBean.setVisible(true); // 是否前台打开word 程序,或者后台运行 302. wordBean.openFile(\); 303. wordBean.insertJpeg(\ + File.separator + \); // 插入图片(注意刚打开的word 304. // ,光标处于开头,故,图片在最前方插入) 305. wordBean.save(); 306. wordBean.closeDocument(); 307. wordBean.closeWord(); 308. } 309. public static void insertFormatStr(String str) { 310. WordBean wordBean = new WordBean(); 311. wordBean.setVisible(true); // 是否前台打开word 程序,或者后台运行 312. wordBean.createNewDocument();// 创建一个新文档 313. wordBean.insertFormatStr(str);// 插入一个段落,对其中的字体进行了设置 314. } 315. public static void insertTableTest() { 316. WordBean wordBean = new WordBean(); 317. wordBean.setVisible(true); // 是否前台打开word 程序,或者后台运行 318. wordBean.createNewDocument();// 创建一个新文档 319. wordBean.setLocation(); 320. wordBean.insertTable(\表名\, 3, 2); 321. wordBean.saveFileAs(\); 322. wordBean.closeDocument(); 323. wordBean.closeWord(); 324. } 325. public static void mergeTableCellTest() { 326. insertTableTest();//生成d://table.doc 327. WordBean wordBean = new WordBean(); 328. wordBean.setVisible(true); // 是否前台打开word 程序,或者后台运行 329. wordBean.openFile(\); 330. wordBean.mergeCellTest(); 331. } 332. public static void main(String[] args) { 333. // 进行测试前要保证d://a.jpg 图片文件存在 334. // createANewFileTest();//创建一个新文件 335. // openAnExistsFileTest();// 打开一个存在 的文件 336. // insertFormatStr(\格式 化字 …… 此处隐藏:3256字,全部文档内容请下载后查看。喜欢就下载吧 ……

java - jacob - 操作word - 文档,进行写操作(4).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)