zoukankan      html  css  js  c++  java
  • 从一个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插入->标签,按照提示操作即可

  • 相关阅读:
    C#操作REDIS例子
    A C# Framework for Interprocess Synchronization and Communication
    UTF8 GBK UTF8 GB2312 之间的区别和关系
    开源项目选型问题
    Mysql命令大全——入门经典
    RAM, SDRAM ,ROM, NAND FLASH, NOR FLASH 详解(引用)
    zabbix邮件报警通过脚本来发送邮件
    centos启动提示unexpected inconsistency RUN fsck MANUALLY
    rm 或者ls 报Argument list too long
    初遇Citymaker (六)
  • 原文地址:https://www.cnblogs.com/zhugaopeng/p/7118531.html
Copyright © 2011-2022 走看看