用jacob做了一个东西,就是对word进行操作,开始时费了好大劲,后来总算是有点思路。现将已试验过的方法总结如下。
还有一点就是所用的JAR文件和DLL文件好像比较特殊,JDK换来换去就用了JDK1.6,jacob.jar为1.9的,dll为2005年2月26日的。
有什么问题可以在此留言,大家一起交流。
- import com.jacob.activeX.ActiveXComponent;
- import com.jacob.com.Dispatch;
- import com.jacob.com.Variant;
- import com.jacob.com.ComThread;
- public class StudyJacob {
-
- private Dispatch doc = null ;
-
- private ActiveXComponent word = null ;
-
- private Dispatch documents = null ;
-
- private static Dispatch selection = null ;
-
- private boolean saveOnExit = true ;
-
- private static Dispatch document = null ;
-
-
-
- private Dispatch tables;
-
- private Dispatch table;
-
- private Dispatch cell;
-
- private Dispatch rows;
-
- private Dispatch row;
-
- private Dispatch columns;
-
- private Dispatch column;
-
-
- private static Dispatch shapes;
- private static Dispatch shape;
- private static Dispatch textRange;
- private static Dispatch textframe;
- private Dispatch range;
- private Dispatch paragraphs;
- private Dispatch paragraph;
-
- public StudyJacob() {
- if (word == null ) {
- word = new ActiveXComponent( "Word.Application" );
- word.setProperty( "Visible" , new Variant( true ));
- }
- if (documents == null ) {
- documents = word.getProperty( "Documents" ).toDispatch();
- }
- }
-
- public void createNewDocument() {
- doc = Dispatch.call(documents, "Add" ).toDispatch();
- selection = Dispatch.get(word, "Selection" ).toDispatch();
- }
-
- public void openDocument(String docPath) {
- if ( this .doc != null ) {
- this .closeDocument();
- }
- doc = Dispatch.call(documents, "Open" , docPath).toDispatch();
- selection = Dispatch.get(word, "Selection" ).toDispatch();
- }
-
- public void closeDocument() {
- if (doc != null ) {
- Dispatch.call(doc, "Save" );
- Dispatch.call(doc, "Close" , new Variant(saveOnExit));
- doc = null ;
- }
- }
-
- public void close() {
- closeDocument();
- if (word != null ) {
- Dispatch.call(word, "Quit" );
- word = null ;
- }
- selection = null ;
- documents = null ;
- }
-
- public void moveStart() {
- if (selection == null )
- selection = Dispatch.get(word, "Selection" ).toDispatch();
- Dispatch.call(selection, "HomeKey" , new Variant( 6 ));
- }
-
- public void insertText(String newText) {
- Dispatch.put(selection, "Text" , newText);
- }
-
- public void insertImage(String imagePath) {
- Dispatch.call(Dispatch.get(selection, "InLineShapes" ).toDispatch(),
- "AddPicture" , imagePath);
- }
-
- public void moveDown( int pos) {
- if (selection == null )
- selection = Dispatch.get(word, "Selection" ).toDispatch();
- for ( int i = 0 ; i < pos; i++)
- Dispatch.call(selection, "MoveDown" );
- }
-
- public void moveUp( int pos) {
- if (selection == null )
- selection = Dispatch.get(word, "Selection" ).toDispatch();
- for ( int i = 0 ; i < pos; i++)
- Dispatch.call(selection, "MoveUp" );
- }
-
- public void moveLeft( int pos) {
- if (selection == null )
- selection = Dispatch.get(word, "Selection" ).toDispatch();
- for ( int i = 0 ; i < pos; i++) {
- Dispatch.call(selection, "MoveLeft" );
- }
- }
-
- public void moveRight( int pos) {
- if (selection == null )
- selection = Dispatch.get(word, "Selection" ).toDispatch();
- for ( int i = 0 ; i < pos; i++)
- Dispatch.call(selection, "MoveRight" );
- }
-
- public void save(String savePath) {
- Dispatch.call(
- (Dispatch) Dispatch.call(word, "WordBasic" ).getDispatch(),
- "FileSaveAs" , savePath);
- }
-
- public String getCellString( int tableIndex, int cellRowIdx, int cellColIdx)
- throws Exception {
-
- Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
- .toDispatch();
- Dispatch cell = Dispatch.call(table, "Cell" , new Variant(cellRowIdx),
- new Variant(cellColIdx)).toDispatch();
- Dispatch.call(cell, "Select" );
- return Dispatch.get(selection, "Text" ).toString();
- }
-
- public void getCellValue( int tableIndex, int cellRowIdx, int cellColIdx)
- throws Exception {
-
- Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
- .toDispatch();
- Dispatch cell = Dispatch.call(table, "Cell" , new Variant(cellRowIdx),
- new Variant(cellColIdx)).toDispatch();
- Dispatch.call(cell, "Select" );
- Dispatch.call(selection, "Copy" );
- }
-
- public void paste() {
- Dispatch.call(selection, "Paste" );
- }
-
- public void addImage(String imgPath) {
- if (imgPath != "" && imgPath != null ) {
- Dispatch image = Dispatch.get(selection, "InLineShapes" )
- .toDispatch();
- Dispatch.call(image, "AddPicture" , imgPath);
- }
- }
-
- public void putTxtToCell( int tableIndex, int cellRowIdx, int cellColIdx,
- String txt) {
-
- Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
- .toDispatch();
- Dispatch cell = Dispatch.call(table, "Cell" , new Variant(cellRowIdx),
- new Variant(cellColIdx)).toDispatch();
- Dispatch.call(cell, "Select" );
- Dispatch.put(selection, "Text" , txt);
- }
-
- public Dispatch getTables() throws Exception {
- if ( this .doc == null ) {
- throw new Exception( "there is not a document can‘t be operate!!!" );
- }
- return Dispatch.get(doc, "Tables" ).toDispatch();
- }
-
- public int getTablesCount(Dispatch tables) throws Exception {
- int count = 0 ;
- try {
- this .getTables();
- } catch (Exception e) {
- throw new Exception( "there is not any table!!" );
- }
- count = Dispatch.get(tables, "Count" ).toInt();
- return count;
- }
-
- public void copyTable(String pos, int tableIndex) {
-
- Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
- .toDispatch();
- Dispatch range = Dispatch.get(table, "Range" ).toDispatch();
- Dispatch.call(range, "Copy" );
- Dispatch textRange = Dispatch.get(selection, "Range" ).toDispatch();
- Dispatch.call(textRange, "Paste" );
- }
-
- public void copyTableFromAnotherDoc(String anotherDocPath, int tableIndex,
- String pos) {
- Dispatch doc2 = null ;
- try {
- doc2 = Dispatch.call(documents, "Open" , anotherDocPath)
- .toDispatch();
-
- Dispatch tables = Dispatch.get(doc2, "Tables" ).toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item" ,
- new Variant(tableIndex)).toDispatch();
- Dispatch range = Dispatch.get(table, "Range" ).toDispatch();
- Dispatch.call(range, "Copy" );
- Dispatch textRange = Dispatch.get(selection, "Range" ).toDispatch();
- moveDown( 1 );
- Dispatch.call(textRange, "Paste" );
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (doc2 != null ) {
- Dispatch.call(doc2, "Close" , new Variant(saveOnExit));
- doc2 = null ;
- }
- }
- }
-
- public void pasteExcelSheet(String pos) {
- moveStart();
- Dispatch textRange = Dispatch.get(selection, "Range" ).toDispatch();
- Dispatch.call(textRange, "Paste" );
- }
-
- private void select(Dispatch dispatch) {
- Dispatch.call(dispatch, "Select" );
- }
-
- public void copyImageFromAnotherDoc(String anotherDocPath, int shapeIndex,
- String pos) {
- Dispatch doc2 = null ;
- try {
- doc2 = Dispatch.call(documents, "Open" , anotherDocPath)
- .toDispatch();
- Dispatch shapes = Dispatch.get(doc2, "InLineShapes" ).toDispatch();
- Dispatch shape = Dispatch.call(shapes, "Item" ,
- new Variant(shapeIndex)).toDispatch();
- Dispatch imageRange = Dispatch.get(shape, "Range" ).toDispatch();
- Dispatch.call(imageRange, "Copy" );
- Dispatch textRange = Dispatch.get(selection, "Range" ).toDispatch();
- moveDown( 4 );
- Dispatch.call(textRange, "Paste" );
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (doc2 != null ) {
- Dispatch.call(doc2, "Close" , new Variant(saveOnExit));
- doc2 = null ;
- }
- }
- }
-
- public void printFile() {
- if (doc != null ) {
- Dispatch.call(doc, "PrintOut" );
- }
- }
-
- public void println(String s) {
- write(s);
- goToEnd();
- cmd( "TypeParagraph" );
- }
-
- private void cmd(String cmd) {
- Dispatch.call(selection, cmd);
- }
-
- public void goToEnd() {
- Dispatch.call(selection, "EndKey" , "6" );
- }
-
- public void print(String s) {
- goToEnd();
- write(s);
- }
-
- public void write(String s) {
- Dispatch.put(selection, "Text" , s);
- }
-
- public void println() {
- home();
- end();
- cmd( "TypeParagraph" );
- }
-
- public void home() {
- Dispatch.call(selection, "HomeKey" , "5" );
- }
-
- public void end() {
- Dispatch.call(selection, "EndKey" , "5" );
- }
-
- public void goToBegin() {
- Dispatch.call(selection, "HomeKey" , "6" );
- }
-
- public void setColumnWidth( int tableIndex, float columnWidth,
- int columnIndex) {
- this .getTable(tableIndex);
- this .setColumnWidth(columnWidth, columnIndex);
- }
-
- public Dispatch getTable( int tableIndex) {
- Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
- Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
- .toDispatch();
- return table;
- }
- public Dispatch getShapes() throws Exception {
- return Dispatch.get(doc, "Shapes" ).toDispatch();
- }
- public int getShapesCount() throws Exception {
- int count = 0 ;
- count = Dispatch.get(shapes, "Count" ).toInt();
- return count;
- }
- public Dispatch getShape( int tIndex) throws Exception {
- return Dispatch.call(shapes, "Item" , new Variant(tIndex)).toDispatch();
-
-
- }
- public Dispatch getTextFrame() throws Exception {
- return Dispatch.get(shape, "TextFrame" ).toDispatch();
- }
- public Dispatch getTextRange() throws Exception {
- return Dispatch.get(textframe, "TextRange" ).toDispatch();
- }
-
- public void setColumnWidth( float columnWidth, int columnIndex) {
- if (columnWidth < 11 ) {
- columnWidth = 120 ;
- }
- if (columns == null || column == null ) {
- this .getColumns();
- this .getColumn(columnIndex);
- }
- Dispatch.put(column, "Width" , new Variant(columnWidth));
- }
-
- public void setColumnBgColor( int tableIndex, int columnIndex, int color) {
- this .getTable(tableIndex);
- this .setColumnBgColor(columnIndex, color);
- }
-
- public void setColumnBgColor( int columnIndex, int color) {
- this .getColumn(columnIndex);
- Dispatch shading = Dispatch.get(column, "Shading" ).toDispatch();
- if (color > 16 || color < 1 )
- color = 16 ;
- Dispatch
- .put(shading, "BackgroundPatternColorIndex" , new Variant(color));
- }
-
- public void initCom() {
- ComThread.InitSTA();
- }
-
- public void releaseCom() {
- ComThread.Release();
- }
-
- public void setRowBgColor( int rowIndex, int color) {
- this .getRow(rowIndex);
- Dispatch shading = Dispatch.get(row, "Shading" ).toDispatch();
- if (color > 16 || color < 1 )
- color = 16 ;
- Dispatch
- .put(shading, "BackgroundPatternColorIndex" , new Variant(color));
- }
-
- public void setRowBgColor( int tableIndex, int rowIndex, int color) {
- this .getTable(tableIndex);
- this .setRowBgColor(rowIndex, color);
- }
-
- public void setFont( boolean isBold, boolean isItalic, boolean isUnderLine,
- String color, String size, String name) {
- Dispatch font = Dispatch.get(selection, "Font" ).toDispatch();
- Dispatch.put(font, "Name" , new Variant(name));
- Dispatch.put(font, "Bold" , new Variant(isBold));
- Dispatch.put(font, "Italic" , new Variant(isItalic));
- Dispatch.put(font, "Underline" , new Variant(isUnderLine));
- Dispatch.put(font, "Color" , color);
- Dispatch.put(font, "Size" , size);
- }
-
- public void clearFont() {
- this .setFont( false , false , false , "0,0,0" , "12" , "宋体" );
- }
-
- public void setParaFormat( int align, int lineSpace) {
- if (align < 0 || align > 4 ) {
- align = 0 ;
- }
- if (lineSpace < 0 || lineSpace > 4 ) {
- lineSpace = 0 ;
- }
- Dispatch alignment = Dispatch.get(selection, "ParagraphFormat" )
- .toDispatch();
- Dispatch.put(alignment, "Alignment" , align);
- Dispatch.put(alignment, "LineSpacingRule" , new Variant(lineSpace));
- }
-
- public void clearParaFormat() {
- this .setParaFormat( 0 , 0 );
- }
-
- public void createTable(String pos, int numCols, int numRows) {
- if (find(pos)) {
- Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
- Dispatch range = Dispatch.get(selection, "Range" ).toDispatch();
- Dispatch newTable = Dispatch.call(tables, "Add" , range,
- new Variant(numRows), new Variant(numCols)).toDispatch();
- Dispatch.call(selection, "MoveRight" );
- }
- }
-
- public void addTableRow( int tableIndex, int rowIndex) {
-
- Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
- .toDispatch();
-
- Dispatch rows = Dispatch.get(table, "Rows" ).toDispatch();
- Dispatch row = Dispatch.call(rows, "Item" , new Variant(rowIndex))
- .toDispatch();
- Dispatch.call(rows, "Add" , new Variant(row));
- }
-
- public void addFirstTableRow( int tableIndex) {
-
- Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
- .toDispatch();
-
- Dispatch rows = Dispatch.get(table, "Rows" ).toDispatch();
- Dispatch row = Dispatch.get(rows, "First" ).toDispatch();
- Dispatch.call(rows, "Add" , new Variant(row));
- }
-
- public void addLastTableRow( int tableIndex) {
-
- Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
- .toDispatch();
-
- Dispatch rows = Dispatch.get(table, "Rows" ).toDispatch();
- Dispatch row = Dispatch.get(rows, "Last" ).toDispatch();
- Dispatch.call(rows, "Add" , new Variant(row));
- }
-
- public void addRow( int tableIndex) {
- Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
- .toDispatch();
-
- Dispatch rows = Dispatch.get(table, "Rows" ).toDispatch();
- Dispatch.call(rows, "Add" );
- }
-
- public void addCol( int tableIndex) {
-
- Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
- .toDispatch();
-
- Dispatch cols = Dispatch.get(table, "Columns" ).toDispatch();
- Dispatch.call(cols, "Add" ).toDispatch();
- Dispatch.call(cols, "AutoFit" );
- }
-
- public void addTableCol( int tableIndex, int colIndex) {
-
- Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
- .toDispatch();
-
- Dispatch cols = Dispatch.get(table, "Columns" ).toDispatch();
- System.out.println(Dispatch.get(cols, "Count" ));
- Dispatch col = Dispatch.call(cols, "Item" , new Variant(colIndex))
- .toDispatch();
-
- Dispatch.call(cols, "Add" , col).toDispatch();
- Dispatch.call(cols, "AutoFit" );
- }
-
- public void addFirstTableCol( int tableIndex) {
- Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
- .toDispatch();
-
- Dispatch cols = Dispatch.get(table, "Columns" ).toDispatch();
- Dispatch col = Dispatch.get(cols, "First" ).toDispatch();
- Dispatch.call(cols, "Add" , col).toDispatch();
- Dispatch.call(cols, "AutoFit" );
- }
-
- public void addLastTableCol( int tableIndex) {
- Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
-
- Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
- .toDispatch();
-
- Dispatch cols = Dispatch.get(table, "Columns" ).toDispatch();
- Dispatch col = Dispatch.get(cols, "Last" ).toDispatch();
- Dispatch.call(cols, "Add" , col).toDispatch();
- Dispatch.call(cols, "AutoFit" );
- }
-
- public boolean find(String toFindText) {
- if (toFindText == null || toFindText.equals( "" ))
- return false ;
-
- Dispatch find = word.call(selection, "Find" ).toDispatch();
-
- Dispatch.put(find, "Text" , toFindText);
-
- Dispatch.put(find, "Forward" , "True" );
-
- Dispatch.put(find, "Format" , "True" );
-
- Dispatch.put(find, "MatchCase" , "True" );
-
- Dispatch.put(find, "MatchWholeWord" , "True" );
-
- return Dispatch.call(find, "Execute" ).getBoolean();
- }
-
- public boolean replaceText(String toFindText, String newText) {
- if (!find(toFindText))
- return false ;
- Dispatch.put(selection, "Text" , newText);
- return true ;
- }
- %E
- public void replaceAllText(String toFindText, String newText) {
- while (find(toFindText)) {
- Dispatch.put(selection, "Text" , newText);
- Dispatch.call(selection, "MoveRight" );
- }
- }
-
- public boolean replaceImage(String toFindText, String imagePath) {
- if (!find(toFindText))
- return false ;
- Dispatch.call(Dispatch.get(selection, "InLineShapes" ).toDispatch(),
- "AddPicture" , imagePath);
- return true ;
- }
-
- public void replaceAllImage(String toFindText, String imagePath) {
- while (find(toFindText)) {
- Dispatch.call(Dispatch.get(selection, "InLineShapes" ).toDispatch(),
- "AddPicture" , imagePath);
- Dispatch.call(selection, "MoveRight" );
- }
- }
-
-
-
-
-
-
-
-
-
-
- public Dispatch getRow( int rowIndex) {
- if (rows == null )
- this .getRows();
- row = Dispatch.invoke(rows, "item" , Dispatch.Method,
- new Object[] { new Integer(rowIndex) }, new int [ 1 ])
- .toDispatch();
- return row;
- }
- public int getRowsCount() {
- if (rows == null )
- this .getRows();
- return Dispatch.get(rows, "Count" ).getInt();
- }
-
-
- public Dispatch getColumns() {
-
- Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
- Dispatch table = Dispatch.call(tables, "Item" , new Variant( 1 ))
- .toDispatch();
- return this .columns = Dispatch.get(table, "Columns" ).toDispatch();
- }
-
- public Dispatch getColumn( int columnIndex) {
- if (columns == null )
- this .getColumns();
- return this .column = Dispatch.call(columns, "Item" ,
- new Variant(columnIndex)).toDispatch();
- }
-
- public int getColumnsCount() {
- this .getColumns();
- return Dispatch.get(columns, "Count" ).toInt();
- }
-
- public int getColumnsCount( int tableIndex) {
- this .getTable(tableIndex);
- return this .getColumnsCount();
- }
-
- public int getRowsCount( int tableIndex) {
- this .getTable(tableIndex);
- return this .getRowsCount();
- }
-
- public void setRowHeight( float rowHeight) {
- if (rowHeight > 0 ) {
- if (rows == null )
- this .getRows();
- Dispatch.put(rows, "Height" , new Variant(rowHeight));
- }
- }
-
- public void setRowHeight( int tableIndex, float rowHeight) {
- this .getRows(tableIndex);
- this .setRowHeight(rowHeight);
- }
-
- public void setRowHeight( float rowHeight, int rowIndex) {
- if (rowHeight > 0 ) {
- if (rows == null || row == null ) {
- this .getRows();
- this .getRow(rowIndex);
- }
- Dispatch.put(row, "Height" , new Variant(rowHeight));
- }
- }
-
- public void setRowHeight( int tableIndex, float rowHeight, int rowIndex) {
- this .getTable(tableIndex);
- this .setRowHeight(rowHeight, rowIndex);
- }
-
- public void setColumnWidth( float columnWidth) {
- if (columnWidth < 11 ) {
- columnWidth = 120 ;
- }
- if (columns == null )
- this .getColumns();
- Dispatch.put(columns, "Width" , new Variant(columnWidth));
- }
-
- public void setColumnWidth( int tableIndex, float columnWidth) {
- this .getColumns(tableIndex);
- this .setColumnWidth(columnWidth);
- }
-
- public Dispatch getColumns( int tableIndex) {
- getTable(tableIndex);
- return this .getColumns();
- }
-
- public void copyRow( int tableIndex, int rowIndex) {
- getTable(tableIndex);
- getRows();
- row = getRow(rowIndex);
- Dispatch.call(row, "Select" );
- Dispatch.call(selection, "Copy" );
- }
-
- public Dispatch getRows( int tableIndex) {
- getTable(tableIndex);
- return this .getRows();
- }
-
-
- public Dispatch getRows() {
- Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
- Dispatch table = Dispatch.call(tables, "Item" , new Variant( 2 ))
- .toDispatch();
- rows = Dispatch.get(table, "rows" ).toDispatch();
- return rows;
- }
-
- public Dispatch getCell( int tableIndex, int cellRow, int cellColumn) {
- getTable(tableIndex);
- return getCell(cellRow, cellColumn);
- }
-
- public Dispatch getCell( int cellRow, int cellColumn) {
- return cell = Dispatch.call(table, "Cell" , new Variant(cellRow),
- new Variant(cellColumn)).toDispatch();
- }
-
-
-
- public void mergeCell( int fstCellRowIndex, int fstCellColIndex,
- int secCellRowIndex, int secCellColIndex) {
- Dispatch fstCell = Dispatch.call(table, "Cell" ,
- new Variant(fstCellRowIndex), new Variant(fstCellColIndex))
- .toDispatch();
- Dispatch secCell = Dispatch.call(table, "Cell" ,
- new Variant(secCellRowIndex), new Variant(secCellColIndex))
- .toDispatch();
- Dispatch.call(fstCell, "Merge" , secCell);
- }
-
- public void mergeColumn( int columnIndex) {
- this .getColumn(columnIndex);
- Dispatch cells = Dispatch.get(column, "Cells" ).toDispatch();
- Dispatch.call(cells, "Merge" );
- }
-
- public void mergeRow( int rowIndex) {
- this .getRow(rowIndex);
- Dispatch cells = Dispatch.get(row, "Cells" ).toDispatch();
- Dispatch.call(cells, "Merge" );
- }
-
- public void mergeRow( int tableIndex, int rowIndex) {
- this .getTable(tableIndex);
- this .mergeRow(rowIndex);
- }
-
- public void mergeColumn( int tableIndex, int columnIndex) {
- this .getTable(tableIndex);
- this .mergeColumn(columnIndex);
- }
-
- public void mergeCell( int tableIndex, int fstCellRowIndex,
- int fstCellColIndex, int secCellRowIndex, int secCellColIndex) {
- this .getTable(tableIndex);
- this .mergeCell(fstCellRowIndex, fstCellColIndex, secCellRowIndex,
- secCellColIndex);
- }
- public Dispatch getRangeParagraphs() throws Exception {
- return Dispatch.get(range, "Paragraphs" ).toDispatch();
- }
- public Dispatch getParagraph( int tIndex) throws Exception {
- return Dispatch.call(paragraphs, "Item" , new Variant(tIndex))
- .toDispatch();
- }
- public Dispatch getParagraphRange() throws Exception {
- return Dispatch.get(paragraph, "range" ).toDispatch();
- }
- public String getRangeText() throws Exception {
- return Dispatch.get(range, "Text" ).toString();
- }
- public int getParagraphsCount() throws Exception {
- int count = 0 ;
- count = Dispatch.get(paragraphs, "Count" ).toInt();
- return count;
- }
- public static void main(String[] args) {
- long time1 = System.currentTimeMillis();
- int i = 0 ;
- ComThread.InitSTA();
-
- ActiveXComponent objWord = new ActiveXComponent( "Word.Application" );
-
- Dispatch wordObject = (Dispatch) objWord.getObject();
-
- Dispatch.put((Dispatch) wordObject, "Visible" , new Variant( true ));
-
-
- Dispatch documents = objWord.getProperty( "Documents" ).toDispatch();
-
-
-
- for ( int n = 0 ; n <= 10 ; n++) {
- Dispatch document = Dispatch.call(documents, "Open" , "C://ABC.doc" )
- .toDispatch();
- Dispatch wordContent = Dispatch.get(document, "Content" )
- .toDispatch();
- Dispatch.call(wordContent, "InsertAfter" , "这里是一个段落的内容" );
- Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs" )
- .toDispatch();
- int paragraphCount = Dispatch.get(paragraphs, "Count" ).toInt();
-
- Dispatch lastParagraph = Dispatch.call(paragraphs, "Item" ,
- new Variant(paragraphCount)).toDispatch();
- Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range" )
- .toDispatch();
- Dispatch font = Dispatch.get(lastParagraphRange, "Font" )
- .toDispatch();
- Dispatch.put(font, "Bold" , new Variant( true ));
- Dispatch.put(font, "Italic" , new Variant( true ));
- Dispatch.put(font, "Name" , new Variant( "宋体" ));
- Dispatch.put(font, "Size" , new Variant( 18 ));
- Dispatch frames = Dispatch.call(wordContent, "Frames" ).toDispatch();
-
- int frameCount = Dispatch.call(frames, "Count" ).toInt();
- System.out.println( "" + frameCount + n + "/n" );
- Dispatch.call(document, "SaveAs" , new Variant( "C://" + (i++)+ "abc.doc"));
- Dispatch.call(document, "Close" , new Variant( true ));
- }
-
-
- Dispatch.call(objWord, "Quit" );
- ComThread.Release();
- long time2 = System.currentTimeMillis();
- double time3 = (time2 - time1) / 1000.0 ;
- System.out.println( "/n" + time3 + " 秒." );
- }
- }
jacob的使用方法总结
原文:http://www.cnblogs.com/lixue7/p/4164780.html