C# MSGraph.Chart.8 画图,制表

本文档展示了如何使用C#结合MSGraph库在Word文档中创建表格和图表。通过`Query`方法获取数据,然后利用`Tables.Add`方法插入表格,设置表格样式,并填充内容。接着创建图表,设定图表类型、颜色、标签等属性,以及调整X轴的刻度显示方向。
摘要由CSDN通过智能技术生成

using System;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.IO;
using System.Windows.Forms;
using Graph = Microsoft.Office.Interop.Graph;
using Word = Microsoft.Office.Interop.Word;

 

namespace WeeklyReporter
{
    public partial class MainForm : Form
    { 

        private void CreateReport(DateTime fromDate, DateTime toDate, string reportDepartment)
        {  

                DateTable dt = Query("...");

                if (oDoc.Bookmarks.Exists(Convert.ToString(oBookMark)))
                {
                    object nothing = System.Reflection.Missing.Value;

                    //文档中创建表格
                    Microsoft.Office.Interop.Word.Table gmsTable = oDoc.Tables.Add(oDoc.Bookmarks[oBookMark].Range, dt.Rows.Count + 1, dt.Columns.Count, ref nothing, ref nothing);

                    //设置表格样式
                    gmsTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    gmsTable.Borders.OutsideColor = Word.WdColor.wdColorPaleBlue;
                    gmsTable.Borders.OutsideLineWidth = Word.WdLineWidth.wdLineWidth225pt;

                    gmsTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    gmsTable.Borders.InsideColor = Word.WdColor.wdColorPaleBlue;
                    gmsTable.Borders.InsideLineWidth = Word.WdLineWidth.wdLineWidth225pt;

                    //填充表格内容
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        gmsTable.Cell(1, i + 1).Shading.ForegroundPatternColor = Word.WdColor.wdColorLightGreen;
                        gmsTable.Cell(1, i + 1).Range.Bold = 20;
                    }

                    gmsTable.Cell(1, 1).Range.Text = "A";
                    gmsTable.Cell(1, 2).Range.Text = "B";
                    gmsTable.Cell(1, 3).Range.Text = "C";
                    gm

表格格式:<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值