前提:引入COM组件
1、创建
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = newWord.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(refoMissing, refoMissing, refoMissing, refoMissing);
2、打开
objectoMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = newWord.Application();
oWord.Visible = true;
objectfileName = @"E:CCCXCXXTestDoc.doc";
oDoc = oWord.Documents.Open(reffileName,
refoMissing, refoMissing, refoMissing, refoMissing, refoMissing,
refoMissing, refoMissing, refoMissing, refoMissing, refoMissing,
refoMissing, refoMissing, refoMissing, refoMissing, refoMissing);
3、导入模板
objectoMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = newWord.Application();
oWord.Visible = true;
objectfileName = @"E:XXXCCXTest.doc";
oDoc = oWord.Documents.Add(reffileName, refoMissing, refoMissing, refoMissing);
4、添加新表
objectoMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = newWord.Application();
oWord.Visible = true;
objectfileName = @"E:XXXCCXTest.doc";
oDoc = oWord.Documents.Add(reffileName, refoMissing, ref oMissing, ref oMissing);
5、表插入行
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 start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
6、单元格合并
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 start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));
7、单元格分离
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(oMissing, ref oMissing, ref oMissing);
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);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));
object Rownum = 2;
object Columnnum = 2;
cell.Split(ref Rownum, ref Columnnum);
8、通过段落控制插入
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = @"\endofdoc";
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);
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "Heading 1";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24;oPara1.Range.InsertParagraphAfter();