C#生成Word Dot【转-】

表格格式:<br>http://hi.baidu.com/metaza/blog/item/b277ccc4183b73ab8226ac8b.html<br>http://www.cnblogs.com/cryf/articles/718605.html<br><br>载自 微软MSDN<br><br>private void button1_Click(object sender, System.EventArgs e)<br>{<br>object oMissing = System.Reflection.Missing.Value;<br>object oEndOfDoc = "\\endofdoc"; <br><br>/* \endofdoc是预定义的bookmark */ <br><br>//创建一个document.<br>Word._Application oWord;<br>Word._Document oDoc;<br>oWord = new Word.Application();<br>oWord.Visible = true;<br>oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,<br>ref oMissing, ref oMissing);<br><br>//在document的开始部分添加一个paragraph.<br>Word.Paragraph oPara1;<br>oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);<br>oPara1.Range.Text = "Heading 1";<br>oPara1.Range.Font.Bold = 1;<br>oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.<br>oPara1.Range.InsertParagraphAfter();<br><br>//在当前document的最后添加一个paragraph<br><br>Word.Paragraph oPara2;<br>object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;<br>oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);<br>oPara2.Range.Text = "Heading 2";<br>oPara2.Format.SpaceAfter = 6;<br>oPara2.Range.InsertParagraphAfter();<br><br>//接着添加一个paragraph<br>Word.Paragraph oPara3;<br>oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;<br>oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);<br>oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:";<br>oPara3.Range.Font.Bold = 0;<br>oPara3.Format.SpaceAfter = 24;<br>oPara3.Range.InsertParagraphAfter();<br><br><br>//添加一个3行5列的表格,填充数据,并且设定第一行的样式<br><br>Word.Table oTable;<br>Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;<br>oTable = oDoc.Tables.Add(wrdRng, 3, 5, ref oMissing, ref oMissing);<br>oTable.Range.ParagraphFormat.SpaceAfter = 6;<br>int r, c;<br>string strText;<br>for(r = 1; r <= 3; r++)<br>for(c = 1; c <= 5; c++)<br>{<br>strText = "r" + r + "c" + c;<br>oTable.Cell(r, c).Range.Text = strText;<br>}<br>oTable.Rows[1].Range.Font.Bold = 1;<br>oTable.Rows[1].Range.Font.Italic = 1;<br><br>//接着添加一些文字<br><br>Word.Paragraph oPara4;<br>oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;<br>oPara4 = oDoc.Content.Paragraphs.Add(ref oRng);<br>oPara4.Range.InsertParagraphBefore();<br>oPara4.Range.Text = "And here's another table:";<br>oPara4.Format.SpaceAfter = 24;<br>oPara4.Range.InsertParagraphAfter();<br><br><br>//添加一个5行2列的表,填充数据并且改变列宽<br>wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;<br>oTable = oDoc.Tables.Add(wrdRng, 5, 2, ref oMissing, ref oMissing);<br>oTable.Range.ParagraphFormat.SpaceAfter = 6;<br>for(r = 1; r <= 5; r++)<br>for(c = 1; c <= 2; c++)<br>{<br>strText = "r" + r + "c" + c;<br>oTable.Cell(r, c).Range.Text = strText;<br>}<br>oTable.Columns[1].Width = oWord.InchesToPoints(2); //Change width of columns 1 & 2<br>oTable.Columns[2].Width = oWord.InchesToPoints(3);<br><br>//Keep inserting text. When you get to 7 inches from top of the<br>//document, insert a hard page break.<br>object oPos;<br>double dPos = oWord.InchesToPoints(7);<br>oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range.InsertParagraphAfter();<br>do<br>{<br>wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;<br>wrdRng.ParagraphFormat.SpaceAfter = 6;<br>wrdRng.InsertAfter("A line of text");<br>wrdRng.InsertParagraphAfter();<br>oPos = wrdRng.get_Information<br> (Word.WdInformation.wdVerticalPositionRelativeToPage);<br>}<br>while(dPos >= Convert.ToDouble(oPos));<br>object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;<br>object oPageBreak = Word.WdBreakType.wdPageBreak;<br>wrdRng.Collapse(ref oCollapseEnd);<br>wrdRng.InsertBreak(ref oPageBreak);<br>wrdRng.Collapse(ref oCollapseEnd);<br>wrdRng.InsertAfter("We're now on page 2. Here's my chart:");<br>wrdRng.InsertParagraphAfter();<br><br>//添加一个chart<br><br>Word.InlineShape oShape;<br>object oClassType = "MSGraph.Chart.8";<br>wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;<br>oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref oMissing, <br>ref oMissing, ref oMissing, ref oMissing,<br>ref oMissing, ref oMissing, ref oMissing);<br><br>//Demonstrate use of late bound oChart and oChartApp objects to<br>//manipulate the chart object with MSGraph.<br>object oChart;<br>object oChartApp;<br>oChart = oShape.OLEFormat.Object;<br>oChartApp = oChart.GetType().InvokeMember("Application",<br>BindingFlags.GetProperty, null, oChart, null);<br><br>//Change the chart type to Line.<br>object[] Parameters = new Object[1];<br>Parameters[0] = 4; //xlLine = 4<br>oChart.GetType().InvokeMember("ChartType", BindingFlags.SetProperty,<br>null, oChart, Parameters);<br><br>//Update the chart image and quit MSGraph.<br>oChartApp.GetType().InvokeMember("Update",<br>BindingFlags.InvokeMethod, null, oChartApp, null);<br>oChartApp.GetType().InvokeMember("Quit",<br>BindingFlags.InvokeMethod, null, oChartApp, null);<br>//... If desired, you can proceed from here using the Microsoft Graph <br>//Object model on the oChart and oChartApp objects to make additional<br>//changes to the chart.<br><br>//Set the width of the chart.<br>oShape.Width = oWord.InchesToPoints(6.25f);<br>oShape.Height = oWord.InchesToPoints(3.57f);<br><br>//Add text after the chart.<br>wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;<br>wrdRng.InsertParagraphAfter();<br>wrdRng.InsertAfter("THE END.");<br><br>//Close this form.<br>this.Close();<br>}<br><br><br><br>使用模板<br>如果您要使用自动化功能创建的文档都是通用格式,则利用基于预设格式的模板的新文档来开始创建过程会更加容易。与从头创建文档相比,将某个模板与 Word 自动化客户端配合使用有两大优点: ? 您可以对整个文档中的对象的格式设置和布局施加更多控制。 <br>? 可以使用较少的代码创建文档。 <br>通过使用模板,可以精确地调整表格、段落和其他对象在文档中的布局,并可为这些对象添加格式设置。通过使用自动化功能,可以基于包含下面这样的代码的模板创建新文档: 在模板中,可以定义书签,这样,自动化客户端就可以在文档的特定位置加入可变文本,如下所示: 使用模板的另一个优点在于,您可以创建和存储希望在运行时应用的格式样式,如下所示: - 或者 - <br><br><br>object oTemplate = "c:\\MyTemplate.dot";<br>oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,<br> ref oMissing, ref oMissing);<br> <br><br>object oBookMark = "MyBookmark";<br>oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";<br> <br><br>object oStyleName = "MyStyle";<br>oDoc.Bookmarks.Item(ref oBookMark).Range.set_Style(ref oStyleName);<br> <br><br>object oStyleName = "MyStyle";<br><br> <br>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值