对大家来说,
我一直在寻找,但几乎找不到任何答案。我一直在尝试转换这个宏:
Range("A1:B7").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$A$1:$B$7")
ActiveChart.ChartType = xlPie
对于 Excel 2007,它从单元格 A1 到 B7 的数据中制作了一个简单的 2D 饼图。
以下是我如何将其转换为实现 Autowrap 功能的 C++ 代码:
--------杂项代码-------------
VARIANT darange;
IDispatch *pXlRange;
{
VARIANT range;
range.vt = VT_BSTR;
range.bstrVal = ::SysAllocString(L"A1:B7");
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pXlSheet, L"Range", 1, range);
darange.vt = VT_DISPATCH;
darange.pdispVal = result.pdispVal;
pXlRange = result.pdispVal;
}
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_METHOD, &result, pXlRange, L"Select", 0);
}
IDispatch *pXlChartObjects;
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pXlSheet, L"Shapes", 0);
pXlChartObjects = result.pdispVal;
}
IDispatch *pXlChartObject;
{
VARIANT result;
VariantInit(&result);
VARIANT left, top, width, height;
left.vt = VT_R8;
left.dblVal = 600.;
top.vt = VT_R8;
top.dblVal = 600.;
width.vt = VT_R8;
width.dblVal = 200.;
height.vt = VT_R8;
height.dblVal = 200.;
AutoWrap(DISPATCH_METHOD, &result, pXlChartObjects, L"AddChart", 1, left, top, width, height);
pXlChartObject = result.pdispVal;
}
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_METHOD, &result, pXlRange, L"Select", 0);
}
IDispatch *pXlChart;
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pXlChartObject, L"Chart", 0);
pXlChart = result.pdispVal;
}
// NOT WORKING
{
VARIANT result;
VariantInit(&result);
VARIANT type;
type.vt = VT_BSTR;
type.bstrVal = ::SysAllocString(L"xlPie");
//type.vt = VT_I4;
//type.lVal = 75;
AutoWrap(DISPATCH_PROPERTYPUT, &result, pXlChart, L"Chartype", 1, type);
}
// NOT WORKING
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_METHOD, &result, pXlChart, L"SetSourceData", 1, darange);
}
/*
--------杂项代码----------
它会打开 Excel 并生成一个条形图,但我需要一个饼图来实现。不幸的是,我不知道如何将“xlPie”传递给这段代码:
// NOT WORKING
{
VARIANT result;
VariantInit(&result);
VARIANT type;
type.vt = VT_BSTR;
type.bstrVal = ::SysAllocString(L"xlPie");
//type.vt = VT_I4;
//type.lVal = 75;
AutoWrap(DISPATCH_PROPERTYPUT, &result, pXlChart, L"Chartype", 1, type);
}
// NOT WORKING
有人有想法吗?