C#操作Word完全方法

 
前提:
导入COM库:Microsoft word 11.0 Object Library.
引用里面就增加了

创建新Word
1  object  oMissing =  System.Reflection.Missing.Value;
2  Word._Application oWord;
3  Word._Document oDoc;
4  oWord =   new  Word.Application();
5  oWord.Visible =   true ;
6  oDoc =  oWord.Documents.Add( ref  oMissing, ref  oMissing,
7       ref  oMissing, ref  oMissing);
8 

打开文档:
 1  object  oMissing =  System.Reflection.Missing.Value;
 2  Word._Application oWord;
 3  Word._Document oDoc;
 4  oWord =   new  Word.Application();
 5  oWord.Visible =   true ;
 6  object  fileName =   @" E:CCCXCXXTestDoc.doc " ;
 7  oDoc =  oWord.Documents.Open( ref  fileName,
 8  ref  oMissing, ref  oMissing, ref  oMissing, ref  oMissing, ref  oMissing,
 9  ref  oMissing, ref  oMissing, ref  oMissing, ref  oMissing, ref  oMissing,
10  ref  oMissing, ref  oMissing, ref  oMissing, ref  oMissing, ref  oMissing);
11 

导入模板
1  object  oMissing =  System.Reflection.Missing.Value;
2  Word._Application oWord;
3  Word._Document oDoc;
4  oWord =   new  Word.Application();
5  oWord.Visible =   true ;
6  object  fileName =   @" E:XXXCCXTest.doc " ;
7  oDoc =  oWord.Documents.Add( ref  fileName, ref  oMissing,
8                   ref  oMissing, ref  oMissing);
9 

.添加新表
 1  object  oMissing =  System.Reflection.Missing.Value;
 2  Word._Application oWord;
 3  Word._Document oDoc;
 4  oWord =   new  Word.Application();
 5  oWord.Visible =   true ;
 6  oDoc =  oWord.Documents.Add( ref  oMissing, ref  oMissing,
 7       ref  oMissing, ref  oMissing);
 8 
 9  object  start =   0 ;
10  object  end =   0 ;
11  Word.Range tableLocation =  oDoc.Range( ref  start, ref  end);
12  oDoc.Tables.Add(tableLocation, 3 , 4 , ref  oMissing, ref  oMissing);
13 

.表插入行
 1  object  oMissing =  System.Reflection.Missing.Value;
 2  Word._Application oWord;
 3  Word._Document oDoc;
 4  oWord =   new  Word.Application();
 5  oWord.Visible =   true ;
 6  oDoc =  oWord.Documents.Add( ref  oMissing, ref  oMissing,
 7       ref  oMissing, ref  oMissing);
 8 
 9  object  start =   0 ;
10  object  end =   0 ;
11  Word.Range tableLocation =  oDoc.Range( ref  start, ref  end);
12  oDoc.Tables.Add(tableLocation, 3 , 4 , ref  oMissing, ref  oMissing);
13 
14  Word.Table newTable =  oDoc.Tables[ 1 ];
15  object  beforeRow =  newTable.Rows[ 1 ];
16  newTable.Rows.Add( ref  beforeRow);
17 

.单元格合并
 1  object  oMissing =  System.Reflection.Missing.Value;
 2  Word._Application oWord;
 3  Word._Document oDoc;
 4  oWord =   new  Word.Application();
 5  oWord.Visible =   true ;
 6  oDoc =  oWord.Documents.Add( ref  oMissing, ref  oMissing,
 7       ref  oMissing, ref  oMissing);
 8 
 9  object  start =   0 ;
10  object  end =   0 ;
11  Word.Range tableLocation =  oDoc.Range( ref  start, ref  end);
12  oDoc.Tables.Add(tableLocation, 3 , 4 , ref  oMissing, ref  oMissing);
13 
14  Word.Table newTable =  oDoc.Tables[ 1 ];
15  object  beforeRow =  newTable.Rows[ 1 ];
16  newTable.Rows.Add( ref  beforeRow);
17 
18  Word.Cell cell =  newTable.Cell( 1 , 1 );
19  cell.Merge(newTable.Cell( 1 , 2 ));
20 

.单元格分离
 1  object  oMissing =  System.Reflection.Missing.Value;
 2  Word._Application oWord;
 3  Word._Document oDoc;
 4  oWord =   new  Word.Application();
 5  oWord.Visible =   true ;
 6  oDoc =  oWord.Documents.Add( oMissing,
 7       ref  oMissing, ref  oMissing);
 8 
 9  object  start =   0 ;
10  object  end =   0 ;
11  Word.Range tableLocation =  oDoc.Range( ref  start, ref  end);
12  oDoc.Tables.Add(tableLocation, 3 , 4 , ref  oMissing, ref  oMissing);
13 
14  Word.Table newTable =  oDoc.Tables[ 1 ];
15  object  beforeRow =  newTable.Rows[ 1 ];
16  newTable.Rows.Add( ref  beforeRow);
17 
18  Word.Cell cell =  newTable.Cell( 1 , 1 );
19  cell.Merge(newTable.Cell( 1 , 2 ));
20 
21  object  Rownum =   2 ;
22  object  Columnnum =   2 ;
23  cell.Split( ref  Rownum, ref   Columnnum);
24 

通过段落 控制插入
 1  object  oMissing =  System.Reflection.Missing.Value;
 2  object  oEndOfDoc =   " endofdoc " ; /**//*  endofdoc is a predefined bookmark */
 3 
 4  // Start Word and create a new document.
 5  Word._Application oWord;
 6  Word._Document oDoc;
 7  oWord =   new  Word.Application();
 8  oWord.Visible =   true ;
 9  oDoc =  oWord.Documents.Add( ref  oMissing, ref  oMissing,
10       ref  oMissing, ref  oMissing);
11 
12  // Insert a paragraph at the beginning of the document.
13  Word.Paragraph oPara1;
14  oPara1 =  oDoc.Content.Paragraphs.Add( ref  oMissing);
15  oPara1.Range.Text =   " Heading 1 " ;
16  oPara1.Range.Font.Bold =   1 ;
17  oPara1.Format.SpaceAfter =   24 ; // 24 pt spacing after paragraph.
18  oPara1.Range.InsertParagraphAfter();
19 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值