`

jacob操作word文档(二)

 
阅读更多

  1. import com.jacob.activeX.ActiveXComponent;  
  2. import com.jacob.com.Dispatch;  
  3. import com.jacob.com.Variant;   
  4.   
  5. /** 
  6.  *  
  7.  * @author zhuzhen_hua@yahoo.com.cn 
  8.  *  
  9.  */   
  10.   
  11. public class WordBean {  
  12.  // word文档  
  13.  private Dispatch doc;   
  14.   
  15.  // word运行程序对象  
  16.  private ActiveXComponent word;   
  17.   
  18.  // 所有word文档集合  
  19.  private Dispatch documents;   
  20.   
  21.  // 选定的范围或插入点  
  22.  private Dispatch selection;  
  23.     
  24.   
  25.  private boolean saveOnExit = true;   
  26.   
  27.  public WordBean() {  
  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.   * @param saveOnExit 
  40.   *            boolean true-退出时保存文件,false-退出时不保存文件 
  41.   */  
  42.  public void setSaveOnExit(boolean saveOnExit) {  
  43.   this.saveOnExit = saveOnExit;  
  44.  }   
  45.   
  46.  /** 
  47.   * 创建一个新的word文档 
  48.   *  
  49.   */  
  50.  public void createNewDocument() {  
  51.   doc = Dispatch.call(documents, "Add").toDispatch();  
  52.   selection = Dispatch.get(word, "Selection").toDispatch();  
  53.  }   
  54.   
  55.  /** 
  56.   * 打开一个已存在的文档 
  57.   *  
  58.   * @param docPath 
  59.   */  
  60.  public void openDocument(String docPath) {  
  61.   closeDocument();  
  62.   doc = Dispatch.call(documents, "Open", docPath).toDispatch();  
  63.   selection = Dispatch.get(word, "Selection").toDispatch();  
  64.  }   
  65.   
  66.  /** 
  67.   * 把选定的内容或插入点向上移动 
  68.   *  
  69.   * @param pos 
  70.   *            移动的距离 
  71.   */  
  72.  public void moveUp(int pos) {  
  73.   if (selection == null)  
  74.    selection = Dispatch.get(word, "Selection").toDispatch();  
  75.   for (int i = 0; i < pos; i++)  
  76.    Dispatch.call(selection, "MoveUp");   
  77.   
  78.  }   
  79.   
  80.  /** 
  81.   * 把选定的内容或者插入点向下移动 
  82.   *  
  83.   * @param pos 
  84.   *            移动的距离 
  85.   */  
  86.  public void moveDown(int pos) {  
  87.   if (selection == null)  
  88.    selection = Dispatch.get(word, "Selection").toDispatch();  
  89.   for (int i = 0; i < pos; i++)  
  90.    Dispatch.call(selection, "MoveDown");  
  91.  }   
  92.   
  93.  /** 
  94.   * 把选定的内容或者插入点向左移动 
  95.   *  
  96.   * @param pos 
  97.   *            移动的距离 
  98.   */  
  99.  public void moveLeft(int pos) {  
  100.   if (selection == null)  
  101.    selection = Dispatch.get(word, "Selection").toDispatch();  
  102.   for (int i = 0; i < pos; i++) {  
  103.    Dispatch.call(selection, "MoveLeft");  
  104.   }  
  105.  }   
  106.   
  107.  /** 
  108.   * 把选定的内容或者插入点向右移动 
  109.   *  
  110.   * @param pos 
  111.   *            移动的距离 
  112.   */  
  113.  public void moveRight(int pos) {  
  114.   if (selection == null)  
  115.    selection = Dispatch.get(word, "Selection").toDispatch();  
  116.   for (int i = 0; i < pos; i++)  
  117.    Dispatch.call(selection, "MoveRight");  
  118.  }   
  119.   
  120.  /** 
  121.   * 把插入点移动到文件首位置 
  122.   *  
  123.   */  
  124.  public void moveStart() {  
  125.   if (selection == null)  
  126.    selection = Dispatch.get(word, "Selection").toDispatch();  
  127.   Dispatch.call(selection, "HomeKey"new Variant(6));  
  128.  }   
  129.   
  130.  /** 
  131.   * 从选定内容或插入点开始查找文本 
  132.   *  
  133.   * @param toFindText 
  134.   *            要查找的文本 
  135.   * @return boolean true-查找到并选中该文本,false-未查找到文本 
  136.   */  
  137.  public boolean find(String toFindText) {  
  138.   if (toFindText == null || toFindText.equals(""))  
  139.    return false;  
  140.   // 从selection所在位置开始查询  
  141.   Dispatch find = word.call(selection, "Find").toDispatch();  
  142.   // 设置要查找的内容  
  143.   Dispatch.put(find, "Text", toFindText);  
  144.   // 向前查找  
  145.   Dispatch.put(find, "Forward""True");  
  146.   // 设置格式  
  147.   Dispatch.put(find, "Format""True");  
  148.   // 大小写匹配  
  149.   Dispatch.put(find, "MatchCase""True");  
  150.   // 全字匹配  
  151.   Dispatch.put(find, "MatchWholeWord""True");  
  152.   // 查找并选中  
  153.   return Dispatch.call(find, "Execute").getBoolean();  
  154.  }   
  155.   
  156.  /** 
  157.   * 把选定选定内容设定为替换文本 
  158.   *  
  159.   * @param toFindText 
  160.   *            查找字符串 
  161.   * @param newText 
  162.   *            要替换的内容 
  163.   * @return 
  164.   */  
  165.  public boolean replaceText(String toFindText, String newText) {  
  166.   if (!find(toFindText))  
  167.    return false;  
  168.   Dispatch.put(selection, "Text", newText);  
  169.   return true;  
  170.  }   
  171.   
  172.  /** 
  173.   * 全局替换文本 
  174.   *  
  175.   * @param toFindText 
  176.   *            查找字符串 
  177.   * @param newText 
  178.   *            要替换的内容 
  179.   */  
  180.  public void replaceAllText(String toFindText, String newText) {  
  181.   while (find(toFindText)) {  
  182.    Dispatch.put(selection, "Text", newText);  
  183.    Dispatch.call(selection, "MoveRight");  
  184.   }  
  185.  }   
  186.   
  187.  /** 
  188.   * 在当前插入点插入字符串 
  189.   *  
  190.   * @param newText 
  191.   *            要插入的新字符串 
  192.   */  
  193.  public void insertText(String newText) {  
  194.   Dispatch.put(selection, "Text", newText);  
  195.  }   
  196.   
  197.  /** 
  198.   *  
  199.   * @param toFindText 
  200.   *            要查找的字符串 
  201.   * @param imagePath 
  202.   *            图片路径 
  203.   * @return 
  204.   */  
  205.  public boolean replaceImage(String toFindText, String imagePath) {  
  206.   if (!find(toFindText))  
  207.    return false;  
  208.   Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),  
  209.     "AddPicture", imagePath);  
  210.   return true;  
  211.  }   
  212.   
  213.  /** 
  214.   * 全局替换图片 
  215.   *  
  216.   * @param toFindText 
  217.   *            查找字符串 
  218.   * @param imagePath 
  219.   *            图片路径 
  220.   */  
  221.  public void replaceAllImage(String toFindText, String imagePath) {  
  222.   while (find(toFindText)) {  
  223.    Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),  
  224.      "AddPicture", imagePath);  
  225.    Dispatch.call(selection, "MoveRight");  
  226.   }  
  227.  }   
  228.   
  229.  /** 
  230.   * 在当前插入点插入图片 
  231.   *  
  232.   * @param imagePath 
  233.   *            图片路径 
  234.   */  
  235.  public void insertImage(String imagePath) {  
  236.   Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),  
  237.     "AddPicture", imagePath);  
  238.  }   
  239.   
  240.  /** 
  241.   * 合并单元格 
  242.   *  
  243.   * @param tableIndex 
  244.   * @param fstCellRowIdx 
  245.   * @param fstCellColIdx 
  246.   * @param secCellRowIdx 
  247.   * @param secCellColIdx 
  248.   */  
  249.  public void mergeCell(int tableIndex, int fstCellRowIdx, int fstCellColIdx,  
  250.    int secCellRowIdx, int secCellColIdx) {  
  251.   // 所有表格  
  252.   Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();  
  253.   // 要填充的表格  
  254.   Dispatch table = Dispatch.call(tables, "Item"new Variant(tableIndex))  
  255.     .toDispatch();  
  256.   Dispatch fstCell = Dispatch.call(table, "Cell",  
  257.     new Variant(fstCellRowIdx), new Variant(fstCellColIdx))  
  258.     .toDispatch();  
  259.   Dispatch secCell = Dispatch.call(table, "Cell",  
  260.     new Variant(secCellRowIdx), new Variant(secCellColIdx))  
  261.     .toDispatch();  
  262.   Dispatch.call(fstCell, "Merge", secCell);  
  263.  }   
  264.   
  265.  /** 
  266.   * 在指定的单元格里填写数据 
  267.   *  
  268.   * @param tableIndex 
  269.   * @param cellRowIdx 
  270.   * @param cellColIdx 
  271.   * @param txt 
  272.   */  
  273.  public void putTxtToCell(int tableIndex, int cellRowIdx, int cellColIdx,  
  274.    String txt) {  
  275.   // 所有表格  
  276.   Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();  
  277.   // 要填充的表格  
  278.   Dispatch table = Dispatch.call(tables, "Item"new Variant(tableIndex))  
  279.     .toDispatch();  
  280.   Dispatch cell = Dispatch.call(table, "Cell"new Variant(cellRowIdx),  
  281.     new Variant(cellColIdx)).toDispatch();  
  282.   Dispatch.call(cell, "Select");  
  283.   Dispatch.put(selection, "Text", txt);  
  284.  }  
  285.  /** 
  286.   * 在当前文档拷贝剪贴板数据 
  287.   * @param pos 
  288.   */  
  289.     public void pasteExcelSheet(String pos){  
  290.      moveStart();  
  291.      if (this.find(pos)) {  
  292.    Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();  
  293.    Dispatch.call(textRange, "Paste");  
  294.   }  
  295.     }  
  296.       
  297.  /** 
  298.   * 在当前文档指定的位置拷贝表格 
  299.   *  
  300.   * @param pos 
  301.   *            当前文档指定的位置 
  302.   * @param tableIndex 
  303.   *            被拷贝的表格在word文档中所处的位置 
  304.   */  
  305.  public void copyTable(String pos, int tableIndex) {  
  306.   // 所有表格  
  307.   Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();  
  308.   // 要填充的表格  
  309.   Dispatch table = Dispatch.call(tables, "Item"new Variant(tableIndex))  
  310.     .toDispatch();  
  311.   Dispatch range = Dispatch.get(table, "Range").toDispatch();  
  312.   Dispatch.call(range, "Copy");  
  313.   if (this.find(pos)) {  
  314.    Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();  
  315.    Dispatch.call(textRange, "Paste");  
  316.   }  
  317.  }   
  318.   
  319.  /** 
  320.   * 在当前文档指定的位置拷贝来自另一个文档中的表格 
  321.   *  
  322.   * @param anotherDocPath 
  323.   *            另一个文档的磁盘路径 
  324.   * @param tableIndex 
  325.   *            被拷贝的表格在另一格文档中的位置 
  326.   * @param pos 
  327.   *            当前文档指定的位置 
  328.   */  
  329.  public void copyTableFromAnotherDoc(String anotherDocPath, int tableIndex,  
  330.    String pos) {  
  331.   Dispatch doc2 = null;  
  332.   try {  
  333.    doc2 = Dispatch.call(documents, "Open", anotherDocPath)  
  334.      .toDispatch();  
  335.    // 所有表格  
  336.    Dispatch tables = Dispatch.get(doc2, "Tables").toDispatch();  
  337.    // 要填充的表格  
  338.    Dispatch table = Dispatch.call(tables, "Item",  
  339.      new Variant(tableIndex)).toDispatch();  
  340.    Dispatch range = Dispatch.get(table, "Range").toDispatch();  
  341.    Dispatch.call(range, "Copy");  
  342.    if (this.find(pos)) {  
  343.     Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();  
  344.     Dispatch.call(textRange, "Paste");  
  345.    }  
  346.   } catch (Exception e) {  
  347.    e.printStackTrace();  
  348.   } finally {  
  349.    if (doc2 != null) {  
  350.     Dispatch.call(doc2, "Close"new Variant(saveOnExit));  
  351.     doc2 = null;  
  352.    }  
  353.   }  
  354.  }  
  355.  /** 
  356.   * 在当前文档指定的位置拷贝来自另一个文档中的图片 
  357.   * @param anotherDocPath 另一个文档的磁盘路径 
  358.   * @param shapeIndex 被拷贝的图片在另一格文档中的位置 
  359.   * @param pos 当前文档指定的位置 
  360.   */  
  361.     public void copyImageFromAnotherDoc(String anotherDocPath,int shapeIndex,String pos){  
  362.      Dispatch doc2 = null;  
  363.   try {  
  364.    doc2 = Dispatch.call(documents, "Open", anotherDocPath)  
  365.      .toDispatch();  
  366.    Dispatch shapes = Dispatch.get(doc2, "InLineShapes").toDispatch();  
  367.    Dispatch shape = Dispatch.call(shapes, "Item"new Variant(shapeIndex)).toDispatch();  
  368.    Dispatch imageRange = Dispatch.get(shape, "Range").toDispatch();  
  369.    Dispatch.call(imageRange, "Copy");  
  370.    if (this.find(pos)) {  
  371.     Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();  
  372.     Dispatch.call(textRange, "Paste");  
  373.    }  
  374.   } catch (Exception e) {  
  375.    e.printStackTrace();  
  376.   } finally {  
  377.    if (doc2 != null) {  
  378.     Dispatch.call(doc2, "Close"new Variant(saveOnExit));  
  379.     doc2 = null;  
  380.    }  
  381.   }  
  382.     }  
  383.  /** 
  384.   * 创建表格 
  385.   *  
  386.   * @param pos 
  387.   *            位置 
  388.   * @param cols 
  389.   *            列数 
  390.   * @param rows 
  391.   *            行数 
  392.   */  
  393.  public void createTable(String pos, int numCols, int numRows) {  
  394.   if (find(pos)) {  
  395.    Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();  
  396.    Dispatch range = Dispatch.get(selection, "Range").toDispatch();  
  397.    Dispatch newTable = Dispatch.call(tables, "Add", range,  
  398.      new Variant(numRows), new Variant(numCols)).toDispatch();  
  399.    Dispatch.call(selection, "MoveRight");  
  400.   }else{  
  401.     Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();  
  402.     Dispatch range = Dispatch.get(selection, "Range").toDispatch();  
  403.     Dispatch newTable = Dispatch.call(tables, "Add", range,  
  404.       new Variant(numRows), new Variant(numCols)).toDispatch();  
  405.     Dispatch.call(selection, "MoveRight");  
  406.   }  
  407.  }   
  408.   
  409.  /** 
  410.   * 在指定行前面增加行 
  411.   *  
  412.   * @param tableIndex 
  413.   *            word文件中的第N张表(从1开始) 
  414.   * @param rowIndex 
  415.   *            指定行的序号(从1开始) 
  416.   */  
  417.  public void addTableRow(int tableIndex, int rowIndex) {  
  418.   // 所有表格  
  419.   Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();  
  420.   // 要填充的表格  
  421.   Dispatch table = Dispatch.call(tables, "Item"new Variant(tableIndex))  
  422.     .toDispatch();  
  423.   // 表格的所有行  
  424.   Dispatch rows = Dispatch.get(table, "Rows").toDispatch();  
  425.   Dispatch row = Dispatch.call(rows, "Item"new Variant(rowIndex))  
  426.     .toDispatch();  
  427.   Dispatch.call(rows, "Add"new Variant(row));  
  428.  }   
  429.   
  430.  /** 
  431.   * 在第1行前增加一行 
  432.   *  
  433.   * @param tableIndex 
  434.   *  word文档中的第N张表(从1开始) 
  435.   */  
  436.  public void addFirstTableRow(int tableIndex) {  
  437.   // 所有表格  
  438.   Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();  
  439.   // 要填充的表格  
  440.   Dispatch table = Dispatch.call(tables, "Item"new Variant(tableIndex))  
  441.     .toDispatch();  
  442.   // 表格的所有行  
  443.   Dispatch rows = Dispatch.get(table, "Rows").toDispatch();  
  444.   Dispatch row = Dispatch.get(rows, "First").toDispatch();  
  445.   Dispatch.call(rows, "Add"new Variant(row));  
  446.  }   
  447.   
  448.  /** 
  449.   * 在最后1行前增加一行 
  450.   *  
  451.   * @param tableIndex 
  452.   *            word文档中的第N张表(从1开始) 
  453.   */  
  454.  public void addLastTableRow(int tableIndex) {  
  455.   // 所有表格  
  456.   Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();  
  457.   // 要填充的表格  
  458.   Dispatch table = Dispatch.call(tables, "Item"new Variant(tableIndex))  
  459.     .toDispatch();  
  460.   // 表格的所有行  
  461.   Dispatch rows = Dispatch.get(table, "Rows").toDispatch();  
  462.   Dispatch row = Dispatch.get(rows, "Last").toDispatch();  
  463.   Dispatch.call(rows, "Add"new Variant(row));  
  464.  }   
  465.  /** 
  466.   * 增加一行 
  467.   *  
  468.   * @param tableIndex 
  469.   *            word文档中的第N张表(从1开始) 
  470.   */  
  471.  public void addRow(int tableIndex) {  
  472.   Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();  
  473.   // 要填充的表格  
  474.   Dispatch table = Dispatch.call(tables, "Item"new Variant(tableIndex))  
  475.     .toDispatch();  
  476.   // 表格的所有行  
  477.   Dispatch rows = Dispatch.get(table, "Rows").toDispatch();  
  478.   Dispatch.call(rows, "Add");  
  479.  }   
  480.   
  481.  /** 
  482.   * 增加一列 
  483.   *  
  484.   * @param tableIndex 
  485.   *            word文档中的第N张表(从1开始) 
  486.   */  
  487.  public void addCol(int tableIndex) {  
  488.   // 所有表格  
  489.   Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();  
  490.   // 要填充的表格  
  491.   Dispatch table = Dispatch.call(tables, "Item"new Variant(tableIndex))  
  492.     .toDispatch();  
  493.   // 表格的所有行  
  494.   Dispatch cols = Dispatch.get(table, "Columns").toDispatch();  
  495.   Dispatch.call(cols, "Add").toDispatch();  
  496.   Dispatch.call(cols, "AutoFit");  
  497.  }   
  498.   
  499.  /** 
  500.   * 在指定列前面增加表格的列 
  501.   *  
  502.   * @param tableIndex 
  503.   *            word文档中的第N张表(从1开始) 
  504.   * @param colIndex 
  505.   *            制定列的序号 (从1开始) 
  506.   */  
  507.  public void addTableCol(int tableIndex, int colIndex) {  
  508.   // 所有表格  
  509.   Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();  
  510.   // 要填充的表格  
  511.   Dispatch table = Dispatch.call(tables, "Item"new Variant(tableIndex))  
  512.     .toDispatch();  
  513.   // 表格的所有行  
  514.   Dispatch cols = Dispatch.get(table, "Columns").toDispatch();  
  515.   System.out.println(Dispatch.get(cols, "Count"));  
  516.   Dispatch col = Dispatch.call(cols, "Item"new Variant(colIndex))  
  517.     .toDispatch();  
  518.   // Dispatch col = Dispatch.get(cols, "First").toDispatch();  
  519.   Dispatch.call(cols, "Add", col).toDispatch();  
  520.   Dispatch.call(cols, "AutoFit");  
  521.  }   
  522.   
  523.  /** 
  524.   * 在第1列前增加一列 
  525.   *  
  526.   * @param tableIndex 
  527.   *            word文档中的第N张表(从1开始) 
  528.   */  
  529.  public void addFirstTableCol(int tableIndex) {  
  530.   Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();  
  531.   // 要填充的表格  
  532.   Dispatch table = Dispatch.call(tables, "Item"new Variant(tableIndex))  
  533.     .toDispatch();  
  534.   // 表格的所有行  
  535.   Dispatch cols = Dispatch.get(table, "Columns").toDispatch();  
  536.   Dispatch col = Dispatch.get(cols, "First").toDispatch();  
  537.   Dispatch.call(cols, "Add", col).toDispatch();  
  538.   Dispatch.call(cols, "AutoFit");  
  539.  }   
  540.   
  541.  /** 
  542.   * 在最后一列前增加一列 
  543.   *  
  544.   * @param tableIndex 
  545.   *            word文档中的第N张表(从1开始) 
  546.   */  
  547.  public void addLastTableCol(int tableIndex) {  
  548.   Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();  
  549.   // 要填充的表格  
  550.   Dispatch table = Dispatch.call(tables, "Item"new Variant(tableIndex))  
  551.     .toDispatch();  
  552.   // 表格的所有行  
  553.   Dispatch cols = Dispatch.get(table, "Columns").toDispatch();  
  554.   Dispatch col = Dispatch.get(cols, "Last").toDispatch();  
  555.   Dispatch.call(cols, "Add", col).toDispatch();  
  556.   Dispatch.call(cols, "AutoFit");  
  557.  }  
  558.  /** 
  559.   * 自动调整表格 
  560.   * 
  561.   */  
  562.     public void autoFitTable(){  
  563.      Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();  
  564.      int count = Dispatch.get(tables, "Count").toInt();  
  565.      for(int i=0;i<count;i++){  
  566.       Dispatch table = Dispatch.call(tables, "Item"new Variant(i+1))  
  567.    .toDispatch();  
  568.       Dispatch cols = Dispatch.get(table, "Columns").toDispatch();  
  569.       Dispatch.call(cols, "AutoFit");  
  570.      }  
  571.     }  
  572.     /** 
  573.      * 调用word里的宏以调整表格的宽度,其中宏保存在document下 
  574.      * 
  575.      */  
  576.     public void callWordMacro(){  
  577.      Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();  
  578.      int count = Dispatch.get(tables, "Count").toInt();  
  579.      Variant vMacroName = new Variant("Normal.NewMacros.tableFit");  
  580.      Variant vParam = new Variant("param1");  
  581.      Variant para[]=new Variant[]{vMacroName};  
  582.      for(int i=0;i<count;i++){       Dispatch table = Dispatch.call(tables, "Item"new Variant(i+1))  
  583.    .toDispatch();  
  584.       Dispatch.call(table, "Select");  
  585.       Dispatch.call(word,"Run","tableFitContent");  
  586.      }   
  587.     }  
  588.  /** 
  589.   * 设置当前选定内容的字体 
  590.   *  
  591.   * @param boldSize 
  592.   * @param italicSize 
  593.   * @param underLineSize 
  594.   *            下划线 
  595.   * @param colorSize 
  596.   *            字体颜色 
  597.   * @param size 
  598.   *            字体大小 
  599.   * @param name 
  600.   *            字体名称 
  601.   */  
  602.  public void setFont(boolean bold, boolean italic, boolean underLine,  
  603.    String colorSize, String size, String name) {  
  604.   Dispatch font = Dispatch.get(selection, "Font").toDispatch();  
  605.   Dispatch.put(font, "Name"new Variant(name));  
  606.   Dispatch.put(font, "Bold"new Variant(bold));  
  607.   Dispatch.put(font, "Italic"new Variant(italic));  
  608.   Dispatch.put(font, "Underline"new Variant(underLine));  
  609.   Dispatch.put(font, "Color", colorSize);  
  610.   Dispatch.put(font, "Size", size);  
  611.  }   
  612.   
  613.  /** 
  614.   * 文件保存或另存为 
  615.   *  
  616.   * @param savePath 
  617.   *            保存或另存为路径 
  618.   */  
  619.  public void save(String savePath) {  
  620.   Dispatch.call(Dispatch.call(word, "WordBasic").getDispatch(),  
  621.     "FileSaveAs", savePath);  
  622.  }   
  623.   
  624.  /** 
  625.   * 关闭文档 
  626.   *@param val 0不保存修改 -1 保存修改 -2 提示是否保存修改 
  627.   */  
  628.  public void closeDocument(int val) {  
  629.   Dispatch.call(doc, "Close"new Variant(val));  
  630.   doc = null;  
  631.  }  
  632.  /** 
  633.   * 关闭当前word文档 
  634.   *  
  635.   */  
  636.  public void closeDocument() {  
  637.   if (doc != null) {  
  638.    Dispatch.call(doc, "Save");  
  639.    Dispatch.call(doc, "Close"new Variant(saveOnExit));  
  640.    doc = null;  
  641.   }  
  642.  }   
  643.   
  644.  /** 
  645.   * 关闭全部应用 
  646.   *  
  647.   */  
  648.  public void close() {  
  649.   //closeDocument();  
  650.   if (word != null) {  
  651.    Dispatch.call(word, "Quit");  
  652.    word = null;  
  653.   }  
  654.   selection = null;  
  655.   documents = null;  
  656.  }   
  657.   
  658.  /** 
  659.   * 打印当前word文档 
  660.   *  
  661.   */  
  662.  public void printFile() {  
  663.   if (doc != null) {  
  664.    Dispatch.call(doc, "PrintOut");  
  665.   }  
  666.  }  
  667.   
  668. //读取表格指定单元格中的内容  
  669.  public String getContent(int tableIndex, int cellRowIdx, int cellColIdx){  
  670.      // 所有表格  
  671.      Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();  
  672.      // 要填充的表格  
  673.      Dispatch table = Dispatch.call(tables, "Item"new Variant(tableIndex))  
  674.        .toDispatch();  
  675.      Dispatch cell = Dispatch.call(table, "Cell"new Variant(cellRowIdx),  
  676.        new Variant(cellColIdx)).toDispatch();  
  677.      Dispatch Range=Dispatch.get(cell,"Range").toDispatch();   
  678.      return Dispatch.get(Range,"Text").toString();  
  679.        
  680.  }  
  681.   
  682.  public static void main(String args[]) {  
  683.   WordBean msWordManager = new WordBean();  
  684.   try {  
  685.   /* 
  686.    创建 
  687.     msWordManager.createNewDocument();  
  688.    msWordManager.createTable("", 5, 5); 
  689.    msWordManager.mergeCell(1, 1, 1, 1, 5); 
  690.    msWordManager.mergeCell(1, 2, 1, 2, 5); 
  691.    msWordManager.mergeCell(1, 3, 1, 3, 5); 
  692.    msWordManager.putTxtToCell(1,1,1,"主题"); 
  693.    msWordManager.putTxtToCell(1,2,1,"时间"); 
  694.    msWordManager.putTxtToCell(1,3,1,"人员"); 
  695.    msWordManager.putTxtToCell(1,4,2,"说话了"); 
  696.    msWordManager.save("c:\\asdf1.doc");*/  
  697.    msWordManager.openDocument("c:\\asdf1.doc");  
  698.    msWordManager.addRow(1);  
  699.    msWordManager.putTxtToCell(141"gaga");  
  700.   } catch (Exception e)  
  701.    e.printStackTrace();  
  702.   }finally{  
  703.    msWordManager.close();  
  704.   }  
  705.  }   
  706.   
  707. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics