用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 {
- // word文档
- private Dispatch doc = null ;
- // word运行程序对象
- private ActiveXComponent word = null ;
- // 所有word文档集合
- private Dispatch documents = null ;
- // 选定的范围或插入点
- private static Dispatch selection = null ;
- // 设置是否保存后才退出的标志
- private boolean saveOnExit = true ;
- /**
- * word中的当前文档
- */
- private static Dispatch document = null ;
- // private static Dispatch textFrame = null;
- //
- /**
- * 所有表格
- */
- private Dispatch tables;
- /**
- * 当前表格
- */
- private Dispatch table;
- /**
- * 当前单元格
- */
- private Dispatch cell;
- /**
- * 当前表格中的所有行
- */
- private Dispatch rows;
- /**
- * 表格中的某一行
- */
- private Dispatch row;
- /**
- * 表格中的所有列
- */
- private Dispatch columns;
- /**
- * 表格中的某一列
- */
- private Dispatch column;
- /**
- * 打开word时同时要打开的文档,不指定时将新建一个空白文档
- */
- // private File openDoc;
- 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;
- // constructor
- public StudyJacob() {
- if (word == null ) {
- word = new ActiveXComponent( "Word.Application" );
- word.setProperty( "Visible" , new Variant( true ));
- }
- if (documents == null ) {
- documents = word.getProperty( "Documents" ).toDispatch();
- }
- }
- /**
- * 创建一个新的word文档
- *
- */
- public void createNewDocument() {
- doc = Dispatch.call(documents, "Add" ).toDispatch();
- selection = Dispatch.get(word, "Selection" ).toDispatch();
- }
- /**
- * 打开一个已存在的文档
- *
- * @param docPath
- */
- public void openDocument(String docPath) {
- if ( this .doc != null ) {
- this .closeDocument();
- }
- doc = Dispatch.call(documents, "Open" , docPath).toDispatch();
- selection = Dispatch.get(word, "Selection" ).toDispatch();
- }
- /**
- * 关闭当前word文档
- *
- */
- 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 ));
- }
- /**
- * 在当前插入点插入字符串
- *
- * @param newText
- * 要插入的新字符串
- */
- public void insertText(String newText) {
- Dispatch.put(selection, "Text" , newText);
- }
- /**
- * 在当前插入点插入图片
- *
- * @param imagePath
- * 图片路径
- */
- public void insertImage(String imagePath) {
- Dispatch.call(Dispatch.get(selection, "InLineShapes" ).toDispatch(),
- "AddPicture" , imagePath);
- }
- /**
- * 把选定的内容或者插入点向下移动
- *
- * @param pos
- * 移动的距离
- */
- 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" );
- }
- /**
- * 把选定的内容或插入点向上移动
- *
- * @param pos
- * 移动的距离
- */
- 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" );
- }
- /**
- * 把选定的内容或者插入点向左移动
- *
- * @param pos
- * 移动的距离
- */
- 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" );
- }
- }
- /**
- * 把选定的内容或者插入点向右移动
- *
- * @param pos
- * 移动的距离
- */
- 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" );
- }
- /**
- * 文件保存或另存为
- *
- * @param savePath
- * 一定要记得加上扩展名 .doc 保存或另存为路径
- */
- public void save(String savePath) {
- Dispatch.call(
- (Dispatch) Dispatch.call(word, "WordBasic" ).getDispatch(),
- "FileSaveAs" , savePath);
- }
- /**
- * 从第tIndex个Table中取出值第row行,第col列的值
- *
- * @param tableIndex
- * 文档中的第tIndex个Table,即tIndex为索引取
- * @param cellRowIdx
- * cell在Table第row行
- * @param cellColIdx
- * cell在Talbe第col列
- * @return cell单元值
- * @throws Exception
- */
- 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();
- }
- /**
- * 从第tableIndex个Table中取出值第cellRowIdx行,第cellColIdx列的值
- *
- * @param tIndex
- * 文档中的第tIndex个Table,即tIndex为索引取
- * @param cellRowIdx
- * cell在Table第row行
- * @param cellColIdx
- * cell在Talbe第col列
- * @return cell单元值
- * @throws Exception
- */
- 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" );
- }
- /**
- * 在当前光标处添加图片
- *
- * @param imgPath
- * 图片的地址
- */
- public void addImage(String imgPath) {
- if (imgPath != "" && imgPath != null ) {
- Dispatch image = Dispatch.get(selection, "InLineShapes" )
- .toDispatch();
- Dispatch.call(image, "AddPicture" , imgPath);
- }
- }
- /**
- * 在指定的单元格里填写数据
- *