• 从一个word文件中读取所有的表格和标题(2)


    上一篇文章主要讲了从word底层xml中获取表格和标题的方法,但是存在一个问题:word文件必须是docx格式的。如果为doc格式的,可以有两种解决方案:

    一、把doc文件转换成docx格式文件,用上一种办法来处理

    二、利用com组件和word的标签机制去处理

    下面直接贴代码:

    1)获取表格数据

     1 void MyWord::getTableData( const int index, QVector<QVector<QString> > &vec )
     2 {
     3         m_word = new QAxObject(parent);
     4         if(!m_word->setControl("Word.Application"))
     5     {
     6         QMessageBox::warning(0, tr("警告"), tr("绑定word控件失败"), tr("确定"));
     7         return ;
     8     }
     9     m_word->setProperty("Visible", false); //设置窗体不可见
    10     m_word->setProperty("DisplayAlerts", false); //不现实警告信息
    11 
    12     m_documents = m_word->querySubObject("Documents");
    13     m_documents->dynamicCall("Open(const QString &)", filename); //打开文件
    14     m_document = m_word->querySubObject("ActiveDocument"); //获取当前被激活文档
    15 
    16     QAxObject *table = m_document->querySubObject("Tables(int)", index); //获取表格
    17     if(!table)
    18     {
    19         return ;
    20     }
    21 23     int rowCnt = table->querySubObject("Rows")->property("Count").toInt(); //行数
    24     int colCnt = table->querySubObject("Columns")->property("Count").toInt(); //列数
    25     for(int nR = 1; nR <= rowCnt; ++nR)
    26     {
    27         QVector<QString> tmpVec;
    28         for(int nC = 1; nC <= colCnt; ++nC)
    29         {
    30             QAxObject *cell = table->querySubObject("Cell(int, int)", nR, nC);//每一个单元格
    31             if(!cell)
    32             {
    33                 tmpVec.push_back("");
    34                 continue;
    35             }
    36             QString text = cell->querySubObject("Range")->property("Text").toString();//获取单元格文本
    37             tmpVec.push_back(text.remove(text.size() - 1, 1));//去除文本的换行符
    38         }
    39         vec.push_back(tmpVec);
    40     }
    41 }    

     2)读取标签相关区域文本

    QString MyWord::getTextFromBookmark( const int index /*= 1*/ )
    {
        if(index < 1 || index > getBookmarkCount() && !m_document)
        {
            return "";
        }
    
        QAxObject *bookmark = m_document->querySubObject("Bookmarks(int)", index);
        if(bookmark)
        {
            QAxObject *range = bookmark->querySubObject("Range");
            if(range)
            {
                return range->property("Text").toString();
            }
        }
        return "";
    }
    
    QString MyWord::getTextFromBookmark( const QString &bookmarkName )
    {
        if(!m_document)
        {
            return "";
        }
    
        QAxObject *bookmark = m_document->querySubObject("Bookmarks(const QString &)", bookmarkName);
        if(bookmark)
        {
            QAxObject *range = bookmark->querySubObject("Range");
            if(range)
            {
                return range->property("Text").toString();
            }
        }
        return "";
    }

    3)如何插入标签

    选中要插如标签的文本,word插入->标签,按照提示操作即可

  • 相关阅读:
    洛谷P3819 松江1843路
    洛谷P1896 [SCOI2005]互不侵犯King
    洛谷P1197 [JSOI2008]星球大战
    洛谷P1171 售货员的难题
    2017-10-24 NOIP模拟赛
    LibreOJ #6192. 「美团 CodeM 复赛」城市网络
    洛谷P2258 子矩阵
    Cogs 9. 中心台站建设
    Cogs 6. 线型网络
    洛谷P3138 [USACO16FEB]负载平衡Load Balancing_Silver
  • 原文地址:https://www.cnblogs.com/zhugaopeng/p/7118531.html
Copyright © 2020-2023  润新知