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

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

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

2、           word 文档基本操作

  1 )新建word 文档:

           object oMissing = System.Reflection.Missing .Value;

            Word._Application oWord;

       Word._Document oDoc;

oWord = new Word.Application ();

            oWord.Visible = true ;

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

 

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

          object oMissing = System.Reflection.Missing .Value;

            Word._Application oWord;

       Word._Document oDoc;

oWord = new Word.Application ();

            oWord.Visible = true ;

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

            oDoc = oWord.Documents.Open(ref fileName,   ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,   ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  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 = new Word.Application ();

            oWord.Visible = true ;

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

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

  4 )关闭word

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

      关闭不保存:

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

             // 关闭文档

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

            // 退出word 程序

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

      另存为:

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

              oDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

 

3、           表格操作

(1)       添加表格

           object start = 0;

            object end = 0;

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

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

(2)       表插入行

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

            object beforeRow = newTable.Rows[3];

     newTable.Rows.Add(ref beforeRow);

(3)       表插入列

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

            object beforeColumn = newTable.Columns[1];

     newTable.Columns.Add(ref beforeColumn);

(4)       合并单元格

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

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

(5)       拆分单元格

object Rownum = 4;

           object Columnnum = 2;

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

(6)       填充单元格

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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值