MFC程序COM操作EXCEL时,获取合并单元格行、列时出现的问题

我添加了一个IsMergeRange函数,代码如下:
int CExcelCell::IsMergeRange(CRange range, bool col_row)
{
COleVariant vResult;
vResult=range.get_MergeCells();
if(vResult.boolVal!=0)
{
long x, y;
m_ecRange.AttachDispatch(range.get_Rows(),true);
x=m_ecRange.get_Count();
m_ecRange.AttachDispatch(range.get_Columns(),true);
y=m_ecRange.get_Count();
if(col_row)
return y;
else return x;
}
return 0;
}
此函数作用是判断指定单元格是否为合并单元格,是则按要求返回该合并单元格的行或者列,我在模板text.xlsx中,将A1到A5合并。然后引用函数IsMergeRange(getRange(_T("A1")), false),但是程序返回的却是1行,正常应该是5啊,查找CSDN和百度知道的相关问题后仍然未能确定问题原因,我使用的是OFFICE 2010,求各位指点!(getRange()函数返回指定单元格,亲测绝对没问题)
下断点,x=1,y=1。是A1单个单元格的行和列,为什么不是获取到合并单元格的行和列呢?

if(vResult.boolVal!=0)
{
long x, y;
CRange Th_range; \\加入新的CRange对象
Th_range.AttachDispatch(range.get_MergeArea(),true); \\获取合并单元格区域
m_ecRange.AttachDispatch(Th_range.get_Rows(),true); \\Th_range内获取行
x=m_ecRange.get_Count();
m_ecRange.AttachDispatch(Th_range.get_Columns(),true);\\Th_range内获取列
y=m_ecRange.get_Count();
if(col_row)
return y;
else return x;
}
温馨提示:答案为网友推荐,仅供参考
相似回答