C#操作Word完全方法·VS2005专业教程网|GotASPX.COM,专注于ASP.NET的专业教程网站

本文介绍如何使用Microsoft Word 11.0 Object Library进行Word文档的操作,包括创建新文档、打开已有文档、添加表格、插入行、合并及拆分单元格等步骤。提供了详细的代码示例,便于理解和实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前提:
导入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 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值