这是一个excel表单,目标是把其中的数据提取出来。
文章学习自:QT中将excel中的数据快速的读取出来显示在tablewidget中/将tablewidget中的数据快速的写入excel中_qt将excel表格中指定范围内容显示在界面中_Jessica_1409573408的博客-CSDN博客
程序如下:
1.打开一个Excel应用程序,并使其可视化。
QAxObject excel("Excel.Application");
excel.setProperty("Visible",true);
2.打开已存在的工作簿
QAxObject *workbooks = excel.querySubObject("WorkBooks");
QString path="D:/Qt/Qt5Book/Qt5.9Samp/happy_prediction_table/build-happy_prediction_table-Desktop_Qt_5_14_2_MSVC2017_32bit-Debug/test.xlsx";
workbooks->dynamicCall("Open (const QString&)",QString(path));
3.初步获取数据
QAxObject *workbook = excel.querySubObject("ActiveWorkBook"); //获取活动工作簿
//获取第一个工作表
QAxObject *worksheet = workbook->querySubObject("Worksheets(int)", 1);
QAxObject *usedrange=worksheet->querySubObject("UsedRange");//获取整个worksheet的范围
QVariant cell=usedrange->dynamicCall("value");//将worksheet的全部内容存储在QVariant变量中
qDebug()<<cell;
结果(这已经是我优化过的了):
QVariant(QVariantList, (
QVariant(QVariantList,
(QVariant(QString, "日期"),
QVariant(QString, "活动类型(特指能使你产\n生成就感或快乐的活动)"),
QVariant(QString, "陪你完成活动的人(如果是独\n自一人,请写明\"我自己\")"), QVariant(QString, "预计快乐程度\n(0~100%,活动\n开始前填写)"),
QVariant(QString, "实际快乐程度\n(0~100%,活动\n结束后填写)")
)
),
QVariant(QVariantList,
(
QVariant(QString, "2023/7/7\t"),
QVariant(QString, "写我有兴趣的程序\t"),
QVariant(QString, "我自己\t"),
QVariant(QString, "30%\t"),
QVariant(QString, "50%\t")
)
),
QVariant(QVariantList,
(
QVariant(QString, "2023/7/7\t"),
QVariant(QString, "跑步\t"),
QVariant(QString, "我自己\t"),
QVariant(QString, "45%\t"),
QVariant(QString, "55%\t")
)
),
QVariant(QVariantList,
(
QVariant(QString, "\t"),
QVariant(QString, "\t"),
QVariant(QString, "\t"),
QVariant(QString, "\t"),
QVariant(QString, "\t")
)
),
QVariant(QVariantList,
(
QVariant(QString, "\t"),
QVariant(QString, "\t"),
QVariant(QString, "\t"),
QVariant(QString, "\t"),
QVariant(QString, "\t")
)
)
)
)
为了更好地看出数据的层次,我做了这样的处理。
x1=QVariant(QString, "日期")
x2=QVariant(QString, "活动类型(特指能使你产\n生成就感或快乐的活动)")
x3=QVariant(QString, "陪你完成活动的人(如果是独\n自一人,请写明\"我自己\")")
x4=QVariant(QString, "预计快乐程度\n(0~100%,活动\n开始前填写)")
x5=QVariant(QString, "实际快乐程度\n(0~100%,活动\n结束后填写)")
QVariant(QVariantList,(x1,x2,x3,x4,x5))
y1=QVariant(QString, "2023/7/7\t")
y2=QVariant(QString, "写我有兴趣的程序\t")
y3=QVariant(QString, "我自己\t")
y4=QVariant(QString, "30%\t")
y5=QVariant(QString, "50%\t")
QVariant(QVariantList, (y1,y2,y3,y4,y5))
z1=QVariant(QString, "2023/7/7\t")
z2=QVariant(QString, "跑步\t")
z3=QVariant(QString, "我自己\t")
z4=QVariant(QString, "45%\t")
z5=QVariant(QString, "55%\t")
QVariant(QVariantList, (z1,z2,z3,z4,z5))
h=QVariant(QString, "\t")
QVariant(QVariantList, (h,h,h,h,h))
化简后:
QVariant(QVariantList,
(
QVariant(QVariantList,(x1,x2,x3,x4,x5)),
QVariant(QVariantList, (y1,y2,y3,y4,y5)),
QVariant(QVariantList, (z1,z2,z3,z4,z5)),
QVariant(QVariantList, (h,h,h,h,h)),
QVariant(QVariantList, (h,h,h,h,h)),
)
)
探索:
因为觉得上面的数据没有规律性,不方便找规律,我建了下面这张表:
QVariant(QVariantList, (
QVariant(QVariantList,
(QVariant(QString, "(1,1)"), QVariant(QString, "(1,2)"), QVariant(QString, "(1,3)"))),
QVariant(QVariantList,
(QVariant(QString, "(2,1)"), QVariant(QString, "(2,2)"), QVariant(QString, "(2,3)")))))
转化:
QVariant(QVariantList, (QVariant(QVariantList, (x)),QVariant(QVariantList, (y))))
x=QVariant(QString, "(1,1)"), QVariant(QString, "(1,2)"), QVariant(QString, "(1,3)")
y=QVariant(QString, "(2,1)"), QVariant(QString, "(2,2)"), QVariant(QString, "(2,3)")
最外层是一个QVariantList,里面是两个QVariant,对应两行数据。
每个QVariant又是一个QVariantList,里面是3个QVariant,对应3列数据。
小贴士:
观察QVariant(QString, "活动类型(特指能使你产\n生成就感或快乐的活动)")
发现它的结构是:QVariant(类型,内容)
含义:类型为QString,内容为"活动类型(特指能使你产\n生成就感或快乐的活动)"
现在已经得到了QVariant类型的数据,它存储的数据类似二维数组,这里把它转化为
QList<QList<QVariant>>
使用这个函数:
Qvariant2listlistVariant(const QVariant &var,QList<QList<QVariant>> &ret)
{
QVariantList varrows=var.toList();
if(varrows.isEmpty())
{
return;
}
else {
const int rowcount=varrows.size();//行数
qDebug()<<"excel中有几行"<<rowcount;
QVariantList rowdata;
for(int i=0;i<rowcount;i++)
{
rowdata=varrows[i].toList();//将每一行的值存入到list中
ret.push_back(rowdata);
}
}
}
语句解析:
QVariantList varrows=var.toList();
使用函数:
QList<QVariant> toList() const
QList<QVariant>与QVariantList是同义的。那么QList<QList<QVariant>>等同于QList<QVariantList>。
varrows的内容:
(
QVariant(QVariantList,
(
QVariant(QString, "(1,1)"), QVariant(QString, "(1,2)"), QVariant(QString, "(1,3)")
)
),
QVariant(QVariantList,
(
QVariant(QString, "(2,1)"), QVariant(QString, "(2,2)"), QVariant(QString, "(2,3)")
)
)
)
4.这时候就可以把 QList<QList<QVariant>> ret中的信息提取出来了。
int row=ret.size();
//qDebug()<<row;
for(int i=0;i<row;i++)
{
QList<QVariant> one=ret.at(i);
int column=one.size();
//qDebug()<<column;
for(int j=0;j<column;j++)
{
QString strVal=one.at(j).toString();
qDebug()<<strVal;
/*在这里进行数据提取操作*/
}
}
5. workbook->dynamicCall("Close()"); //关闭工作簿
6.excel.dynamicCall("Quit()");//关闭excel
程序完整版:
#include <QApplication>
#include <QAxObject>
#include <QDebug>
#include <QList>
#pragma execution_character_set("utf-8")
void Qvariant2listlistVariant(const QVariant &var,QList<QList<QVariant>> &ret)
{
QVariantList varrows=var.toList();
qDebug()<<varrows;
if(varrows.isEmpty())
{
return;
}
else{
const int rowcount=varrows.size();//行数
//qDebug()<<"excel中有几行"<<rowcount;
QVariantList rowdata;
for(int i=0;i<rowcount;i++)
{
rowdata=varrows[i].toList();//将每一行的值存入到list中
ret.push_back(rowdata);
}
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QAxObject excel("Excel.Application");
//不对excel进行显示
excel.setProperty("Visible",true);
//添加新的工作簿
QAxObject *workbooks = excel.querySubObject("WorkBooks");
//打开已存在的工作簿
QString path="D:/Qt/Qt5Book/Qt5.9Samp/happy_prediction_table/build-happy_prediction_table-Desktop_Qt_5_14_2_MSVC2017_32bit-Debug/test.xlsx";
workbooks->dynamicCall("Open (const QString&)",QString(path));
//获取活动工作簿
QAxObject *workbook = excel.querySubObject("ActiveWorkBook");
//获取第一个工作表
QAxObject *worksheet = workbook->querySubObject("Worksheets(int)", 2);
QAxObject *usedrange=worksheet->querySubObject("UsedRange");//获取整个worksheet的范围
QVariant cell=usedrange->dynamicCall("value");//将worksheet的全部内容存储在QVariant变量中
QList<QList<QVariant>> ret;
Qvariant2listlistVariant(cell,ret);//将QVariant转换为QList<QList<QVariant>>
int row=ret.size();
//qDebug()<<row;
for(int i=0;i<row;i++)
{
QList<QVariant> one=ret.at(i);
int column=one.size();
//qDebug()<<column;
for(int j=0;j<column;j++)
{
QString strVal=one.at(j).toString();
qDebug()<<strVal;
}
}
workbook->dynamicCall("Close()"); //关闭工作簿
excel.dynamicCall("Quit()"); //关闭excel
return a.exec();
}