本文主要简单介绍本人使用Qt5通过QAxObject根据实时数据生成相应的Excel的过程。
具体流程:
一、准备工作
要使用QAxObject首先要在.pro文件中添加axcontainer
QT += core gui serialport axcontainer
在操作Excel的类中记得要添加头文件#include <QAxObject>
在实际使用过程中需求是在win10/win11环境下 存储数据的时候要先打开个弹窗确定文件存储的位置,有个这么个弹窗确定存储的位置和文件名,效果和代码如下
//获取当前目录位置为默认路径
QString currentPath = QCoreApplication::applicationDirPath()+"/motor log";
//打开一个弹窗
QString file_path = QFileDialog::getSaveFileName(this,"选择存储位置",currentPath,"*.xlsx;;*.xls");
二、操作Excel相关
首先创建个QAxObject对象
QAxObject *excel = new QAxObject;
设置生成的excle对象用于与excel进行交互
if(excel->setControl("Excel.Application")){
//运行逻辑
}
一些基础设置
excel->dynamicCall("SetVisible (bool Visible)","false"); //不显示当前窗体
excel->setProperty("DisplayAlerts", false); //不显示任何警告消息
由于每次都是新建excel而不是对已有的excel进行的操作所以还会对工作簿进行初始化的一些操作
QAxObject *workBooks = excel->querySubObject("WorkBooks");//获取工作簿集合
workBooks->dynamicCall("Add"); //新建一个工作簿
QAxObject *workBook = excel->querySubObject("ActiveWorkBook"); //获取当前工作簿
QAxObject *workSheet = workBook->querySubObject("Worksheets(int)", 1); //获取第一个工作表(后面的参数代表的是第几张工作表)
对标题行进行一些特殊处理
QAxObject *cell;
//标题行
cell = workSheet->querySubObject("Cells(int, int)", 1, 1);
cell->querySubObject("Font")->setProperty("Size", 15);//对字体进行设置
//调整行高
workSheet->querySubObject("Range(const QString&)", "1:1")->setProperty("RowHeight", 20);
//合并标题行
QString cellTitle;
cellTitle.append("A1:K1");
//cellTitle.append(QString::number(1));
QAxObject *range = workSheet->querySubObject("Range(const QString&)", cellTitle);
QDateTime dateTime= QDateTime::currentDateTime();//获取系统当前的时间
QString str = dateTime .toString("yyyy-MM-dd hh:mm:ss");
range->setProperty("Value2","数据记录开始时间:"+str);
range->setProperty("WrapText", true);//自动换行
range->setProperty("MergeCells", true);//合并单元格
range->setProperty("HorizontalAlignment", -4108);//单元格水平对齐
range->setProperty("VertivcalAlignment", -4108);//单元格垂直对齐
正常存储数据示例
int rowIndex = 1;//行号
int colIndex = 1;//列号
QString str = "123";
//在指定行列的单元格设置数据
workSheet->querySubObject("Cells(int, int)", rowIndex, colIndex++)->dynamicCall("SetValue(const QString&)",str);
如果对某个区域内有设置外边框线与内部框线的需求可通过下列代码进行设置
void SaveAsExcel::setExcelBoreds(QAxObject* bordersRange){
//区域内部水平框线
QAxObject* borders_inside_hor = bordersRange->querySubObject("Borders(xlInsideHorizontal)");
//区域内部垂直框线
QAxObject* borders_inside_ver = bordersRange->querySubObject("Borders(xlInsideVertical)");
//左边框线
QAxObject* borders_left = bordersRange->querySubObject("Borders(xlEdgeLeft)");
//右边框线
QAxObject* borders_right = bordersRange->querySubObject("Borders(xlEdgeRight)");
//顶边框线
QAxObject* borders_top = bordersRange->querySubObject("Borders(xlEdgeTop)");
//下边框线
QAxObject* borders_bottom = bordersRange->querySubObject("Borders(xlEdgeBottom)");
QList<QAxObject*> border_list = {borders_inside_hor, borders_inside_ver};
for (auto& one : border_list) {
one->setProperty("Color", QColor(0, 0, 0));//设置颜色
one->setProperty("LineStyle",2);//设置线型虚线
one->setProperty("Weight",2.25);//设置宽度
one->setProperty("ColorIndex",1);
}
border_list = {borders_left, borders_right, borders_top, borders_bottom};
for (auto& one : border_list) {
one->setProperty("Color", QColor(0, 0, 0));//设置颜色
one->setProperty("LineStyle",1);//设置线型实线
one->setProperty("Weight",3);//设置宽度
one->setProperty("ColorIndex",1);
}
}
最后数据存储完成后文件的保存关闭与退出
workBook->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(fileName)); //保存到fileName的文件
workBook->dynamicCall("Close()"); //关闭工作簿
excel->dynamicCall("Quit()"); //关闭excel