获取文件的绝对值路径
QFileInfo info(QStringLiteral("../GongCheng1/file/阿达.txt"));
QString strFile=info.absoluteFilePath();
1 //QT+=axcontainer 2 3 #include <QFileDialog> 4 #include <QAxObject> 5 #include <QVariant> 6 #include <QList> 7 8 void getExcelContent(QList<QList<QVariant>>& result) 9 { 10 QString filePath=QFileDialog::getOpenFileName( 11 this, QStringLiteral("选择Excel文件"), "", 12 QStringLiteral("Exel file(*.xls *.xlsx)")); 13 if(filePath.isEmpty)return; 14 15 QAxObject* excel=new QAxObject(this); 16 excel->setControl("Excel.Application");//连接Excel控件 17 excel->dynamicCall("SetVisible (bool Visible)","false");//不显示窗体 18 excel->setProperty("DisplayAlerts", false);//不显示任何警告信息。如果为true那么在关闭是会出现类似“文件已修改,是否保存”的提示 19 20 //打开文件 21 QAxObject *workbooks = excel->querySubObject("WorkBooks");//获取工作簿集合 22 QAxObject *workbook = excel->querySubObject("Open (const QString &)", filePath);//获取当前工作簿 23 24 //打开表 25 //QAxObject *worksheets = workbook->querySubObject("Sheets");//获取工作表集合 26 QAxObject *worksheet = worksheets->querySubObject("Item(int)",1);//工作表1,即sheet1 27 28 //获取表中的数据到QVariant中 29 QAxObject *usedRange=worksheet->querySubObject("UsedRange"); 30 QVariant var=usedRange->dynamicCall("value"); 31 32 QVariantList varRowContents=var.toList(); 33 const int rowCount=varRowContents.size(); 34 QVariantList tmp; 35 for(int i=0; i<rowCount; ++i) 36 { 37 tmp=varRowContents[i].toList(); 38 result.append(tmp); 39 }
//删除,防止程序关闭后有excel进程残留
excel->dynamicCall("Quit(void)");
delete excel;
40 }