Qt ActiveX QAxObject格式Excel单元格注释

Qt ActiveX QAxObject format Excel cell comment

本文关键字:单元格 注释 Excel 格式 ActiveX QAxObject Qt      更新时间:2023-10-16

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

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

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);
  1. 问题可能是TextFrame没有属性Characters。相反,它有方法Characters,但它的完整签名是

    Characters(Start, Length)
    

    Qt文档说您应该指定完整签名,所以您可能应该使用查询值

    shape->querySubObject("TextFrame")->querySubObject("Characters(Start,Length)")
    
  2. 不能将AutoShapeType设置为5AutoShapeType的类型为MsoAutoShapeType,并且只允许指定值。

浏览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枚举。这里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);