- 在pro文件中加入
QT += axcontainer - 按步骤:
1)创建Word应用程序对象;
2)获取文档集;
3)打开文档;
4)获取活动表格;
5)读取表格中的信息;
QAxObject *myword = new QAxObject("Word.Application");
QAxObject *mydocs = myword->querySubObject("Documents");
mydocs->dynamicCall("Open(const QString&)","C:\\Users\\Public\\Documents\\1.docx");
QAxObject *document = myword->querySubObject("ActiveDocument");
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);
QString itemString = item->querySubObject("Range")->property("Text").toString();
qDebug()<<itemString;
}