Word开发中的常用接口,Range,Tables,InlineShapes

来源:互联网 发布:mac装双系统 编辑:程序博客网 时间:2024/05/17 13:11

(一)在Word开发中,Range对象是用的最频繁的对象。获取Range的方法有很多,大多数对象都能返回range对象,例如:

(1)从对象模型中获取Range,例如

          document.Range()

          table.Cell(1,1).Range

(2) 搜索字符,获取找到的字符串的range

     为了搜索字符串,首先需要一个range,通过这个range来获得Find对象,然后进行查找。

     Word.Range myRange = myDocument.Range();

  MessageBox.Show("After:start:" + myRange.Start.ToString() + ";end:" + myRange.End.ToString());  //这时,myRange对象是初始值,其起始和结束位置已经是查找到

      的字符的位置。


            Word.Find f = myRange.Find;
            f.Text = “查找的字符串”;
            f.ClearFormatting();
            object G_Missing = System.Reflection.Missing.Value;
            bool finded = f.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                    ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                    ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                    ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                    );

            MessageBox.Show("After:start:" + myRange.Start.ToString() + ";end:" + myRange.End.ToString());  //这时,myRange对象已经发生了改变,其起始和结束位置已经是查找到的字符的位置。

(3)range的常用操作

void Collapse(ref Object Direction),参数Object可选,表示折叠方向,Can be either of the following WdCollapseDirection constants: wdCollapseEnd or wdCollapseStart. The default value is wdCollapseStart.

例如:

 myRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);表示将range对象的Start属性设置为与End属性一样的数值,即折叠到末尾;

 myRange.Collapse(Word.WdCollapseDirection.wdCollapseStart);表示将range对象的End属性设置为与Start属性一样的数值,即折叠到开始;

如果采用默认值, myRange.Collapse();表示将range对象的End属性设置为与Start属性一样的数值,即折叠到开始;

(二)Tables对象

(1)Tables对象是Table的集合,从1开始计数,不是C或C#语言常见集合的从0开始计数的那种。不能用Tables[0],否则会出现异常出错。

(2)根据range对象获取Cells或Cell对象

          range接口具有Cells属性,可以利用Cells属性来完成一些操作,比如通过Find搜索找到相应的Range,然后利用range来获取相应的Cell。通过range.Cells,然后可以选择集合中的第一个或遍历的方式完成后续操作。

(3)利用Cell获取其行或列位置

        Cell.RowIndex或Cell.ColumnIndex

(4)在表格Table中查找到对应文字的Cell

          方法一:用佛reach循环遍历

                Word.Cells theCells = table.Range.Cells;
                foreach(Word.Cell c in theCells)
                {
                   string t = c.Range.Text.Trim(new Char[] {'\r','\t','\n','\a',' ' });
                   string findText = "查找的文字”;
                   if(t==indext)
                    {
                       MessageBox.Show(“columnIndex=”+c.ColumnIndex.ToString() + ":RowIndex=" + c.RowIndex.ToString());
                      break;
                    }
                }

         方法二:利用LINQ找到Cell

                string findText = "查找的文字”;
                Word.Cell c = table.Range.Cells.Cast<Word.Cell>().Where<Word.Cell>(x=>x.Range.Text.Trim(new Char[] { '\r', '\t', '\n', '\a', ' ' })==findText).FirstOrDefault();               
                if(c!=null)
                    MessageBox.Show(c.ColumnIndex.ToString() + ":" + c.RowIndex.ToString());

     说明:有Cells获取Cell集合的方式是用Cells.Cast<Word.Cell>(),而无法用Cells直接调用Where<Word.Cell>(),其它集合比如table.Rows,Tables等等也是一样的需要cast转换;当然,如果仅仅提取Count,是不需要转换;  此外,从表格单元格中直接提取的文本字符的末尾是包含"\r\a“的,因此需要先去掉这样的字符。用字符串的Trim()方法时,如果仅仅使用无参数的默认方法时,是不会去掉'\a'字符的,所以需要调用含有char[]参数的Trim方法。此处写了Trim(new Char[] { '\r', '\t', '\n', '\a', ' ' }),这里面char[]包含了多个符号。实际上可能不需要这么多,不知道char[]存在冗余字符是否会影响性能,由于时间关系此处未做测试。

(5) 在表格中增加一行

     table.Rows.Add();

    如果增加一列,则用table.Columns.Add().

   如果要在中间插入,则调用带有参数的Add方法。

(三)InlineShape

          插入照片,则先获得InlineShapes对象,用AddPicture方法插入,方法第一个参数filePath,第四个参数为插入位置的Range。注意,第四个参数对应的range包含其他如文字之类的,则插入点在这个range的开始位置,不会删除原range中的内容。

                  myRange.InlineShapes.AddPicture(filePath, G_Missing, G_Missing, myRange);