#数据库数据导入导出系列之五 C#实现动态生成Word(转)

1. 一个控制台例子,实现动态生成Word。

首先,添加引用:COM->Microsoft Word 11.0 Object Library。

View Code
  1 using System; 
  2 using System.Collections.Generic; 
  3 using System.Linq; 
  4 using System.Text; 
  5 using System.IO; 
  6 using Microsoft.Office.Interop.Word; 
  7 using System.Reflection; 
  8   
  9 namespace TestWord 
 10 { 
 11     class Program 
 12     { 
 13         static void Main(string[] args) 
 14         { 
 15             object oMissing = System.Reflection.Missing.Value; 
 16             object oEndOfDoc = "\\endofdoc";     //endofdoc是预定义的bookmark 
 17   
 18             //创建一个document. 
 19             Microsoft.Office.Interop.Word._Application oWord; 
 20             Microsoft.Office.Interop.Word._Document oDoc; 
 21             oWord = new Microsoft.Office.Interop.Word.Application(); 
 22             oWord.Visible = true; 
 23             oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, 
 24             ref oMissing, ref oMissing); 
 25   
 26             //在document的开始部分添加一个paragraph. 
 27             Microsoft.Office.Interop.Word.Paragraph oPara1; 
 28             oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); 
 29             oPara1.Range.Text = "Heading 1"; 
 30             oPara1.Range.Font.Bold = 1; 
 31             oPara1.Format.SpaceAfter = 24;        //24 pt 行间距 
 32             oPara1.Range.InsertParagraphAfter(); 
 33   
 34             //在当前document的最后添加一个paragraph 
 35             Microsoft.Office.Interop.Word.Paragraph oPara2; 
 36             object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; 
 37             oPara2 = oDoc.Content.Paragraphs.Add(ref oRng); 
 38             oPara2.Range.Text = "Heading 2"; 
 39             oPara2.Format.SpaceAfter = 6; 
 40             oPara2.Range.InsertParagraphAfter(); 
 41   
 42             //接着添加一个paragraph 
 43             Microsoft.Office.Interop.Word.Paragraph oPara3; 
 44             oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; 
 45             oPara3 = oDoc.Content.Paragraphs.Add(ref oRng); 
 46             oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:"; 
 47             oPara3.Range.Font.Bold = 0; 
 48             oPara3.Format.SpaceAfter = 24; 
 49             oPara3.Range.InsertParagraphAfter(); 
 50   
 51             //添加一个3行5列的表格,填充数据,并且设定第一行的样式 
 52             Microsoft.Office.Interop.Word.Table oTable; 
 53             Microsoft.Office.Interop.Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; 
 54             oTable = oDoc.Tables.Add(wrdRng, 3, 5, ref oMissing, ref oMissing); 
 55             oTable.Range.ParagraphFormat.SpaceAfter = 6; 
 56             int r, c; 
 57             string strText; 
 58             for (r = 1; r <= 3; r++) 
 59                 for (c = 1; c <= 5; c++) 
 60                 { 
 61                     strText = "r" + r + "c" + c; 
 62                     oTable.Cell(r, c).Range.Text = strText; 
 63                 } 
 64             oTable.Rows[1].Range.Font.Bold = 1; 
 65             oTable.Rows[1].Range.Font.Italic = 1; 
 66   
 67             //接着添加一些文字 
 68             Microsoft.Office.Interop.Word.Paragraph oPara4; 
 69             oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; 
 70             oPara4 = oDoc.Content.Paragraphs.Add(ref oRng); 
 71             oPara4.Range.InsertParagraphBefore(); 
 72             oPara4.Range.Text = "And here's another table:"; 
 73             oPara4.Format.SpaceAfter = 24; 
 74             oPara4.Range.InsertParagraphAfter(); 
 75   
 76             //添加一个5行2列的表,填充数据并且改变列宽 
 77             wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; 
 78             oTable = oDoc.Tables.Add(wrdRng, 5, 2, ref oMissing, ref oMissing); 
 79             oTable.Range.ParagraphFormat.SpaceAfter = 6; 
 80             for (r = 1; r <= 5; r++) 
 81                 for (c = 1; c <= 2; c++) 
 82                 { 
 83                     strText = "r" + r + "c" + c; 
 84                     oTable.Cell(r, c).Range.Text = strText; 
 85                 } 
 86             oTable.Columns[1].Width = oWord.InchesToPoints(2); //设置列宽 
 87             oTable.Columns[2].Width = oWord.InchesToPoints(3); 
 88   
 89             //Keep inserting text. When you get to 7 inches from top of the 
 90             //document, insert a hard page break. 
 91             object oPos; 
 92             double dPos = oWord.InchesToPoints(7); 
 93             oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range.InsertParagraphAfter(); 
 94             do
 95             { 
 96                 wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; 
 97                 wrdRng.ParagraphFormat.SpaceAfter = 6; 
 98                 wrdRng.InsertAfter("A line of text"); 
 99                 wrdRng.InsertParagraphAfter(); 
100                 oPos = wrdRng.get_Information 
101                                            (Microsoft.Office.Interop.Word.WdInformation.wdVerticalPositionRelativeToPage); 
102             } 
103             while (dPos >= Convert.ToDouble(oPos)); 
104             object oCollapseEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd; 
105             object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak; 
106             wrdRng.Collapse(ref oCollapseEnd); 
107             wrdRng.InsertBreak(ref oPageBreak); 
108             wrdRng.Collapse(ref oCollapseEnd); 
109             wrdRng.InsertAfter("We're now on page 2. Here's my chart:"); 
110             wrdRng.InsertParagraphAfter(); 
111   
112             //添加一个chart 
113             Microsoft.Office.Interop.Word.InlineShape oShape; 
114             object oClassType = "MSGraph.Chart.8"; 
115             wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; 
116             oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref oMissing, 
117             ref oMissing, ref oMissing, ref oMissing, 
118             ref oMissing, ref oMissing, ref oMissing); 
119   
120             //Demonstrate use of late bound oChart and oChartApp objects to 
121             //manipulate the chart object with MSGraph. 
122             object oChart; 
123             object oChartApp; 
124             oChart = oShape.OLEFormat.Object; 
125             oChartApp = oChart.GetType().InvokeMember("Application", 
126             BindingFlags.GetProperty, null, oChart, null); 
127   
128             //Change the chart type to Line. 
129             object[] Parameters = new Object[1]; 
130             Parameters[0] = 4; //xlLine = 4 
131             oChart.GetType().InvokeMember("ChartType", BindingFlags.SetProperty, 
132             null, oChart, Parameters); 
133   
134             //Update the chart image and quit MSGraph. 
135             oChartApp.GetType().InvokeMember("Update", 
136             BindingFlags.InvokeMethod, null, oChartApp, null); 
137             oChartApp.GetType().InvokeMember("Quit", 
138             BindingFlags.InvokeMethod, null, oChartApp, null); 
139             //... If desired, you can proceed from here using the Microsoft Graph  
140             //Object model on the oChart and oChartApp objects to make additional 
141             //changes to the chart. 
142   
143             //Set the width of the chart. 
144             oShape.Width = oWord.InchesToPoints(6.25f); 
145             oShape.Height = oWord.InchesToPoints(3.57f); 
146   
147             //Add text after the chart. 
148             wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; 
149             wrdRng.InsertParagraphAfter(); 
150             wrdRng.InsertAfter("THE END."); 
151   
152             Console.ReadLine(); 
153         } 
154     } 
155 } 

2. 介绍几篇牛人写的关于操作Word的文章

[分享]一段导出到word模版的代码

http://www.cnblogs.com/goody9807/archive/2005/08/25/222526.html

再谈word2003编程

http://www.cnblogs.com/Andmm/archive/2008/06/18/1224422.html

最近一直在做C#操作office方面的工作!总结一下!Word(二)

http://www.cnblogs.com/wngwz/archive/2004/08/19/34678.html

C#也能动态生成Word文档并填充数据

http://www.cnblogs.com/qyfan82/archive/2007/09/14/893293.html

posted @ 2012-11-13 14:13  cpcpc  阅读(2129)  评论(1编辑  收藏  举报