C# 操作Word,加入柱状图,如何传递参数?分不够再加

sky-defender 2009-11-18 09:27:55

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);



插入了一个chart,但是怎么设置chart的x-axis和y-axis呢?

比如,将x-axis分成一些小段,每一小段里面有两个值(两条柱子)
...全文
352 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sky-defender 2009-11-18
  • 打赏
  • 举报
回复
貌似找到了



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();
}


来自
http://developer.51cto.com/art/200908/147487.htm,这里有
cat_guan1411 2009-11-18
  • 打赏
  • 举报
回复
我也要做这个 帮lz顶
sky-defender 2009-11-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cupwei 的回复:]
不会 顶顶吧给你
[/Quote]

3Q

期待达人到来
cupwei 2009-11-18
  • 打赏
  • 举报
回复
不会 顶顶吧给你

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧