VC++操作Excel生成饼状图!

因为需要通过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++是真不友好,我这种入门小白卡在饼状图卡了一两天!写出来做个笔记,也和大家分享一下。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值