C#中的word编程-基本操作(1)

来源:互联网 发布:windows tmg ipsec 编辑:程序博客网 时间:2024/06/07 07:13

1、          运行环境:Windows XPVisual Studio.NET 2005Office 2003

      在菜单栏选择项目添加引用,弹出的窗口中我们可以选择“COM”选项卡,导入COM库:Microsoft word 11.0 Object Library.

2、          word文档基本操作

  1)新建word文档:

          objectoMissing = System.Reflection.Missing.Value;

            Word._Application oWord;

       Word._Document oDoc;

oWord = newWord.Application();

            oWord.Visible = true;

       oDoc = oWord.Documents.Add(ref oMissing, refoMissing, ref oMissing, ref oMissing);

 

函数原型:oWord.Documents.Add(ref object Template, ref objectNewTemplate, ref object DocumentType, ref object Visible)
2)打开word文档:

          object oMissing = System.Reflection.Missing.Value;

            Word._Application oWord;

       Word._Document oDoc;

oWord = newWord.Application();

            oWord.Visible = true;

           object fileName =@"D:/a.doc"; 

            oDoc = oWord.Documents.Open(reffileName,   ref oMissing, ref oMissing,ref oMissing, ref oMissing, ref oMissing,  ref oMissing, ref oMissing, ref oMissing, ref oMissing, refoMissing,  ref oMissing, ref oMissing,ref oMissing, ref oMissing, ref oMissing);

 

           函数原型:oWord.Documents.Open (ref object fileName, ref object ConfirmConversions,

 ref object ReadOnly, ref object AddToRecentFiles, ref object PasswordDocument,

ref object PasswordTemplate, ref object Revert, ref object WritePasswordDocument,

 ref object WritePasswordTemplate,   ref object Format, ref object Encoding,

 ref object Visible, ref object OpenAndRepair, ref object DocumentDirection,

              ref object NoEncodingDialog, ref object XMLTransform);

 

 3)导入word模板:

object oMissing = System.Reflection.Missing.Value;

            Word._Application oWord;

       Word._Document oDoc;

oWord = newWord.Application();

            oWord.Visible = true;

           object fileName =@"D:/a.doc";

           oDoc = oWord.Documents.Add(reffileName, ref oMissing,  ref oMissing,ref oMissing);

 4)关闭word

     关闭已打开的直接用: oDoc.Save();

      关闭不保存:

         object SaveChanges = false;//是否保存更改 true:保存false:不保存

            //关闭文档

           oDoc.Close(ref SaveChanges, refoMissing, ref oMissing);

            //退出word程序

       oWord.Quit(refSaveChanges, ref oMissing, ref oMissing);

      另存为:

object filename = "C://Documentsand Settings//Administrator//桌面//a.doc";

           oDoc.SaveAs(reffilename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, refoMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, refoMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, refoMissing);

 

3、          表格操作

(1)      添加表格

         object start = 0;

            objectend = 0;

            Word.Range tableLocation = oDoc.Range(ref start, ref end);

           oDoc.Tables.Add(tableLocation, 3, 4, refoMissing, ref oMissing);

(2)      表插入行

          Word.TablenewTable = oDoc.Tables[1];  //把文档对象的第一个表格赋给newTable

            objectbeforeRow = newTable.Rows[3];

     newTable.Rows.Add(ref beforeRow);

(3)      表插入列

          Word.TablenewTable = oDoc.Tables[1];  //把文档对象的第一个表格赋给newTable

            objectbeforeColumn = newTable.Columns[1];

     newTable.Columns.Add(ref beforeColumn);

(4)      合并单元格

         Word.Cellcell = newTable.Cell(2, 2);  //Cell(int row,int column)第几行几列,选中单元格赋给cell

    cell.Merge(newTable.Cell(3,2));    //合并cellCell(3,2),即32列的单元格

(5)      拆分单元格

object Rownum = 4;

           objectColumnnum = 2;

cell.Split(refRownum, ref  Columnnum);  //cell拆分成RownumColumnnum

(6)      填充单元格

newTable.Cell(2, 1).Range.Text = "要填充的内容";