其他分享
首页 > 其他分享> > 读取word中表格的数据

读取word中表格的数据

作者:互联网

  1. 在pro文件中加入
    QT += axcontainer
  2. 按步骤:
    1)创建Word应用程序对象;
    2)获取文档集;
    3)打开文档;
    4)获取活动表格;
    5)读取表格中的信息;
    QAxObject *myword = new QAxObject("Word.Application");//1)创建Word应用程序对象;
    QAxObject *mydocs = myword->querySubObject("Documents");//2)获取文档集;
    mydocs->dynamicCall("Open(const QString&)","C:\\Users\\Public\\Documents\\1.docx");//3)打开文档;
    QAxObject *document = myword->querySubObject("ActiveDocument");//4)获取活动表格;
    QAxObject *mytable = document->querySubObject("Tables(int)",table);
    
     	int rows = mytable->querySubObject("Rows")->dynamicCall("Count").toInt();
    	int cols = mytable->querySubObject("Columns")->dynamicCall("Count").toInt();
    	qDebug()<<"table:"<<table<<" rows:"<<rows<<" column:"<<cols;
        for(int i = 1; i < rows + 1; i++ )
            for(int j = 1; j < cols + 1; j++)
            {
                QAxObject *item = mytable->querySubObject("Cell(int,int)",i,j);// 5)读取表格中的信息;
                QString itemString = item->querySubObject("Range")->property("Text").toString();
                               qDebug()<<itemString;
            }        

标签:word,读取,表格,int,querySubObject,dynamicCall,mytable,QAxObject
来源: https://blog.csdn.net/weixin_45735254/article/details/122774297