2

我想使用 Qt 5 格式化 Microsoft Excel 2010 单元格注释(例如更改字体、粗体等)。

我可以使用以下代码向单元格添加注释:

QAxObject* cellRange = m_activeWorksheet->querySubObject("Cells(int, int)", row, col);
cellRange->dynamicCall("AddComment(const QVariant&)", comment);

我还可以为单元格注释设置 AutoSize 属性:

QAxObject* axComment = cellRange->querySubObject("Comment");
QAxObject* shape = axComment->querySubObject("Shape");
shape->querySubObject("TextFrame")->setProperty("AutoSize", autosize);

但我无法更改“更深”的注释属性,例如 TextFrame.Characters.Font.Bold。

设置单元格注释后,命令

shape->querySubObject("TextFrame") 

返回一个非零指针,但是

shape->querySubObject("TextFrame")->querySubObject("Characters")

返回 NULL。

如何使用 格式化单元格注释QAxObject?是否有QAxObject可访问的不同 s的属性/子对象的描述QAxObject

以下代码没有任何作用:

shape->setProperty("AutoShapeType", 5);
4

2 回答 2

2
  1. 问题可能是 TextFrame 没有属性 Characters。相反,它有方法 Characters,但它的完整签名是

    Characters(Start, Length)
    

    Qt docs你应该指定完整的签名,所以你应该查询值

    shape->querySubObject("TextFrame")->querySubObject("Characters(Start,Length)")
    
  2. 您不能设置AutoShapeType5AutoShapeTypeMsoAutoShapeType类型,并且只允许指定值。

于 2014-10-24T17:05:47.027 回答
1

浏览完 Qt 文档后,QAxBase dynamicCAll 部分展示了如何使用动态调用设置 Excel 单元格注释的形状:

QString comment("My comment");
QAxObject* cellRange = m_activeWorksheet->querySubObject("Cells(int, int)", cellRow, cellColumn);
cellRange->dynamicCall("AddComment(const QVariant&)", comment);
QAxObject* axComment = cellRange->querySubObject("Comment");
QAxObject* shape = axComment->querySubObject("Shape");
shape->dynamicCall("AutoShapeType", 5);

该值可以从 Lol4t0 的链接中找到:MsoAutoShapeType Enumeration。这里 5 用于得到一个圆角矩形(msoShapeRoundedRectangle)。下面是用于更改文本注释格式的剩余代码:

QAxObject* textFrame = shape->querySubObject("TextFrame");
QAxObject* chars = textFrame->querySubObject("Characters(int, int)", 1, comment.size());
QAxObject* font = chars->querySubObject("Font");
font->setProperty("Bold", false);
shape->querySubObject("TextFrame")->querySubObject("Characters(2, 3)")->querySubObject("Font")->setProperty("Size", 24);
于 2014-10-29T19:59:00.910 回答