在Qt保存数据时,用Excel可以很好的对数据进行分类,极大方便后期数据分析。
以下代码段实现了目标路径下创建Excel文件(如果已有Excel文件则打开追加数据)和编辑保存功能。
实现单线程和多线程使用。
//多线程必须初始化,需包含#include "qt_windows.h"头文件.
CoInitializeEx(NULL, COINIT_MULTITHREADED);
QString desktop_path =
QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);//获取桌面路径.
QString templatePath = desktop_path + "/Qt_Excel_Test4.xlsx"; //设置文件路径、名、格式.
templatePath = QDir::toNativeSeparators(templatePath); //转换一下路径,让windows能够识别.
QFileInfo info(templatePath);
if(!info.exists())
{
QAxObject *excel = new QAxObject(); //建立excel操作对象.
excel->setControl("Excel.Application"); //连接Excel控件.
excel->setProperty("Visible", false); //true显示窗体看效果,即打开excel.
excel->setProperty("DisplayAlerts", false); //false不显示任何警告信息,true会出现类似“文件已存在,是否替换”的提示.
QAxObject *workbooks = excel->querySubObject("WorkBooks"); //获取工作簿集合.
QAxObject *workbook = workbooks->querySubObject("Add"); //创建新的表格.
QAxObject *work_book = excel->querySubObject("ActiveWorkBook"); //获取当前工作簿.
QAxObject *worksheet = work_book->querySubObject("Sheets(int)",1); //获取表单1
//设置标题.
QAxObject *cellA,*cellB,*cellC,*cellD,*cellE,*cellF,*cellG,*cellH,*cellI,*cellJ,*cellK,*cellL,*cellM,*cellN,*cellO;
int cellrow=1; //表格第一行.
QString A="A"+QString::number(cellrow); //设置要操作的单元格,如A1.
QString B="B"+QString::number(cellrow);
QString C="C"+QString::number(cellrow);
QString D="D"+QString::number(cellrow);
QString E="E"+QString::number(cellrow);
QString F="F"+QString::number(cellrow);
QString G="G"+QString::number(cellrow);
QString H="H"+QString::number(cellrow);
QString I="I"+QString::number(cellrow);
QString J="J"+QString::number(cellrow);
QString K="K"+QString::number(cellrow);
QString L="L"+QString::number(cellrow);
QString M="M"+QString::number(cellrow);
QString N="N"+QString::number(cellrow);
QString O="O"+QString::number(cellrow);
cellA = worksheet->querySubObject("Range(QVariant, QVariant)",A);//获取单元格.
cellB = worksheet->querySubObject("Range(QVariant, QVariant)",B);
cellC = worksheet->querySubObject("Range(QVariant, QVariant)",C);
cellD = worksheet->querySubObject("Range(QVariant, QVariant)",D);
cellE = worksheet->querySubObject("Range(QVariant, QVariant)",E);
cellF = worksheet->querySubObject("Range(QVariant, QVariant)",F);
cellG = worksheet->querySubObject("Range(QVariant, QVariant)",G);
cellH = worksheet->querySubObject("Range(QVariant, QVariant)",H);
cellI = worksheet->querySubObject("Range(QVariant, QVariant)",I);
cellJ = worksheet->querySubObject("Range(QVariant, QVariant)",J);
cellK = worksheet->querySubObject("Range(QVariant, QVariant)",K);
cellL = worksheet->querySubObject("Range(QVariant, QVariant)",L);
cellM = worksheet->querySubObject("Range(QVariant, QVariant)",M);
cellN = worksheet->querySubObject("Range(QVariant, QVariant)",N);
cellO = worksheet->querySubObject("Range(QVariant, QVariant)",O);
cellA->dynamicCall("SetValue(const QVariant&)",QVariant(QStringLiteral("序号"))); //设置单元格的值.
cellB->dynamicCall("SetValue(const QVariant&)",QVariant(QStringLiteral("QRS波峰值")));
cellC->dynamicCall("SetValue(const QVariant&)",QVariant(QStringLiteral("QRS峰值索引")));
cellD->dynamicCall("SetValue(const QVariant&)",QVariant(QStringLiteral("Q波数值")));
cellE->dynamicCall("SetValue(const QVariant&)",QVariant(QStringLiteral("Q波数值的索引")));
cellF->dynamicCall("SetValue(const QVariant&)",QVariant(QStringLiteral("准S波的值")));
cellG->dynamicCall("SetValue(const QVariant&)",QVariant(QStringLiteral("准S波的值的索引")));
cellH->dynamicCall("SetValue(const QVariant&)",QVariant(QStringLiteral("QR斜率极大值")));
cellI->dynamicCall("SetValue(const QVariant&)",QVariant(QStringLiteral("QR斜率极大值索引")));
cellJ->dynamicCall("SetValue(const QVariant&)",QVariant(QStringLiteral("QR斜率极大值对应的ecg值")));
cellK->dynamicCall("SetValue(const QVariant&)",QVariant(QStringLiteral("Q波和准S波的差值")));
cellL->dynamicCall("SetValue(const QVariant&)",QVariant(QStringLiteral("QRS波宽度")));
cellM->dynamicCall("SetValue(const QVariant&)",QVariant(QStringLiteral("ST段均值")));
cellN->dynamicCall("SetValue(const QVariant&)",QVariant(QStringLiteral("0.15mV标志位")));
cellO->dynamicCall("SetValue(const QVariant&)",QVariant(QStringLiteral("1mV10mS标志位")));
workbook->dynamicCall("SaveAs(const QString&)",QDir::toNativeSeparators(templatePath));//保存至filepath,注意一定要用QDir::toNativeSeparators将路径中的"/"转换为"\",不然一定保存不了.
workbook->dynamicCall("Close()");//关闭工作簿.
excel->dynamicCall("Quit()");//关闭excel.
delete excel;
excel=NULL;
qDebug()<<QStringLiteral("Excel文件不存在,创建新文件!");
//return;
}
if(!templatePath.isEmpty())
{
QAxObject *excel = new QAxObject(); //建立excel操作对象.
excel->setControl("Excel.Application"); //连接Excel控件.
excel->setProperty("Visible", false); //true显示窗体看效果,即打开excel.
excel->setProperty("DisplayAlerts", false); //false不显示任何警告信息,true会出现类似“文件已存在,是否替换”的提示.
QAxObject *workbooks = excel->querySubObject("WorkBooks");
QAxObject *workbook = workbooks->querySubObject("Open(const QString&)",QDir::toNativeSeparators(templatePath) ); //打开已有的Excel表格.
excel->setProperty("Caption", "Qt Excel"); //标题为Qt Excel
QAxObject *work_book = excel->querySubObject("ActiveWorkBook");
QAxObject *worksheet = work_book->querySubObject("Sheets(int)",1); //获取表单1
QAxObject *usedRange = worksheet->querySubObject("UsedRange"); //sheet范围.
QAxObject *Rows = usedRange->querySubObject("Rows");// 获得起始行数
//QAxObject *Columns = usedRange->querySubObject("Columns");//获得起始列数
int row_count = Rows->property("Count").toInt();
qDebug()<<QStringLiteral("空行位置:")<<row_count;
//批量一次性设置A6~I106所在内容.
int newRows = row_count+1;
int newColumns = newRows+4;
QString str = "A"+QString::number(newRows)+":"+"O"+QString::number(newColumns);
qDebug()<<str;
QAxObject *user_range = worksheet->querySubObject("Range(const QString&)",str);
QList<QList<QVariant> > datas;
for(int i=newRows;i<newRows+5;i++)
{
QList<QVariant> rows;
for(int j=1;j<16;j++)
{
if(j==1)
rows.append(i-1);
else
rows.append(i*j);
}
datas.append(rows);
}
//把QList<QList<QVariant> > 转为QVariant,用于快速写入的.
QVariantList vars;
const int rows = datas.size();
for(int i=0;i<rows;++i)
{
vars.append(QVariant(datas[i]));
}
QVariant var = QVariant(vars);
user_range->setProperty("Value", var);
workbook->dynamicCall("SaveAs(const QString&)",QDir::toNativeSeparators(templatePath)); //保存至filepath,注意一定要用QDir::toNativeSeparators将路径中的"/"转换为"\",不然一定保存不了.
workbook->dynamicCall("Close()"); //关闭工作簿.
excel->dynamicCall("Quit()"); //关闭excel.
delete excel;
excel=NULL;
//QMessageBox::information(NULL,"",QStringLiteral("Excel数据保存成功!"));
qDebug()<<QStringLiteral("Excel数据保存成功!");
}