1. 首先下載jacob-1.18.zip,解壓後有兩個文件jacob.jar 和 jacob.dll。須要把jacob.jar放到你工程的classpath中而且把jacob.dll放到jdk的bin目錄下(D:\Program Files\Java\jdk1.8.0_101\bin)目錄下或者系統的system32其餘相應的目錄下。java
放到jdk/bin目錄下,時常會出現jacob.dll already loaded in another classloader異常,因而,將jacob.dll文件放到了tomcat/bin目錄下,目前還未發現問題......tomcat
2.下面是提供的工具類app
1 package com.erqiao.rc.util; 2 3 import com.jacob.activeX.ActiveXComponent; 4 import com.jacob.com.ComThread; 5 import com.jacob.com.Dispatch; 6 import com.jacob.com.Variant; 7 8 /** */ 9 /*** 10 * 11 * @author BruceLeey 12 * 13 */ 14 public class MSWordManager { 15 // word文檔 16 private Dispatch doc = null; 17 // word運行程序對象 18 private ActiveXComponent word = null; 19 // 全部word文檔集合 20 private Dispatch documents = null; 21 // 選定的範圍或插入點 22 private static Dispatch selection = null; 23 // 設置是否保存後才退出的標誌 24 private boolean saveOnExit = true; 25 26 public MSWordManager() { 27 ComThread.InitSTA(); 28 if (word == null) { 29 word = new ActiveXComponent("Word.Application"); 30 word.setProperty("Visible", new Variant(false)); 31 } 32 if (documents == null) 33 documents = word.getProperty("Documents").toDispatch(); 34 } 35 36 /** */ 37 /** 38 * 設置退出時參數 39 * 40 * @param saveOnExit 41 * boolean true-退出時保存文件,false-退出時不保存文件 42 */ 43 public void setSaveOnExit(boolean saveOnExit) { 44 this.saveOnExit = saveOnExit; 45 } 46 47 /** */ 48 /** 49 * 建立一個新的word文檔 50 * 51 */ 52 public void createNewDocument() { 53 doc = Dispatch.call(documents, "Add").toDispatch(); 54 selection = Dispatch.get(word, "Selection").toDispatch(); 55 } 56 57 /** */ 58 /** 59 * 打開一個已存在的文檔 60 * 61 * @param docPath 62 */ 63 public void openDocument(String docPath) { 64 closeDocument(); 65 doc = Dispatch.call(documents, "Open", docPath).toDispatch(); 66 selection = Dispatch.get(word, "Selection").toDispatch(); 67 } 68 69 /** */ 70 /** 71 * 把選定的內容或插入點向上移動 72 * 73 * @param pos 74 * 移動的距離 75 */ 76 public void moveUp(int pos) { 77 if (selection == null) 78 selection = Dispatch.get(word, "Selection").toDispatch(); 79 for (int i = 0; i < pos; i++) 80 Dispatch.call(selection, "MoveUp"); 81 82 } 83 84 /** */ 85 /** 86 * 在指定的單元格里填寫數據 87 * 88 * @param tableIndex 89 * 文檔中的第tIndex個Table,即tIndex爲索引取 90 * @param cellRowIdx 91 * cell在Table第row行 92 * @param cellColIdx 93 * cell在Talbe第col列 94 * @param txt 95 * 填寫的數據 96 */ 97 public void moveToCell(int tableIndex, int cellRowIdx, int cellColIdx) { 98 // 全部表格 99 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 100 // 要填充的表格 101 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 102 Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx), new Variant(cellColIdx)).toDispatch(); 103 Dispatch.call(cell, "Select"); 104 Dispatch.call(selection, "MoveRight"); 105 } 106 107 /** 108 * 插入分節符 109 */ 110 public void InsertBreakNextPage(){ 111 Dispatch.call(word, "Run", new Variant("InsertBreakWdSectionBreakNextPage")); 112 } 113 114 /** 115 * 新增分頁符 116 */ 117 public void InsertBreak(){ 118 Dispatch.call(selection, "InsertBreak" , new Variant(7) ); 119 } 120 121 /** 122 * 換行符 123 */ 124 public void Enter(){ 125 Dispatch.call(selection, "TypeParagraph"); 126 } 127 128 /** */ 129 /** 130 * 把選定的內容或者插入點向下移動 131 * 132 * @param pos 133 * 移動的距離 134 */ 135 public void moveDown(int pos) { 136 if (selection == null) 137 selection = Dispatch.get(word, "Selection").toDispatch(); 138 for (int i = 0; i < pos; i++) 139 Dispatch.call(selection, "MoveDown"); 140 } 141 142 /** */ 143 /** 144 * 把選定的內容或者插入點向左移動 145 * 146 * @param pos 147 * 移動的距離 148 */ 149 public void moveLeft(int pos) { 150 if (selection == null) 151 selection = Dispatch.get(word, "Selection").toDispatch(); 152 for (int i = 0; i < pos; i++) { 153 Dispatch.call(selection, "MoveLeft"); 154 } 155 } 156 157 /** */ 158 /** 159 * 把選定的內容或者插入點向右移動 160 * 161 * @param pos 162 * 移動的距離 163 */ 164 public void moveRight(int pos) { 165 if (selection == null) 166 selection = Dispatch.get(word, "Selection").toDispatch(); 167 for (int i = 0; i < pos; i++) 168 Dispatch.call(selection, "MoveRight"); 169 } 170 171 /** */ 172 /** 173 * 把插入點移動到文件首位置 174 * 175 */ 176 public void moveStart() { 177 if (selection == null) 178 selection = Dispatch.get(word, "Selection").toDispatch(); 179 Dispatch.call(selection, "HomeKey", new Variant(6)); 180 } 181 182 /** */ 183 /** 184 * 從選定內容或插入點開始查找文本 185 * 186 * @param toFindText 187 * 要查找的文本 188 * @return boolean true-查找到並選中該文本,false-未查找到文本 189 */ 190 public boolean find(String toFindText) { 191 if (toFindText == null || toFindText.equals("")) 192 return false; 193 // 從selection所在位置開始查詢 194 Dispatch find = word.call(selection, "Find").toDispatch(); 195 // 設置要查找的內容 196 Dispatch.put(find, "Text", toFindText); 197 // 向前查找 198 Dispatch.put(find, "Forward", "True"); 199 // 設置格式 200 Dispatch.put(find, "Format", "True"); 201 // 大小寫匹配 202 Dispatch.put(find, "MatchCase", "True"); 203 // 全字匹配 204 Dispatch.put(find, "MatchWholeWord", "True"); 205 // 查找並選中 206 return Dispatch.call(find, "Execute").getBoolean(); 207 } 208 209 /** */ 210 /** 211 * 把選定選定內容設定爲替換文本 212 * 213 * @param toFindText 214 * 查找字符串 215 * @param newText 216 * 要替換的內容 217 * @return 218 */ 219 public boolean replaceText(String toFindText, String newText) { 220 if (!find(toFindText)) 221 return false; 222 Dispatch.put(selection, "Text", newText); 223 return true; 224 } 225 226 /** */ 227 /** 228 * 全局替換文本 229 * 230 * @param toFindText 231 * 查找字符串 232 * @param newText 233 * 要替換的內容 234 */ 235 public void replaceAllText(String toFindText, String newText) { 236 while (find(toFindText)) { 237 Dispatch.put(selection, "Text", newText); 238 Dispatch.call(selection, "MoveRight"); 239 } 240 } 241 242 /** */ 243 /** 244 * 在當前插入點插入字符串 245 * 246 * @param newText 247 * 要插入的新字符串 248 */ 249 public void insertText(String newText) { 250 Dispatch.put(selection, "Text", newText); 251 } 252 253 /** */ 254 /** 255 * 256 * @param toFindText 257 * 要查找的字符串 258 * @param imagePath 259 * 圖片路徑 260 * @return 261 */ 262 public boolean replaceImage(String toFindText, String imagePath) { 263 if (!find(toFindText)) 264 return false; 265 Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(), "AddPicture", imagePath); 266 return true; 267 } 268 269 /** */ 270 /** 271 * 全局替換圖片 272 * 273 * @param toFindText 274 * 查找字符串 275 * @param imagePath 276 * 圖片路徑 277 */ 278 public void replaceAllImage(String toFindText, String imagePath) { 279 while (find(toFindText)) { 280 Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(), "AddPicture", imagePath); 281 Dispatch.call(selection, "MoveRight"); 282 } 283 } 284 285 /** */ 286 /** 287 * 在當前插入點插入圖片 288 * 289 * @param imagePath 290 * 圖片路徑 291 */ 292 public void insertImage(String imagePath) { 293 if (imagePath != "" && imagePath != null) { 294 Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(), "AddPicture", imagePath); 295 } 296 } 297 298 /** */ 299 /** 300 * 合併單元格 301 * 302 * @param tableIndex 303 * @param fstCellRowIdx 304 * @param fstCellColIdx 305 * @param secCellRowIdx 306 * @param secCellColIdx 307 */ 308 public void mergeCell(int tableIndex, int fstCellRowIdx, int fstCellColIdx, int secCellRowIdx, int secCellColIdx) { 309 // 全部表格 310 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 311 // 要填充的表格 312 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 313 Dispatch fstCell = Dispatch.call(table, "Cell", new Variant(fstCellRowIdx), new Variant(fstCellColIdx)) 314 .toDispatch(); 315 Dispatch secCell = Dispatch.call(table, "Cell", new Variant(secCellRowIdx), new Variant(secCellColIdx)) 316 .toDispatch(); 317 Dispatch.call(fstCell, "Merge", secCell); 318 } 319 320 /** */ 321 /** 322 * 在指定的單元格里填寫數據 323 * 324 * @param tableIndex 325 * 文檔中的第tIndex個Table,即tIndex爲索引取 326 * @param cellRowIdx 327 * cell在Table第row行 328 * @param cellColIdx 329 * cell在Talbe第col列 330 * @param txt 331 * 填寫的數據 332 */ 333 public void putTxtToCell(int tableIndex, int cellRowIdx, int cellColIdx, String txt) { 334 // 全部表格 335 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 336 // 要填充的表格 337 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 338 Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx), new Variant(cellColIdx)).toDispatch(); 339 Dispatch.call(cell, "Select"); 340 Dispatch.put(selection, "Text", txt); 341 } 342 343 /** */ 344 /** 345 * 在指定的單元格里填寫數據 346 * 347 * @param tableIndex 348 * @param cellRowIdx 349 * @param cellColIdx 350 * @param txt 351 */ 352 public void putTxtToCellCenter(int tableIndex, int cellRowIdx, int cellColIdx, String txt) { 353 // 全部表格 354 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 355 // 要填充的表格 356 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 357 Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx), new Variant(cellColIdx)).toDispatch(); 358 Dispatch.call(cell, "Select"); 359 Dispatch alignment = Dispatch.get(selection, "ParagraphFormat").toDispatch(); 360 Dispatch.put(alignment, "Alignment", "3"); 361 Dispatch.put(selection, "Text", txt); 362 } 363 364 /** 365 * 366 * 獲得當前文檔的tables集合 367 */ 368 public Dispatch getTables() throws Exception { 369 if (this.doc == null) { 370 throw new Exception("there is not a document can't be operate!!!"); 371 } 372 return Dispatch.get(doc, "Tables").toDispatch(); 373 } 374 375 /** 376 * 377 * 獲得當前文檔的表格數 378 * 379 * @param Dispatch 380 */ 381 public int getTablesCount(Dispatch tables) throws Exception { 382 int count = 0; 383 try { 384 this.getTables(); 385 } catch (Exception e) { 386 throw new Exception("there is not any table!!"); 387 } 388 count = Dispatch.get(tables, "Count").getInt(); 389 return count; 390 } 391 392 /** */ 393 /** 394 * 在當前文檔拷貝剪貼板數據 395 * 396 * @param pos 397 */ 398 public void pasteExcelSheet(String pos) { 399 moveStart(); 400 if (this.find(pos)) { 401 Dispatch textRange = Dispatch.get(selection, "Range").toDispatch(); 402 Dispatch.call(textRange, "Paste"); 403 } 404 } 405 406 /** */ 407 /** 408 * 在當前文檔指定的位置拷貝表格 409 * 410 * @param pos 411 * 當前文檔指定的位置 412 * @param tableIndex 413 * 被拷貝的表格在word文檔中所處的位置 414 */ 415 public void copyTable(/*String pos, */int tableIndex) { 416 // 全部表格 417 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 418 // 要填充的表格 419 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 420 Dispatch range = Dispatch.get(table, "Range").toDispatch(); 421 Dispatch.call(range, "Copy"); 422 Dispatch.call(selection, "Paste"); 423 // if (this.find(pos)) { 424 // Dispatch textRange = Dispatch.get(selection, "Range").toDispatch(); 425 // Dispatch.call(textRange, "Paste"); 426 // } 427 } 428 429 /** */ 430 /** 431 * 在當前文檔指定的位置拷貝來自另外一個文檔中的表格 432 * 433 * @param anotherDocPath 434 * 另外一個文檔的磁盤路徑 435 * @param tableIndex 436 * 被拷貝的表格在另外一格文檔中的位置 437 * @param pos 438 * 當前文檔指定的位置 439 */ 440 public void copyTableFromAnotherDoc(String anotherDocPath, int tableIndex, String pos) { 441 Dispatch doc2 = null; 442 try { 443 doc2 = Dispatch.call(documents, "Open", anotherDocPath).toDispatch(); 444 // 全部表格 445 Dispatch tables = Dispatch.get(doc2, "Tables").toDispatch(); 446 // 要填充的表格 447 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 448 Dispatch range = Dispatch.get(table, "Range").toDispatch(); 449 Dispatch.call(range, "Copy"); 450 if (this.find(pos)) { 451 Dispatch textRange = Dispatch.get(selection, "Range").toDispatch(); 452 Dispatch.call(textRange, "Paste"); 453 } 454 } catch (Exception e) { 455 e.printStackTrace(); 456 } finally { 457 if (doc2 != null) { 458 Dispatch.call(doc2, "Close", new Variant(saveOnExit)); 459 doc2 = null; 460 } 461 } 462 } 463 464 /** */ 465 /** 466 * 在當前文檔指定的位置拷貝來自另外一個文檔中的圖片 467 * 468 * @param anotherDocPath 469 * 另外一個文檔的磁盤路徑 470 * @param shapeIndex 471 * 被拷貝的圖片在另外一格文檔中的位置 472 * @param pos 473 * 當前文檔指定的位置 474 */ 475 public void copyImageFromAnotherDoc(String anotherDocPath, int shapeIndex, String pos) { 476 Dispatch doc2 = null; 477 try { 478 doc2 = Dispatch.call(documents, "Open", anotherDocPath).toDispatch(); 479 Dispatch shapes = Dispatch.get(doc2, "InLineShapes").toDispatch(); 480 Dispatch shape = Dispatch.call(shapes, "Item", new Variant(shapeIndex)).toDispatch(); 481 Dispatch imageRange = Dispatch.get(shape, "Range").toDispatch(); 482 Dispatch.call(imageRange, "Copy"); 483 if (this.find(pos)) { 484 Dispatch textRange = Dispatch.get(selection, "Range").toDispatch(); 485 Dispatch.call(textRange, "Paste"); 486 } 487 } catch (Exception e) { 488 e.printStackTrace(); 489 } finally { 490 if (doc2 != null) { 491 Dispatch.call(doc2, "Close", new Variant(saveOnExit)); 492 doc2 = null; 493 } 494 } 495 } 496 497 /** */ 498 /** 499 * 建立表格 500 * 501 * @param pos 502 * 位置 503 * @param cols 504 * 列數 505 * @param rows 506 * 行數 507 */ 508 public void createTable(String pos, int numCols, int numRows) { 509 if (find(pos)) { 510 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 511 Dispatch range = Dispatch.get(selection, "Range").toDispatch(); 512 Dispatch newTable = Dispatch.call(tables, "Add", range, new Variant(numRows), new Variant(numCols)) 513 .toDispatch(); 514 Dispatch.call(selection, "MoveRight"); 515 } 516 } 517 518 /** */ 519 /** 520 * 在指定行前面增長行 521 * 522 * @param tableIndex 523 * word文件中的第N張表(從1開始) 524 * @param rowIndex 525 * 指定行的序號(從1開始) 526 */ 527 public void addTableRow(int tableIndex, int rowIndex) { 528 // 全部表格 529 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 530 // 要填充的表格 531 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 532 // 表格的全部行 533 Dispatch rows = Dispatch.get(table, "Rows").toDispatch(); 534 Dispatch row = Dispatch.call(rows, "Item", new Variant(rowIndex)).toDispatch(); 535 Dispatch.call(rows, "Add", new Variant(row)); 536 } 537 538 /** */ 539 /** 540 * 在第1行前增長一行 541 * 542 * @param tableIndex 543 * word文檔中的第N張表(從1開始) 544 */ 545 public void addFirstTableRow(int tableIndex) { 546 // 全部表格 547 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 548 // 要填充的表格 549 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 550 // 表格的全部行 551 Dispatch rows = Dispatch.get(table, "Rows").toDispatch(); 552 Dispatch row = Dispatch.get(rows, "First").toDispatch(); 553 Dispatch.call(rows, "Add", new Variant(row)); 554 } 555 556 /** */ 557 /** 558 * 在最後1行前增長一行 559 * 560 * @param tableIndex 561 * word文檔中的第N張表(從1開始) 562 */ 563 public void addLastTableRow(int tableIndex) { 564 // 全部表格 565 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 566 // 要填充的表格 567 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 568 // 表格的全部行 569 Dispatch rows = Dispatch.get(table, "Rows").toDispatch(); 570 Dispatch row = Dispatch.get(rows, "Last").toDispatch(); 571 Dispatch.call(rows, "Add", new Variant(row)); 572 } 573 574 /** */ 575 /** 576 * 增長一行 577 * 578 * @param tableIndex 579 * word文檔中的第N張表(從1開始) 580 */ 581 public void addRow(int tableIndex) { 582 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 583 // 要填充的表格 584 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 585 // 表格的全部行 586 Dispatch rows = Dispatch.get(table, "Rows").toDispatch(); 587 Dispatch.call(rows, "Add"); 588 } 589 590 /** */ 591 /** 592 * 增長一列 593 * 594 * @param tableIndex 595 * word文檔中的第N張表(從1開始) 596 */ 597 public void addCol(int tableIndex) { 598 // 全部表格 599 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 600 // 要填充的表格 601 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 602 // 表格的全部行 603 Dispatch cols = Dispatch.get(table, "Columns").toDispatch(); 604 Dispatch.call(cols, "Add").toDispatch(); 605 Dispatch.call(cols, "AutoFit"); 606 } 607 608 /** */ 609 /** 610 * 在指定列前面增長表格的列 611 * 612 * @param tableIndex 613 * word文檔中的第N張表(從1開始) 614 * @param colIndex 615 * 制定列的序號 (從1開始) 616 */ 617 public void addTableCol(int tableIndex, int colIndex) { 618 // 全部表格 619 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 620 // 要填充的表格 621 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 622 // 表格的全部行 623 Dispatch cols = Dispatch.get(table, "Columns").toDispatch(); 624 System.out.println(Dispatch.get(cols, "Count")); 625 Dispatch col = Dispatch.call(cols, "Item", new Variant(colIndex)).toDispatch(); 626 // Dispatch col = Dispatch.get(cols, "First").toDispatch(); 627 Dispatch.call(cols, "Add", col).toDispatch(); 628 Dispatch.call(cols, "AutoFit"); 629 } 630 631 /** */ 632 /** 633 * 在第1列前增長一列 634 * 635 * @param tableIndex 636 * word文檔中的第N張表(從1開始) 637 */ 638 public void addFirstTableCol(int tableIndex) { 639 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 640 // 要填充的表格 641 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 642 // 表格的全部行 643 Dispatch cols = Dispatch.get(table, "Columns").toDispatch(); 644 Dispatch col = Dispatch.get(cols, "First").toDispatch(); 645 Dispatch.call(cols, "Add", col).toDispatch(); 646 Dispatch.call(cols, "AutoFit"); 647 } 648 649 /** */ 650 /** 651 * 在最後一列前增長一列 652 * 653 * @param tableIndex 654 * word文檔中的第N張表(從1開始) 655 */ 656 public void addLastTableCol(int tableIndex) { 657 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 658 // 要填充的表格 659 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 660 // 表格的全部行 661 Dispatch cols = Dispatch.get(table, "Columns").toDispatch(); 662 Dispatch col = Dispatch.get(cols, "Last").toDispatch(); 663 Dispatch.call(cols, "Add", col).toDispatch(); 664 Dispatch.call(cols, "AutoFit"); 665 } 666 667 /** */ 668 /** 669 * 設置當前選定內容的字體 670 * 671 * @param boldSize 672 * @param italicSize 673 * @param underLineSize 674 * 下劃線 675 * @param colorSize 676 * 字體顏色 677 * @param size 678 * 字體大小 679 * @param name 680 * 字體名稱 681 */ 682 public void setFont(boolean bold, boolean italic, boolean underLine, String colorSize, String size, String name) { 683 Dispatch font = Dispatch.get(selection, "Font").toDispatch(); 684 Dispatch.put(font, "Name", new Variant(name)); 685 Dispatch.put(font, "Bold", new Variant(bold)); 686 Dispatch.put(font, "Italic", new Variant(italic)); 687 Dispatch.put(font, "Underline", new Variant(underLine)); 688 Dispatch.put(font, "Color", colorSize); 689 Dispatch.put(font, "Size", size); 690 } 691 692 public void setFontCenter(String name) { 693 Dispatch font = Dispatch.get(selection, "Font").toDispatch(); 694 Dispatch alignment = Dispatch.get(selection, "ParagraphFormat").toDispatch(); 695 Dispatch.put(alignment, "Alignment", "3"); 696 Dispatch.call(selection, "TypeText", name); 697 } 698 699 /** */ 700 /** 701 * 文件保存或另存爲 702 * 703 * @param savePath 704 * 保存或另存爲路徑 705 */ 706 public void save(String savePath) { 707 Dispatch.call(doc, "SaveAs", savePath); // 保存 708 /**//* 709 * Dispatch.call(Dispatch.call(word, "WordBasic").getDispatch(), 710 * "FileSaveAs", savePath); 711 */ 712 } 713 714 /** */ 715 /** 716 * 關閉當前word文檔 717 * 718 */ 719 public void closeDocument() { 720 if (doc != null) { 721 Dispatch.call(doc, "Save"); 722 Dispatch.call(doc, "Close", new Variant(saveOnExit)); 723 doc = null; 724 } 725 } 726 727 /** */ 728 /** 729 * 關閉所有應用 730 * 731 */ 732 public void close() { 733 closeDocument(); 734 if (word != null) { 735 Dispatch.call(word, "Quit"); 736 word = null; 737 } 738 selection = null; 739 documents = null; 740 ComThread.Release(); 741 } 742 743 /** */ 744 /** 745 * 打印當前word文檔 746 * 747 */ 748 public void printFile() { 749 if (doc != null) { 750 Dispatch.call(doc, "PrintOut"); 751 } 752 } 753 754 /** */ 755 /** 756 * 刪除一行 757 * 758 * @param tableIndex 759 * word文檔中的第N張表(從1開始) 760 */ 761 public void delRow(int tableIndex) { 762 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 763 // 要填充的表格 764 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 765 // 表格的全部行 766 Dispatch rows = Dispatch.get(table, "Rows").toDispatch(); 767 Object temp1 = Dispatch.get(rows, "Count"); 768 String temp2 = temp1.toString(); 769 int count = Integer.parseInt(temp2); 770 while (count > 1) { 771 Dispatch row = Dispatch.get(rows, "Last").toDispatch(); 772 Dispatch.call(row, "Delete"); 773 rows = Dispatch.get(table, "Rows").toDispatch(); 774 temp1 = Dispatch.get(rows, "Count"); 775 temp2 = temp1.toString(); 776 count = Integer.parseInt(temp2); 777 } 778 } 779 780 public void setProp(String sName, String sValue) { 781 Dispatch props = Dispatch.get(doc, "CustomDocumentProperties").toDispatch(); 782 Dispatch prop = Dispatch.call(props, "Item", sName).toDispatch(); 783 String sOldVal = Dispatch.get(prop, "Value").toString(); 784 if (!sOldVal.equals(sValue)) 785 Dispatch.put(prop, "Value", sValue); 786 } 787 788 /** */ 789 /** 790 * @param nType: 791 * 1, number; 2,bool; 3,date; 4,str; 792 */ 793 public void addProp(String sName, int nType, String sValue) { 794 Dispatch props = Dispatch.get(doc, "CustomDocumentProperties").toDispatch(); 795 Dispatch prop = null; 796 try { 797 prop = Dispatch.call(props, "Item", sName).toDispatch(); 798 } catch (Exception e) { 799 prop = null; 800 } 801 if (prop != null) 802 return; 803 // 1, number; 2,bool; 3,date; 4,str; 804 prop = Dispatch.call(props, "Add", sName, false, nType, sValue).toDispatch(); 805 Dispatch.put(prop, "Value", sValue); 806 } 807 808 public String getProp(String sName) { 809 String sValue = null; 810 Dispatch props = Dispatch.get(doc, "CustomDocumentProperties").toDispatch(); 811 Dispatch prop = Dispatch.call(props, "Item", sName).toDispatch(); 812 813 sValue = Dispatch.get(prop, "Value").toString(); 814 @SuppressWarnings("unused") 815 String sType = Dispatch.get(prop, "Type").toString(); 816 817 try { 818 Dispatch prop0 = Dispatch.call(doc, "CustomDocumentProperties", sName).toDispatch(); 819 sValue = Dispatch.get(prop0, "Value").toString(); 820 } catch (Exception e) { 821 e.printStackTrace(); 822 } 823 return sValue; 824 } 825 826 public void fack_change() { 827 Dispatch _sel = Dispatch.call(doc, "Range", 0, 0).toDispatch(); 828 Dispatch.call(_sel, "InsertBefore", "A"); 829 Dispatch.call(_sel, "Select"); 830 Dispatch.call(_sel, "Delete"); 831 } 832 833 /** 834 * 從第tIndex個Table中取出值第row行,第col列的值 835 * 836 * @param tableIndex 837 * 文檔中的第tIndex個Table,即tIndex爲索引取 838 * @param cellRowIdx 839 * cell在Table第row行 840 * @param cellColIdx 841 * cell在Talbe第col列 842 * @return cell單元值 843 * @throws Exception 844 */ 845 public String getCellString(int tableIndex, int cellRowIdx, int cellColIdx) throws Exception { 846 // 全部表格 847 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 848 // 要取數據的表格 849 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 850 Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx), new Variant(cellColIdx)).toDispatch(); 851 Dispatch.call(cell, "Select"); 852 return Dispatch.get(selection, "Text").toString(); 853 } 854 855 /** 856 * 從第tableIndex個Table中取出值第cellRowIdx行,第cellColIdx列的值 857 * 858 * @param tIndex 859 * 文檔中的第tIndex個Table,即tIndex爲索引取 860 * @param cellRowIdx 861 * cell在Table第row行 862 * @param cellColIdx 863 * cell在Talbe第col列 864 * @return cell單元值 865 * @throws Exception 866 */ 867 public void getCellValue(int tableIndex, int cellRowIdx, int cellColIdx) throws Exception { 868 869 // 全部表格 870 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); 871 // 要取數據的表格 872 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); 873 Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx), new Variant(cellColIdx)).toDispatch(); 874 Dispatch.call(cell, "Select"); 875 Dispatch.call(selection, "Copy"); 876 877 } 878 879 880 }
3. 這裏比較關鍵工具
須要調用宏來寫代碼,才能實現更多的功能測試
調用word宏
第一步,錄製宏
在d盤根目錄下(文檔存放在哪裏沒有要求)新建一個word文檔,名爲test1.doc,打開,而後錄製一段宏(具體錄製哪類宏自便,調用時無需傳參數便可,可是宏的保存方式要選擇「全部文檔(Normal)」,這樣任何文檔均可以調用這個宏了),宏名爲macro1。
第二步,將test1.doc中宏macro1產生的影響撤銷(好比那段宏是輸入一段文字,那麼就把這段文字刪除)並保存,以便觀察測試。
第三步,編寫java調用代碼 字體
1 ActiveXComponent word=new ActiveXComponent("Word.Application"); 2 Dispatch documents = word.getProperty("Documents").toDispatch(); 3 Dispatch document = Dispatch.call(documents, "Open", "d:/test1.doc").toDispatch();//指定要打開的文檔而且打開它 4 Dispatch.call(word, "Run", new Variant("macro1"));//在這個文檔上運行宏
第四步,執行這段java代碼
執行完成以後,能夠發現被撤銷的宏影響又回來了,說明宏調用成功。
第五步,高級特性
在相同目錄下(文檔存放目錄沒有規定)新建一個空白的word文檔test2.doc,而後將以上代碼修改成: ui
1 ActiveXComponent word=new ActiveXComponent("Word.Application"); 2 Dispatch documents = word.getProperty("Documents").toDispatch(); 3 Dispatch document = Dispatch.call(documents, "Open", "d:/test2.doc").toDispatch();//指定要打開的文檔而且打開它 4 Dispatch.call(word, "Run", new Variant("macro1"));//在這個文檔上運行宏
執行以上代碼,能夠發現,咱們在test1.doc上錄製的宏也能夠在test2.doc上運行成功(固然選擇宏保存時必需要保存到「全部文檔(Normal)」中)。 this
調用excel宏
調用excel宏和調用word宏有點區別,由於excel不能將宏保存到「全部文檔(Normal)」上,所以在調用宏的時候須要指明宏所在的具體文檔,最後一條語句須要這麼寫:spa
1 Variant result = Dispatch.call(excel, "Run", new Variant("test.xls!Module1.test"),//這裏須要說明宏所在的文檔 2 new Variant(arg1), 3 new Variant(arg2));
4. 插入分節符代碼這麼來寫;.net
首先錄製一個宏,命名爲InsertBreakWdSectionBreakNextPage
在程序中用jacob調用它。
Dispatch.call(app, "Run", new Variant("InsertBreakWdSectionBreakNextPage"));
參考: