【20090319-02】asp.net 操作 word(转)

 

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

 

 

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

 

 

posted @ 2009-03-19 13:53  WillWayer  阅读(296)  评论(0编辑  收藏  举报