导出Werd

  1 本文分步介绍如何利用 Visual C# .NET 的自动化功能在 Word 中创建新文档。 
  2 
  3 
  4 代码示例
  5  本文中的代码示例将说明如何完成以下任务: •插入包含文本和格式的段落。
  6 •浏览和修改文档中的不同范围。
  7 •插入表格、设置表格格式并在表格中填充数据。
  8 •添加图表。
  9  要利用 Visual C# .NET 的自动化功能创建新的 Word 文档,请执行以下步骤: 1.启动 Microsoft Visual Studio .NET。在文件菜单上,单击新建,然后单击项目。在项目类型下,单击 Visual C# 项目,然后单击模板下的 Windows 应用程序。默认情况下会创建 Form1。
 10 2.添加对 Microsoft Word 对象库的引用。为此,请按照下列步骤操作:a.在项目菜单上,单击添加引用。
 11 b.在 COM 选项卡上,找到 Microsoft Word 对象库,然后单击选择。
 12 c.在添加引用对话框中单击确定,接受您的选择。如果系统提示您为选定的库生成包装,请单击是。
 13 
 14 3.在视图菜单上,选择工具箱以显示工具箱,然后向 Form1 添加一个按钮。
 15 4.双击 Button1。出现该窗体的代码窗口。
 16 5.在代码窗口中,将以下代码
 17 
 18 private void button1_Click(object sender, System.EventArgs e)
 19 {
 20 }
 21                      替换为: 
 22 
 23 private void button1_Click(object sender, System.EventArgs e)
 24 {
 25     object oMissing = System.Reflection.Missing.Value;
 26     object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */ 
 27 
 28     //Start Word and create a new document.
 29     Word._Application oWord;
 30     Word._Document oDoc;
 31     oWord = new Word.Application();
 32     oWord.Visible = true;
 33     oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
 34         ref oMissing, ref oMissing);
 35 
 36     //Insert a paragraph at the beginning of the document.
 37     Word.Paragraph oPara1;
 38     oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
 39     oPara1.Range.Text = "Heading 1";
 40     oPara1.Range.Font.Bold = 1;
 41     oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.
 42     oPara1.Range.InsertParagraphAfter();
 43 
 44     //Insert a paragraph at the end of the document.
 45     Word.Paragraph oPara2;
 46     object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
 47     oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);
 48     oPara2.Range.Text = "Heading 2";
 49     oPara2.Format.SpaceAfter = 6;
 50     oPara2.Range.InsertParagraphAfter();
 51 
 52     //Insert another paragraph.
 53     Word.Paragraph oPara3;
 54     oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
 55     oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);
 56     oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:";
 57     oPara3.Range.Font.Bold = 0;
 58     oPara3.Format.SpaceAfter = 24;
 59     oPara3.Range.InsertParagraphAfter();
 60 
 61     //Insert a 3 x 5 table, fill it with data, and make the first row
 62     //bold and italic.
 63     Word.Table oTable;
 64     Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
 65     oTable = oDoc.Tables.Add(wrdRng, 3, 5, ref oMissing, ref oMissing);
 66     oTable.Range.ParagraphFormat.SpaceAfter = 6;
 67     int r, c;
 68     string strText;
 69     for(r = 1; r <= 3; r++)
 70         for(c = 1; c <= 5; c++)
 71         {
 72             strText = "r" + r + "c" + c;
 73             oTable.Cell(r, c).Range.Text = strText;
 74         }
 75     oTable.Rows[1].Range.Font.Bold = 1;
 76     oTable.Rows[1].Range.Font.Italic = 1;
 77 
 78     //Add some text after the table.
 79     Word.Paragraph oPara4;
 80     oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
 81     oPara4 = oDoc.Content.Paragraphs.Add(ref oRng);
 82     oPara4.Range.InsertParagraphBefore();
 83     oPara4.Range.Text = "And here's another table:";
 84     oPara4.Format.SpaceAfter = 24;
 85     oPara4.Range.InsertParagraphAfter();
 86 
 87     //Insert a 5 x 2 table, fill it with data, and change the column widths.
 88     wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
 89     oTable = oDoc.Tables.Add(wrdRng, 5, 2, ref oMissing, ref oMissing);
 90     oTable.Range.ParagraphFormat.SpaceAfter = 6;
 91     for(r = 1; r <= 5; r++)
 92         for(c = 1; c <= 2; c++)
 93         {
 94             strText = "r" + r + "c" + c;
 95             oTable.Cell(r, c).Range.Text = strText;
 96         }
 97     oTable.Columns[1].Width = oWord.InchesToPoints(2); //Change width of columns 1 & 2
 98     oTable.Columns[2].Width = oWord.InchesToPoints(3);
 99 
100     //Keep inserting text. When you get to 7 inches from top of the
101     //document, insert a hard page break.
102     object oPos;
103     double dPos = oWord.InchesToPoints(7);
104     oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range.InsertParagraphAfter();
105     do
106     {
107         wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
108         wrdRng.ParagraphFormat.SpaceAfter = 6;
109         wrdRng.InsertAfter("A line of text");
110         wrdRng.InsertParagraphAfter();
111         oPos = wrdRng.get_Information
112                        (Word.WdInformation.wdVerticalPositionRelativeToPage);
113     }
114     while(dPos >= Convert.ToDouble(oPos));
115     object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
116     object oPageBreak = Word.WdBreakType.wdPageBreak;
117     wrdRng.Collapse(ref oCollapseEnd);
118     wrdRng.InsertBreak(ref oPageBreak);
119     wrdRng.Collapse(ref oCollapseEnd);
120     wrdRng.InsertAfter("We're now on page 2. Here's my chart:");
121     wrdRng.InsertParagraphAfter();
122 
123     //Insert a chart.
124     Word.InlineShape oShape;
125     object oClassType = "MSGraph.Chart.8";
126     wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
127     oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref oMissing, 
128         ref oMissing, ref oMissing, ref oMissing,
129         ref oMissing, ref oMissing, ref oMissing);
130 
131     //Demonstrate use of late bound oChart and oChartApp objects to
132     //manipulate the chart object with MSGraph.
133     object oChart;
134     object oChartApp;
135     oChart = oShape.OLEFormat.Object;
136     oChartApp = oChart.GetType().InvokeMember("Application",
137         BindingFlags.GetProperty, null, oChart, null);
138 
139     //Change the chart type to Line.
140     object[] Parameters = new Object[1];
141     Parameters[0] = 4; //xlLine = 4
142     oChart.GetType().InvokeMember("ChartType", BindingFlags.SetProperty,
143         null, oChart, Parameters);
144 
145     //Update the chart image and quit MSGraph.
146     oChartApp.GetType().InvokeMember("Update",
147         BindingFlags.InvokeMethod, null, oChartApp, null);
148     oChartApp.GetType().InvokeMember("Quit",
149         BindingFlags.InvokeMethod, null, oChartApp, null);
150     //... If desired, you can proceed from here using the Microsoft Graph 
151     //Object model on the oChart and oChartApp objects to make additional
152     //changes to the chart.
153 
154     //Set the width of the chart.
155     oShape.Width = oWord.InchesToPoints(6.25f);
156     oShape.Height = oWord.InchesToPoints(3.57f);
157 
158     //Add text after the chart.
159     wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
160     wrdRng.InsertParagraphAfter();
161     wrdRng.InsertAfter("THE END.");
162 
163     //Close this form.
164     this.Close();
165 }
166                     
167  6.滚动到代码窗口的顶部。将下面的代码行添加到 using 指令列表的末尾:
168 
169 using Word = Microsoft.Office.Interop.Word;
170 using System.Reflection;
171                     
172  7.按 F5 键生成并运行程序。
173 8.单击 Button1,启动 Word 自动化功能并创建文档。
174  代码执行完成后,检查为您创建的文档。该文档包含两页设置了格式的段落、表格和图表。 
175 
176 
177 使用模板
178  如果您要使用自动化功能创建的文档都是通用格式,则利用基于预设格式的模板的新文档来开始创建过程会更加容易。与从头创建文档相比,将某个模板与 Word 自动化客户端配合使用有两大优点: •您可以对整个文档中的对象的格式设置和布局施加更多控制。
179 •可以使用较少的代码创建文档。
180  通过使用模板,可以精确地调整表格、段落和其他对象在文档中的布局,并可为这些对象添加格式设置。通过使用自动化功能,可以基于包含下面这样的代码的模板创建新文档: 
181 
182 object oTemplate = "c:\\MyTemplate.dot";
183 oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
184     ref oMissing, ref oMissing);
185                  在模板中,可以定义书签,这样,自动化客户端就可以在文档的特定位置加入可变文本,如下所示: 
186 
187 object oBookMark = "MyBookmark";
188 oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";
189                  使用模板的另一个优点在于,您可以创建和存储希望在运行时应用的格式样式,如下所示: 
190 
191 object oStyleName = "MyStyle";
192 oDoc.Bookmarks.Item(ref oBookMark).Range.set_Style(ref oStyleName);
193                  - 或者 - 
194 
195 object oStyleName = "MyStyle";
196 oWord.Selection.set_Style(ref oStyleName);
原文地址:https://www.cnblogs.com/a1235202005/p/2762590.html