jacob操作API

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

// 移到开头
Dispatch.call(selection, "HomeKey", new Variant(6));

 

原创粉丝点击