111,094
社区成员




oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oChart = oShape.OLEFormat.Object;
oChartApp = oChart.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, oChart, null);
oChartApp.GetType().InvokeMember("Update",BindingFlags.InvokeMethod, null, oChartApp, null);
oChartApp.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, null, oChartApp, null);
public static void AddSimpleChart(Document WordDoc, Word.Application WordApp, Object oEndOfDoc, string [,]data)
{
//插入chart
object oMissing = System.Reflection.Missing.Value;
Word.InlineShape oShape;
object oClassType = "MSGraph.Chart.8";
Word.Range wrdRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref oMissing,
ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing);
//Demonstrate use of late bound oChart and oChartApp objects to
//manipulate the chart object with MSGraph.
object oChart;
object oChartApp;
oChart = oShape.OLEFormat.Object;
oChartApp = oChart.GetType().InvokeMember("Application",BindingFlags.GetProperty, null, oChart, null);
//Change the chart type to Line.
object[] Parameters = new Object[1];
Parameters[0] = 4; //xlLine = 4
oChart.GetType().InvokeMember("ChartType", BindingFlags.SetProperty,
null, oChart, Parameters);
Chart objChart = (Chart)oShape.OLEFormat.Object;
objChart.ChartType = XlChartType.xlColumnClustered;
//绑定数据
DataSheet dataSheet;
dataSheet = objChart.Application.DataSheet;
int rownum=data.GetLength(0);
int columnnum=data.GetLength(1);
for(int i=1;i<=rownum;i++ )
for (int j = 1; j <= columnnum; j++)
{
dataSheet.Cells[i,j] =data[i-1,j-1];
}
objChart.Application.Update();
oChartApp.GetType().InvokeMember("Update",
BindingFlags.InvokeMethod, null, oChartApp, null);
oChartApp.GetType().InvokeMember("Quit",
BindingFlags.InvokeMethod, null, oChartApp, null);
//设置大小
oShape.Width = WordApp.InchesToPoints(6.25f);
oShape.Height = WordApp.InchesToPoints(3.57f);
}
private void button1_Click(object sender, EventArgs e)
{
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc";
string[,] data=new string[4,5];
data[0,1] = "第一月";
data[0, 2] = "第二月";
data[0, 3] = "第三月";
data[0, 4] = "第四月";
data[1,0] = "东部";
data[1,1] = "50";
data[1,2] = "50";
data[1,3] = "40";
data[1,4] = "50";
data[2,0] = "西部";
data[2,1] = "60";
data[2,2] = "60";
data[2,3] = "70";
data[2,4] = "80";
//data[3,6] = "0";
data[3,0] = "中部";
data[3,1] = "50";
data[3,2] = "50";
data[3,3] = "40";
data[3,4] = "50";
/* \endofdoc是预定义的bookmark */
//创建一个document.
//Word._Application oWord;
//Word._Document oDoc;
Word.Application oWord;
Word.Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
Word.Range wrdRng =oDoc .Bookmarks.get_Item(ref oEndOfDoc).Range;
AddSimpleChart(oDoc, oWord, oEndOfDoc, data);
wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
wrdRng.InsertParagraphAfter();
wrdRng.InsertAfter("THE END.");
//Close this form.
this.Close();
}