pro文件中加入一下语句: CONFIG += qaxcontainer
.h或.cpp文件中引入:
#include <QAxObject>
#include <ActiveQt/QAxObject>
测试实现代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QDebug>
#include <QMap>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString modelName = "ZDE";//modelName由传入参数获取
QString fileName = "E:\\Touchpad\\TP.xlsx";
if(fileName.isEmpty()){
//return false;传入的文件名是否为空
qDebug()<<"fileName not find";
}
qDebug()<<"1 find fileName";
QFile file(fileName);
if(file.exists()){
file.close();//只用做判断文件是否存在 excel文件用专有方式打开
}else{
qDebug()<<"file not find";
}
qDebug()<<"2 find file";
QAxObject* m_Excel = new QAxObject;
bool isOpened = m_Excel->setControl("Excel.Application");//用office打开excel文档
qDebug()<<"3 make office open Excel";
if(!isOpened){
isOpened = m_Excel->setControl("ket.Application");//用wps打开excel文档
if (!isOpened)
{
// 返回异常(Excel和WPS打开excel文件均未成功)
}
}
QAxObject *m_WorkBooks = m_Excel->querySubObject("WorkBooks");
//打开指定excel文件
m_WorkBooks->dynamicCall("Open(const QString &)",fileName);
//获取当前活动工作簿
QAxObject *m_WorkBook = m_Excel->querySubObject("ActiveWorkBook");
if (m_WorkBook == nullptr)
{
// 返回异常(当前活动工作簿打开失败)
}
//获取该excel的所有工作表
QAxObject *m_WorkSheets = m_WorkBook->querySubObject("WorkSheets");
if(m_WorkSheets == nullptr)
{
// 返回异常(当前表格集WorkSheets不存在,打开失败!)
}
//获取该excel的工作表个数
int m_WorkSheetCount = m_WorkSheets->dynamicCall("Count").toInt();
QAxObject * worksheet = m_WorkBook->querySubObject("WorkSheets(int)", 1); //访问第一个工作表
QAxObject * usedrange = worksheet->querySubObject("UsedRange");//获取权限
QAxObject * rows = usedrange->querySubObject("Rows");
int Rows = rows->property("Count").toInt(); //获取总行数
int cols = usedrange->querySubObject("Columns")->property("Count").toInt();//获取总列数
//获取数据起始行数和列数(可以解决不规则Excel)
int iRow = usedrange->property("Row").toInt();
int iCol = usedrange->property("Column").toInt();
qDebug()<<" iCol="+QString::number(iCol)+",iRow="+QString::number(iRow);
//QAxObject *range = worksheet->querySubObject("Cells(int,int)",iRow,1);
int modelRow = 0;//记录查找到的列数
for(int i = iRow;i<=Rows;i++){//获取第一列所有数据 判断机种
QString cellTdext = worksheet->querySubObject("Cells(int,int)",i,1)->dynamicCall("Value2()").toString();//记录查出数据
qDebug()<<cellTdext;
ui->textBrowser->append(cellTdext);
if(modelName == cellTdext){
modelRow = i;
break;
}
}
if(!modelRow){
// 返回异常(excel表中未找到指定数据)
}
QMap<QString,QString> map;
QString ParameterType,ParameterContent;
for(int j = iCol;j<=cols;j++){//查询第一行目录数据及modelRow行的所有数据 一一对应存入map中
ParameterType = worksheet->querySubObject("Cells(int,int)",1,j)->dynamicCall("Value2()").toString();//第一行所有数据
ParameterContent = worksheet->querySubObject("Cells(int,int)",modelRow,j)->dynamicCall("Value2()").toString();//指定modelRow行所有数据
map.insert(ParameterType,ParameterContent);
//ui->textBrowser->append(ParameterType+"="+ParameterContent);
}
if(map.isEmpty()){
// 返回异常(未找到指定数据map为空)
}
/*加一个参数 选择使用excel参数 还是 配置档参数
* 将上面打包到一个function中->使用excel参数调用它 将数据存入map map使用全局变量
* 下面的也打包成一个function 在加载参数时传入指定的 查找出来即可
*
*
*/
if(map.contains("TopL")){
QMap<QString,QString>::iterator it = map.find("TopL");
QString content = it.value();
qDebug()<<"TopL="<<content;
ui->textBrowser->append("TopL="+content);
}
//最后返回true说明查找到表中数据并存入map
this->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
实际需求实现:将从excel中获取的指定行数据存入map中,需要时通过列名对应map-key获取
vi_func.mDLLGetConfigINT("getParameterWay",getParameterWay);//获取参数方式 0.配置档 1.excel文档(TP.xlsx)
if(getParameterWay){//从excel获取参数
vi_func.mDLLLogCatfunc("getParameterWay=1,从excel获取参数",eLog_Normal);
string excelPath;
bool ep = vi_func.mDLLGetConfigString("excelPath",excelPath);//拿到excel文件路径 例:E:\\Touchpad\\TP.xlsx
QString fileName,modelName;
if(!ep){
vi_func.mDLLLogCatfunc("Get excelPath fail - 获取excel路径失败!",eLog_Error);//获取excel路径失败
return "Def:EPFail";
}else{
fileName = QString::fromStdString(excelPath);
}
// 拆分Totalinput拿到modelName
QString temp =QString::fromStdString(Totalinput);
QStringList tempList=temp.split(";");
for (int i=0;i<tempList.size();i++){
if(tempList[i].contains("Model=", Qt::CaseInsensitive))
{
modelName=tempList[i].mid(6,tempList[i].length()-6);
}
}
if(openExcel(vi_func,fileName,modelName)){
vi_func.mDLLLogCatfunc("获取excel参数成功",eLog_Normal);
}else{
vi_func.mDLLLogCatfunc("Get excelParameter fail - 获取excel参数失败!",eLog_Error);//获取参数失败
return "Def:EPFail";
} }
//从excel中查找对应model的调试参数
bool openExcel(DLLFunc vi_func,QString fileName,QString modelName){
//QString modelName = "ZDE";//modelName由传入参数获取
//QString fileName = "E:\\Touchpad\\TP.xlsx";
vi_func.mDLLLogCatfunc("fileName="+fileName.toStdString(),eLog_Normal);
vi_func.mDLLLogCatfunc("modelName="+modelName.toStdString(),eLog_Normal);
if(fileName.isEmpty()){
vi_func.mDLLLogCatfunc("传入的excel文件名为空!",eLog_Error);
return false;//传入的文件名是否为空
}
QFile file(fileName);
if(file.exists()){
file.close();//只用做判断文件是否存在 excel文件用专有方式打开
}else{
vi_func.mDLLLogCatfunc("在指定路径未找到传入的excel文件名!",eLog_Error);
return false;
}
QAxObject* m_Excel = new QAxObject;
bool isOpened = m_Excel->setControl("Excel.Application");//用office打开excel文档
if(!isOpened){
isOpened = m_Excel->setControl("ket.Application");//用wps打开excel文档
if (!isOpened)
{
vi_func.mDLLLogCatfunc("Excel和WPS打开excel文件均未成功!",eLog_Error);
return false;// 返回异常(Excel和WPS打开excel文件均未成功)
}
}
QAxObject *m_WorkBooks = m_Excel->querySubObject("WorkBooks");
//打开指定excel文件
m_WorkBooks->dynamicCall("Open(const QString &)",fileName);
//获取当前活动工作簿
QAxObject *m_WorkBook = m_Excel->querySubObject("ActiveWorkBook");
if (m_WorkBook == nullptr)
{
vi_func.mDLLLogCatfunc("当前活动工作簿打开失败!",eLog_Error);
return false;// 返回异常(当前活动工作簿打开失败)
}
//获取该excel的所有工作表
QAxObject *m_WorkSheets = m_WorkBook->querySubObject("WorkSheets");
if(m_WorkSheets == nullptr)
{
vi_func.mDLLLogCatfunc("当前表格集WorkSheets不存在,打开失败!",eLog_Error);
return false;// 返回异常(当前表格集WorkSheets不存在,打开失败!)
}
//获取该excel的工作表个数
int m_WorkSheetCount = m_WorkSheets->dynamicCall("Count").toInt();
QAxObject * worksheet = m_WorkBook->querySubObject("WorkSheets(int)", 1); //访问第一个工作表
QAxObject * usedrange = worksheet->querySubObject("UsedRange");//获取权限
int Rows = usedrange->querySubObject("Rows")->property("Count").toInt(); //获取总行数
int cols = usedrange->querySubObject("Columns")->property("Count").toInt();//获取总列数
//获取数据起始行数和列数(可以解决不规则Excel)
int iRow = usedrange->property("Row").toInt();
int iCol = usedrange->property("Column").toInt();
int modelRow = 0;//记录查找到的列数
for(int i = iRow;i<=Rows;i++){//获取第一列所有数据 判断机种
QString cellTdext = worksheet->querySubObject("Cells(int,int)",i,1)->dynamicCall("Value2()").toString();//记录查出数据
if(modelName == cellTdext){
modelRow = i;
break;
}
}
if(!modelRow){
vi_func.mDLLLogCatfunc("excel表中未找到指定机种数据!",eLog_Error);
return false;// 返回异常(excel表中未找到指定机种)
}
//QMap<QString,QString> excelMap; 此为全局变量 走点中需要使用
QString ParameterType,ParameterContent;
for(int j = iCol;j<=cols;j++){//查询modelRow行的所有数据 存入map中
ParameterType = worksheet->querySubObject("Cells(int,int)",1,j)->dynamicCall("Value2()").toString();//第一行所有数据(各参数名称)
ParameterContent = worksheet->querySubObject("Cells(int,int)",modelRow,j)->dynamicCall("Value2()").toString();//指定modelRow行所有数据
excelMap.insert(ParameterType,ParameterContent);//excelMap定义的是全局变量
//ui->textBrowser->append(ParameterType+"="+ParameterContent);
}
if(excelMap.isEmpty()){
vi_func.mDLLLogCatfunc("未找到指定数据map为空!",eLog_Error);
return false;// 返回异常(未找到指定数据map为空)
}
//最后返回true说明查找到表中数据并存入map
return true;
}
获取map中的参数:
//从excelMap中查找对应参数 parameter是需要的参数名称也就是excel的列名对应map-key
QString getParameter(QString parameter){
if(excelMap.contains(parameter)){
QMap<QString,QString>::iterator it = excelMap.find(parameter);
QString content = it.value();
return content;
}else{
return "NULL";
}
}
获取实例:
int TopL;
TopL= getParameter("TopL").toInt();//TopL是参数名称