• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Qt 导出word

武飞扬头像
kchmmd
帮助1

前言

qt导出可以利用QAxObject去导出word或者excel等。
QAxObject主要是调用QAxObject的 querySubObject、dynamicCall、setProperty等方法,其中方法的参数属性,可以通过word官网查询,具体属性怎么用,暂时也没找到有相应文档,有需要可以结合office官方文档属性和下面分享的或者网上找下资料的,依葫芦画瓢,找规律试一试。
学新通
下面分享导出word的一些常用属性。

Test案例

首先在.pro文件中需要加入:QT = axcontainer
使用时加头文件

#include <QAxObject>

不多bb直接上代码
.cpp文件

#include "widget.h"
#include "ui_widget.h"
#include <QAxObject>
#include <QFile>
#include <QMap>
#include <QDebug>
#include <QFileDialog>
#include <QPushButton>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QAxObject* m_wordWidget = new QAxObject();
    bool flag = m_wordWidget->setControl("Word.Application");//初始化COM对象,新建一个word应用程序
    if(!flag)
    {
        flag = m_wordWidget->setControl("kwps.Application");//尝试用wps打开
        if(!flag)
            return;
    }

    m_wordWidget->setProperty("Visible", true);//设置为可见,false 则不会在界面打开显示

    QAxObject* documents = m_wordWidget->querySubObject("Documents");//获取所有的工作文档(返回一个指向QAxObject包含的COM对象)
    if(!documents)
        return ;

    //法 一
//    QString filePathDot = "D:/qt-test/word/Doc1.dot";
//    QFile file(filePathDot);
//    if(file.exists())
//        documents->dynamicCall("Add(const QString&)",filePathDot);//以Doc1.dot新建一个
//    else
//        return ;
     //法二
    QString filePathDocx = "D:/qt-test/word/test.docx";
    QFile file(filePathDocx);
    if(file.exists())
        documents->dynamicCall("Open(const QString&)",filePathDocx);//直接打开一个空文档
    else
        return;
    //上面描述的方法,都必须要在先创建一个文档


    QAxObject *activeDocument = m_wordWidget->querySubObject("ActiveDocument");//获取当前激活的文档

//    //读取word中的文字
//    QAxObject* pRange = activeDocument->querySubObject("Range()");
//   if (NULL != pRange)
//   {
//       QString text = pRange->property("Text").toString();
//       qDebug()<<"read:"<<text;
//       delete pRange;
//       pRange = NULL;
//   }

   //插入文字
    QAxObject* selection  = m_wordWidget->querySubObject("Selection");
    if(!selection)
        return;

    selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleTitle");//设置标题
    selection->querySubObject("Font")->setProperty("Name","微软雅黑");//设置字体
    selection->querySubObject("Font")->setProperty("Size",20);//设置字体大小
    //selection->querySubObject("Font")->setProperty("Color",0);//设置字体颜色
    selection->dynamicCall("TypeText(const QString&)"," 测试报告");
    selection->dynamicCall("TypeParagraph(void)");//插入回车

    selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleSubtitle");//设置副标题
    selection->querySubObject("Font")->setProperty("Name","微软雅黑");
    selection->querySubObject("Font")->setProperty("Size",15);
    selection->dynamicCall("TypeText(const QString&)","勒布朗·詹姆斯个人信息");
    selection->dynamicCall("TypeParagraph(void)");


    selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleHeading1");//设置标题1
    selection->querySubObject("Font")->setProperty("Name","微软雅黑");
    selection->querySubObject("Font")->setProperty("Size",12);
    selection->dynamicCall("TypeText(const QString&)"," 勒布朗·詹姆斯简介");
    selection->dynamicCall("TypeParagraph(void)");


    selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleBodyTextFirstIndent2");//	正文首行缩进
    selection->querySubObject("Font")->setProperty("Name","微软雅黑");
    selection->querySubObject("Font")->setProperty("Size",10);
    selection->dynamicCall("TypeText(const QString&)","勒布朗·詹姆斯(LeBron James),"
    											"全名勒布朗·雷蒙·詹姆斯(LeBron Raymone James),"
                                                "1984年12月30日出生于美国俄亥俄州阿克伦,美国职业篮球运动员,"
                                                "司职小前锋,绰号“小皇帝”,效力于NBA洛杉矶湖人队。");
    selection->dynamicCall("TypeParagraph(void)");


    //插入图片
    QString picPath = "D:/qt-test/word/whoBetter.jpg";
    QAxObject *Inlineshapes = selection->querySubObject("InlineShapes");
    Inlineshapes->dynamicCall("AddPicture(const QString&)",picPath);
    delete Inlineshapes;
    selection->dynamicCall("TypeParagraph(void)");

    selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleHeading2");//设置标题2
    selection->querySubObject("Font")->setProperty("Name","微软雅黑");
    selection->querySubObject("Font")->setProperty("Size",12);
    selection->dynamicCall("TypeText(const QString&)"," 主要荣誉");
    selection->dynamicCall("TypeParagraph(void)");


    selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleBodyTextFirstIndent2");
    selection->querySubObject("Font")->setProperty("Name","微软雅黑");
    selection->querySubObject("Font")->setProperty("Size",10);
    selection->dynamicCall("TypeText(const QString&)","4届NBA总冠军、NBA总决赛MVP(2012;2013;2016;2020)"
                           "3届NBA全明星正赛MVP(2006;2008;2018)"
                           "2届奥运会金牌(2008,2012)"
                           "2019-20赛季NBA助攻王 [8] "
                           "2007-08赛季NBA得分王"
                           "18届NBA全明星阵容(2005-2022)"
                           "17届NBA最佳阵容(13次一阵,3次二阵,1次三阵)");
    selection->dynamicCall("TypeParagraph(void)");


    selection->dynamicCall("setStyle(WdBuiltinStyle)", "wdStyleHeading3");//设置标题2
    selection->querySubObject("Font")->setProperty("Name","微软雅黑");
    selection->querySubObject("Font")->setProperty("Size",12);
    selection->dynamicCall("TypeText(const QString&)"," 重要事件");
    selection->dynamicCall("TypeParagraph(void)");

    QStringList headList;
    headList<<"时间"<<"事件";

    QMap<int , QString> map;
    map.insert(2003,"2003年NBA选秀状元");
    map.insert(2010,"2010年加盟迈阿密热火队");
    map.insert(2014,"2014年重回克利夫兰骑士队");
    map.insert(2016,"2016年夺得克利夫兰骑士队首冠");
    map.insert(2018,"2018年加盟洛杉矶湖人队");

    //插入表格
    QAxObject *range = selection->querySubObject("Range");
    QAxObject *tables = activeDocument->querySubObject("Tables");
    QAxObject *table = tables->querySubObject("Add(QVariant,int,int)",
    range->asVariant(),map.size() 1,headList.length());//map.size 行

    table->setProperty("Style","网格型");
    table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", 2);//表格自动拉伸列 0固定  1根据内容调整  2 根据窗口调整

    //设置表头
    for(int i=0;i<headList.size();i  )
    {
        table->querySubObject("Cell(Long,Long)",1,i 1)->querySubObject("Range")
            ->querySubObject("Shading")
            ->dynamicCall("BackgroundPatternColorIndex", "wdDarkBlue");//设置表格底色

        table->querySubObject("Cell(Long,Long)",1,i 1)->querySubObject("Range")
                ->querySubObject("ParagraphFormat")
                ->dynamicCall("Alignment", "wdAlignParagraphCenter");//设置表格居中

        table->querySubObject("Cell(Long,Long)",1,i 1)->querySubObject("Range")
        ->dynamicCall("SetText(QString)", headList.at(i));//设置表格内容
        table->querySubObject("Cell(Long,Long)",1,i 1)->querySubObject("Range")
        ->dynamicCall("SetBold(int)", true);//加粗
    }

    //填充表格
    QMap<int , QString>::iterator itor = map.begin();
    for (int j=0; itor != map.end(); itor  ,j  )
    {
        table->querySubObject("Cell(Long,Long)",j 2,1)->querySubObject("Range")
                ->querySubObject("ParagraphFormat")
                ->dynamicCall("Alignment", "wdAlignParagraphLeft");//居左

        table->querySubObject("Cell(Long,Long)",j 2,1)
        ->querySubObject("Range")->querySubObject("Font")
        ->setProperty("Color","wdColorBlack");//设置字体颜色
        table->querySubObject("Cell(Long,Long)",j 2,1)
        ->querySubObject("Range")->dynamicCall("SetText(QString)", QString::number(itor.key()));


        table->querySubObject("Cell(Long,Long)",j 2,2)->querySubObject("Range")
                ->querySubObject("ParagraphFormat")
                ->dynamicCall("Alignment", "wdAlignParagraphLeft");//居左

        table->querySubObject("Cell(Long,Long)",j 2,2)
        ->querySubObject("Range")->querySubObject("Font")
        ->setProperty("Color","wdColorBlack");//设置字体颜色
        table->querySubObject("Cell(Long,Long)",j 2,2)->querySubObject("Range")
        ->dynamicCall("SetText(QString)", itor.value());
    }

    //跳出表格
    QVariantList params;
    params.append(6);
    params.append(0);
    selection->dynamicCall("EndOf(QVariant&, QVariant&)", params).toInt();

    QString filePath2 = "D:/qt-test/word/test2.docx";
    activeDocument->dynamicCall("SaveAs(const QString&)",
    QDir::toNativeSeparators(filePath2));//保存至filePath2,

}

Widget::~Widget()
{
    delete ui;
}


学新通

导出word后

学新通

总结

相应属性可以在office官网 Docs/office VBA/word 找到这里左边可以就可以搜索相应关键字了 。这里以颜色属性为例,这里也直接附上地址, office官方稳定网址
学新通
这里只是简单的实现了标题,副标题,插入文字,表格,图片等。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbgbfc
系列文章
更多 icon
同类精品
更多 icon
继续加载