C#操作Word完全功略!

Reference:
http://msdn2.microsoft.com/zh-cn/library/kw65a0we(VS.80).aspx
http://support.microsoft.com/kb/316384/zh-cn#top

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



创建新Word

None.gif              object  oMissing  =  System.Reflection.Missing.Value;
None.gif            Word._Application oWord;
None.gif            Word._Document oDoc;
None.gif            oWord 
=   new  Word.Application();
None.gif            oWord.Visible 
=   true ;
None.gif            oDoc 
=  oWord.Documents.Add( ref  oMissing,  ref  oMissing,
None.gif                
ref  oMissing,  ref  oMissing);

打开文档:
None.gif              object  oMissing  =  System.Reflection.Missing.Value;
None.gif            Word._Application oWord;
None.gif            Word._Document oDoc;
None.gif            oWord 
=   new  Word.Application();
None.gif            oWord.Visible 
=   true ;
None.gif            
object  fileName  =   @" E:\CCCXCXX\TestDoc.doc " ;
None.gif            oDoc 
=  oWord.Documents.Open( ref  fileName,
None.gif            
ref  oMissing,  ref  oMissing,  ref  oMissing,  ref  oMissing,  ref  oMissing,
None.gif            
ref  oMissing,  ref  oMissing,  ref  oMissing,  ref  oMissing,  ref  oMissing,
None.gif            
ref  oMissing,  ref  oMissing,  ref  oMissing,  ref  oMissing,  ref  oMissing);

导入模板
None.gif              object  oMissing  =  System.Reflection.Missing.Value;
None.gif            Word._Application oWord;
None.gif            Word._Document oDoc;
None.gif            oWord 
=   new  Word.Application();
None.gif            oWord.Visible 
=   true ;
None.gif            
object  fileName  =   @" E:\XXXCCX\Test.doc " ;
None.gif            oDoc 
=  oWord.Documents.Add( ref  fileName,  ref  oMissing,
None.gif                            
ref  oMissing,  ref  oMissing);


.添加新表
None.gif              object  oMissing  =  System.Reflection.Missing.Value;
None.gif            Word._Application oWord;
None.gif            Word._Document oDoc;
None.gif            oWord 
=   new  Word.Application();
None.gif            oWord.Visible 
=   true ;
None.gif            oDoc 
=  oWord.Documents.Add( ref  oMissing,  ref  oMissing,
None.gif                
ref  oMissing,  ref  oMissing);
None.gif
None.gif            
object  start  =   0 ;
None.gif            
object  end  =   0 ;
None.gif            Word.Range tableLocation 
=  oDoc.Range( ref  start,  ref  end);
None.gif            oDoc.Tables.Add(tableLocation, 
3 4 ref  oMissing,  ref  oMissing);

.表插入行
None.gif              object  oMissing  =  System.Reflection.Missing.Value;
None.gif            Word._Application oWord;
None.gif            Word._Document oDoc;
None.gif            oWord 
=   new  Word.Application();
None.gif            oWord.Visible 
=   true ;
None.gif            oDoc 
=  oWord.Documents.Add( ref  oMissing,  ref  oMissing,
None.gif                
ref  oMissing,  ref  oMissing);
None.gif
None.gif            
object  start  =   0 ;
None.gif            
object  end  =   0 ;
None.gif            Word.Range tableLocation 
=  oDoc.Range( ref  start,  ref  end);
None.gif            oDoc.Tables.Add(tableLocation, 
3 4 ref  oMissing,  ref  oMissing);
None.gif
None.gif            Word.Table newTable 
=  oDoc.Tables[ 1 ];
None.gif            
object  beforeRow  =  newTable.Rows[ 1 ];
None.gif            newTable.Rows.Add(
ref  beforeRow);

.单元格合并
None.gif              object  oMissing  =  System.Reflection.Missing.Value;
None.gif            Word._Application oWord;
None.gif            Word._Document oDoc;
None.gif            oWord 
=   new  Word.Application();
None.gif            oWord.Visible 
=   true ;
None.gif            oDoc 
=  oWord.Documents.Add( ref  oMissing,  ref  oMissing,
None.gif                
ref  oMissing,  ref  oMissing);
None.gif
None.gif            
object  start  =   0 ;
None.gif            
object  end  =   0 ;
None.gif            Word.Range tableLocation 
=  oDoc.Range( ref  start,  ref  end);
None.gif            oDoc.Tables.Add(tableLocation, 
3 4 ref  oMissing,  ref  oMissing);
None.gif
None.gif            Word.Table newTable 
=  oDoc.Tables[ 1 ];
None.gif            
object  beforeRow  =  newTable.Rows[ 1 ];
None.gif            newTable.Rows.Add(
ref  beforeRow);
None.gif
None.gif            Word.Cell cell 
=  newTable.Cell( 1 1 );
None.gif            cell.Merge(newTable.Cell(
1 2 ));

.单元格分离
None.gif              object  oMissing  =  System.Reflection.Missing.Value;
None.gif            Word._Application oWord;
None.gif            Word._Document oDoc;
None.gif            oWord 
=   new  Word.Application();
None.gif            oWord.Visible 
=   true ;
None.gif            oDoc 
=  oWord.Documents.Add( ref  oMissing,  ref  oMissing,
None.gif                
ref  oMissing,  ref  oMissing);
None.gif
None.gif            
object  start  =   0 ;
None.gif            
object  end  =   0 ;
None.gif            Word.Range tableLocation 
=  oDoc.Range( ref  start,  ref  end);
None.gif            oDoc.Tables.Add(tableLocation, 
3 4 ref  oMissing,  ref  oMissing);
None.gif
None.gif            Word.Table newTable 
=  oDoc.Tables[ 1 ];
None.gif            
object  beforeRow  =  newTable.Rows[ 1 ];
None.gif            newTable.Rows.Add(
ref  beforeRow);
None.gif
None.gif            Word.Cell cell 
=  newTable.Cell( 1 1 );
None.gif            cell.Merge(newTable.Cell(
1 2 ));
None.gif
None.gif            
object  Rownum  =   2 ;
None.gif            
object  Columnnum  =   2 ;
None.gif            cell.Split(
ref  Rownum,  ref   Columnnum);
None.gif

通过段落控制插入
None.gif              object  oMissing  =  System.Reflection.Missing.Value;
ExpandedBlockStart.gifContractedBlock.gif            
object  oEndOfDoc  =   " \\endofdoc " /**/ /* \endofdoc is a predefined bookmark */
None.gif
None.gif            
// Start Word and create a new document.
None.gif
            Word._Application oWord;
None.gif            Word._Document oDoc;
None.gif            oWord 
=   new  Word.Application();
None.gif            oWord.Visible 
=   true ;
None.gif            oDoc 
=  oWord.Documents.Add( ref  oMissing,  ref  oMissing,
None.gif                
ref  oMissing,  ref  oMissing);
None.gif
None.gif            
// Insert a paragraph at the beginning of the document.
None.gif
            Word.Paragraph oPara1;
None.gif            oPara1 
=  oDoc.Content.Paragraphs.Add( ref  oMissing);
None.gif            oPara1.Range.Text 
=   " Heading 1 " ;
None.gif            oPara1.Range.Font.Bold 
=   1 ;
None.gif            oPara1.Format.SpaceAfter 
=   24 ;     // 24 pt spacing after paragraph.
None.gif
            oPara1.Range.InsertParagraphAfter();

有部分是我的原创(50%吧)。

现在我已经基本掌握了Word了。剩下大家只需要看看类库函数就懂了。

转载于:https://www.cnblogs.com/zc22/articles/564617.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值