Qt ActiveX QAxObject格式Excel单元格注释
我想使用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
访问的不同QAxObject
的属性/子对象的描述?
以下代码不起作用:
shape->setProperty("AutoShapeType", 5);
-
可能问题是TextFrame没有属性
Characters
。 相反它有方法Characters
,但它的全部签名是Characters(Start, Length)
Qt文档说 ,你应该指定完整的签名,所以你可能应该查询值
shape->querySubObject("TextFrame")->querySubObject("Characters(Start,Length)")
-
您不能将
AutoShapeType
设置为5
。AutoShapeType
的types是MsoAutoShapeType,并且只允许指定的值。
在浏览Qt文档之后, QAxBase dynamicCAll部分展示了如何使用dynamic调用设置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的链接中find: MsoAutoShapeType枚举 。 这里5用来得到一个圆angular矩形(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);