【20090319-02】asp.net 操作 word(转)
1
导入COM库:Microsoft word 11.0 Object Library.
2
引用里面就增加了:
3
4
创建新Word
5
6
7
8
object oMissing = System.Reflection.Missing.Value;
9
Word._Application oWord;
10
Word._Document oDoc;
11
oWord = new Word.Application();
12
oWord.Visible = true;
13
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
14
15
打开文档:
16
17
18
19
object oMissing = System.Reflection.Missing.Value;
20
Word._Application oWord;
21
Word._Document oDoc;
22
oWord = new Word.Application();
23
oWord.Visible = true;
24
object fileName = @"E:CCCXCXXTestDoc.doc";
25
oDoc = oWord.Documents.Open(ref fileName,
26
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
27
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
28
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
29
30
导入模板
31
32
33
34
object oMissing = System.Reflection.Missing.Value;
35
Word._Application oWord;
36
Word._Document oDoc;
37
oWord = new Word.Application();
38
oWord.Visible = true;
39
object fileName = @"E:XXXCCXTest.doc";
40
oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);
41
42
添加新表
43
44
45
46
object oMissing = System.Reflection.Missing.Value;
47
Word._Application oWord;
48
Word._Document oDoc;
49
oWord = new Word.Application();
50
oWord.Visible = true;
51
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
52
53
object start = 0;
54
object end = 0;
55
Word.Range tableLocation = oDoc.Range(ref start, ref end);
56
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
57
58
表插入行
59
60
61
62
object oMissing = System.Reflection.Missing.Value;
63
Word._Application oWord;
64
Word._Document oDoc;
65
oWord = new Word.Application();
66
oWord.Visible = true;
67
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
68
69
object start = 0;
70
object end = 0;
71
Word.Range tableLocation = oDoc.Range(ref start, ref end);
72
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
73
74
Word.Table newTable = oDoc.Tables[1];
75
object beforeRow = newTable.Rows[1];
76
newTable.Rows.Add(ref beforeRow);
77
78
单元格合并
79
80
81
82
object oMissing = System.Reflection.Missing.Value;
83
Word._Application oWord;
84
Word._Document oDoc;
85
oWord = new Word.Application();
86
oWord.Visible = true;
87
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
88
89
object start = 0;
90
object end = 0;
91
Word.Range tableLocation = oDoc.Range(ref start, ref end);
92
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
93
94
Word.Table newTable = oDoc.Tables[1];
95
object beforeRow = newTable.Rows[1];
96
newTable.Rows.Add(ref beforeRow);
97
98
Word.Cell cell = newTable.Cell(1, 1);
99
cell.Merge(newTable.Cell(1, 2));
100
101
单元格分离
102
103
object oMissing = System.Reflection.Missing.Value;
104
Word._Application oWord;
105
Word._Document oDoc;
106
oWord = new Word.Application();
107
oWord.Visible = true;
108
oDoc = oWord.Documents.Add(oMissing, ref oMissing, ref oMissing);
109
110
object start = 0;
111
object end = 0;
112
Word.Range tableLocation = oDoc.Range(ref start, ref end);
113
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
114
115
Word.Table newTable = oDoc.Tables[1];
116
object beforeRow = newTable.Rows[1];
117
newTable.Rows.Add(ref beforeRow);
118
119
Word.Cell cell = newTable.Cell(1, 1);
120
cell.Merge(newTable.Cell(1, 2));
121
122
object Rownum = 2;
123
object Columnnum = 2;
124
cell.Split(ref Rownum, ref Columnnum);
125
126
通过段落控制插入
127
128
object oMissing = System.Reflection.Missing.Value;
129
object oEndOfDoc = @""endofdoc"; /* endofdoc is a predefined bookmark */
130
131
//Start Word and create a new document.
132
133
Word._Application oWord;
134
Word._Document oDoc;
135
oWord = new Word.Application();
136
oWord.Visible = true;
137
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
138
139
//Insert a paragraph at the beginning of the document.
140
141
Word.Paragraph oPara1;
142
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
143
oPara1.Range.Text = "Heading 1";
144
oPara1.Range.Font.Bold = 1;
145
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
146
147
oPara1.Range.InsertParagraphAfter();
148
导入COM库:Microsoft word 11.0 Object Library. 2
引用里面就增加了: 3

4
创建新Word 5

6

7

8
object oMissing = System.Reflection.Missing.Value;9
Word._Application oWord;10
Word._Document oDoc;11
oWord = new Word.Application();12
oWord.Visible = true;13
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);14

15
打开文档: 16

17

18

19
object oMissing = System.Reflection.Missing.Value;20
Word._Application oWord;21
Word._Document oDoc;22
oWord = new Word.Application();23
oWord.Visible = true;24
object fileName = @"E:CCCXCXXTestDoc.doc";25
oDoc = oWord.Documents.Open(ref fileName,26
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,27
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,28
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);29

30
导入模板 31

32

33

34
object oMissing = System.Reflection.Missing.Value;35
Word._Application oWord;36
Word._Document oDoc;37
oWord = new Word.Application();38
oWord.Visible = true;39
object fileName = @"E:XXXCCXTest.doc";40
oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);41

42
添加新表 43

44

45

46
object oMissing = System.Reflection.Missing.Value;47
Word._Application oWord;48
Word._Document oDoc;49
oWord = new Word.Application();50
oWord.Visible = true;51
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);52

53
object start = 0;54
object end = 0;55
Word.Range tableLocation = oDoc.Range(ref start, ref end);56
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);57

58
表插入行 59

60

61

62
object oMissing = System.Reflection.Missing.Value;63
Word._Application oWord;64
Word._Document oDoc;65
oWord = new Word.Application();66
oWord.Visible = true;67
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);68

69
object start = 0;70
object end = 0;71
Word.Range tableLocation = oDoc.Range(ref start, ref end);72
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);73

74
Word.Table newTable = oDoc.Tables[1];75
object beforeRow = newTable.Rows[1];76
newTable.Rows.Add(ref beforeRow);77

78
单元格合并 79

80

81

82
object oMissing = System.Reflection.Missing.Value;83
Word._Application oWord;84
Word._Document oDoc;85
oWord = new Word.Application();86
oWord.Visible = true;87
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);88

89
object start = 0;90
object end = 0;91
Word.Range tableLocation = oDoc.Range(ref start, ref end);92
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);93

94
Word.Table newTable = oDoc.Tables[1];95
object beforeRow = newTable.Rows[1];96
newTable.Rows.Add(ref beforeRow);97

98
Word.Cell cell = newTable.Cell(1, 1);99
cell.Merge(newTable.Cell(1, 2));100

101
单元格分离 102

103
object oMissing = System.Reflection.Missing.Value;104
Word._Application oWord;105
Word._Document oDoc;106
oWord = new Word.Application();107
oWord.Visible = true;108
oDoc = oWord.Documents.Add(oMissing, ref oMissing, ref oMissing);109

110
object start = 0;111
object end = 0;112
Word.Range tableLocation = oDoc.Range(ref start, ref end);113
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);114

115
Word.Table newTable = oDoc.Tables[1];116
object beforeRow = newTable.Rows[1];117
newTable.Rows.Add(ref beforeRow);118

119
Word.Cell cell = newTable.Cell(1, 1);120
cell.Merge(newTable.Cell(1, 2));121

122
object Rownum = 2;123
object Columnnum = 2;124
cell.Split(ref Rownum, ref Columnnum);125

126
通过段落控制插入 127

128
object oMissing = System.Reflection.Missing.Value;129
object oEndOfDoc = @""endofdoc"; /* endofdoc is a predefined bookmark */130

131
//Start Word and create a new document.132

133
Word._Application oWord;134
Word._Document oDoc;135
oWord = new Word.Application();136
oWord.Visible = true;137
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);138

139
//Insert a paragraph at the beginning of the document.140

141
Word.Paragraph oPara1;142
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);143
oPara1.Range.Text = "Heading 1";144
oPara1.Range.Font.Bold = 1;145
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.146

147
oPara1.Range.InsertParagraphAfter();148

Microsoft Office 2000 开发人员对象模型指南

浙公网安备 33010602011771号