因为需要通过MFC实现自动生成Excel文件,并且实现数据饼状图的效果,搜索全网只找到生成柱状图的程序模板,怎么改都会显示错误,后来通过Excel宏的录制和编辑弄明白了饼状图的做法(其他图形类似)
chart.ChartWizard(var, // Source.
COleVariant((short)62), // Gallery: PIE.
covOptional, // Format, use default.
COleVariant((short)1), // PlotBy: xlRows.
COleVariant((short)0), // CategoryLabels. 第一行是分类标签
COleVariant((short)1), // SeriesLabels. 第一列是系列标签
COleVariant((short)TRUE), // HasLegend.
COleVariant(L"合格情况"), // Title.
covOptional, // CategoryTitle.
COleVariant(L"百分率"), // ValueTitles.
covOptional // ExtraTitle.
);
缺省值可以用covOptional填充,这个函数基本满足画其他图的功能了(比如柱状类的)
long left, top, width, height;
left = 400;
top = 150;
width = 350;
height = 250;
LPDISPATCH lpDisp = sheet.ChartObjects(covOptional);
chartobjects.AttachDispatch(lpDisp); // Attach the lpDisp pointer
// for ChartObjects to the chartobjects object.
if (chartobjects.get_Count() != 0) //当excel中存在原有图表时,删除之
{
chartobjects.Delete();
}
CChartObject chartobject = chartobjects.Add(left, top, width, height);//图表在表单中的位置及大小
lpDisp = sheet.get_Range(COleVariant(L"A9"), COleVariant(L"B10"));
chart.AttachDispatch(chartobject.get_Chart()); // GetChart() returns
chart.put_ChartType(5); // 建立图表,饼状图的检索值是5
chart.SetSourceData(lpDisp, COleVariant((short)2)); // 源数据区域 数据绘制方式(1或2)
chart.ClearToMatchStyle();
chart.put_ChartStyle(COleVariant((short)253));//图表风格(饼状图有不同风格,通过Excel宏的编辑获取值)
chart.put_HasTitle(true);//设置图表带有标题
chart.ChartWizard( // 在这个函数里可以设置标题名称
covOptional, //源数据
covOptional, // Gallery: PIE.
covOptional, // Format, use default.
covOptional, // PlotBy: xlRows.
covOptional, // CategoryLabels. 第一行是分类标签
covOptional, // SeriesLabels. 第一列是系列标签
covOptional, // HasLegend.
COleVariant(L"合格情况"), // Title.
covOptional, // CategoryTitle.
covOptional, // ValueTitles.
covOptional // ExtraTitle.
);
MSDN里对C++是真不友好,我这种入门小白卡在饼状图卡了一两天!写出来做个笔记,也和大家分享一下。