前提:
导入COM库:Microsoft word 11.0 Object Library.
引用里面就增加了:
创建新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);
打开文档:
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:\CCCXCXX\TestDoc.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);
导入模板
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:\XXXCCX\Test.doc";
oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
ref oMissing, ref oMissing);
.添加新表
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);
![[转]C#操作Word完全功略](https://i-blog.csdnimg.cn/blog_migrate/0d64405f076852a727853140f1b5ed12.gif)
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);
.表插入行
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);
![[转]C#操作Word完全功略](https://i-blog.csdnimg.cn/blog_migrate/d8cb661a69c8a44b0477812de3b6a686.gif)
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);
![[转]C#操作Word完全功略](https://i-blog.csdnimg.cn/blog_migrate/c3aa6c8060bdc76355141314b44b853f.gif)
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
.单元格合并
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);
![[转]C#操作Word完全功略](https://i-blog.csdnimg.cn/blog_migrate/47f82711beca49891e96369e762abe8d.gif)
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);
![[转]C#操作Word完全功略](https://i-blog.csdnimg.cn/blog_migrate/9815d673fa03891fc23847dd400bcaa5.gif)
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
![[转]C#操作Word完全功略](https://i-blog.csdnimg.cn/blog_migrate/c8e425dc1d07418a6cf15ef22a692ce7.gif)
Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));
.单元格分离
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);
![[转]C#操作Word完全功略](https://i-blog.csdnimg.cn/blog_migrate/e4739a87443e4ea059b0b175dfafc373.gif)
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);
![[转]C#操作Word完全功略](https://i-blog.csdnimg.cn/blog_migrate/c4a0430bd1748603bd857217725d89e8.gif)
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
![[转]C#操作Word完全功略](https://i-blog.csdnimg.cn/blog_migrate/2cdd5c4e4e268c22aa8cd966e6165a4c.gif)
Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));
![[转]C#操作Word完全功略](https://i-blog.csdnimg.cn/blog_migrate/232f99fa251d5b5e86aee1709883c68a.gif)
object Rownum = 2;
object Columnnum = 2;
cell.Split(ref Rownum, ref Columnnum);
通过段落控制插入
导入COM库:Microsoft word 11.0 Object Library.
引用里面就增加了:
创建新Word
打开文档:
导入模板
.添加新表
.表插入行
.单元格合并
.单元格分离
通过段落控制插入
本文介绍如何使用C#操作Microsoft Word文档,包括创建新文档、打开现有文档、添加表格、插入行、合并及拆分单元格等操作。

被折叠的 条评论
为什么被折叠?



