c#操作word2007生成OLEFormat图表
1 首先要插入 OLEFormat 对象,选择对象菜单中对象
2 选择 microsoft graph 图表,确定。
2 选择 microsoft graph 图表,确定。
3 插入图表模版,修改成如图,我的书签名称是chy
3 编写c#代码,操作图表模版
private void button2_Click(object sender, EventArgs e)
{
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "//endofdoc"; /* /endofdoc is a predefined bookmark */
object remarkOverTable = "chy";
//Start Word and create a new document.
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = true;
object Template = Application.StartupPath + @"\Report.docx";
oDoc = oWord.Documents.Add(ref Template, ref oMissing,
ref oMissing, ref oMissing);
//得到书签
Microsoft.Office.Interop.Word.Range TestRemark = oDoc.Bookmarks.get_Item(ref remarkOverTable).Range;
Word.InlineShape ii = TestRemark.InlineShapes[1];
//这是里2007和2003的区别!!好多人说 TestRemark.InlineShapes[0] 获取不到,这是索引1了。还有注意的是必须使用2007插入对象,不能用2003插入图表后在用2007的访问,不然还是会访问不到!!
ii.OLEFormat.Open();
//TestRemark.InlineShapes[0].OLEFormat.Open();// 打开OLE对像,注意这一步一定要有
Microsoft.Office.Interop.Graph.Chart _testChart =
(Microsoft.Office.Interop.Graph.Chart)(ii.OLEFormat.Object);
Microsoft.Office.Interop.Graph.Application _testApp =
_testChart.Application;
//在模板里面设置 饼图的 显示模式
object[] ColName = new object[] { "A", "B", "C", "D" }; //饼图列名字
object[] Values = new object[] { 20, 30, 20, 30 }; //饼图数据
for (int i = 0; i < Values.Length; i++)
{
//这里的行列式为循环下来填写的是A2,B2,C2,D2.... OK?
_testChart.Application.DataSheet.Cells.set_Item(2, ColName[i], Values[i]);
}
_testApp.Quit();
}
程序集引用