前提:
导入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);
打开文档:
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);
导入模板
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);
添加新表
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);
表插入行
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);
单元格合并
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));
单元格分离
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);
通过段落控制插入
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();