void queryOneObject(QAxObject *worksheet,QString label,int row,QString data){
? ? QAxObject *cellOne;
? ? label.append(QString::number(row));
? ? cellOne = worksheet->querySubObject("Range(QVariant, QVariant)",label);
? ? cellOne->dynamicCall("SetValue(const QVariant&)",QVariant(data));
}
void? saveout(){
//獲取保存路徑
? ? QString filepath=QFileDialog::getSaveFileName(this,tr("Save"),".",tr(" (*.xlsx)"));
? ? if(filepath.isEmpty()){
? ? ? ? ui->label_Tip->hide();
? ? ? ? qDebug()<<"創(chuàng)建exel文件失斉呶纹笼!";
? ? ? ? return;
? ? }else{
? ? ? ? ui->label_Tip->show();
? ? }
? ? QVector exlData;
? ? exlData = actionDone::getInstance()->mSaveoutExl;
? ? int rows = exlData.size();
? ? qDebug()<<"待導(dǎo)出:"<<rows<<"行";
? ? if(rows==0){
? ? ? ? QMessageBox::information(nullptr,"提示",start+"--"+end+"無導(dǎo)出記錄? ");
? ? ? ? return;
? ? }
? ? QAxObject *excel = new QAxObject(this);
? ? //連接Excel控件
? ? excel->setControl("Excel.Application");
? ? //不顯示窗體
? ? excel->dynamicCall("SetVisible (bool Visible)","false");
? ? //不顯示任何警告信息锄弱。如果為true那么在關(guān)閉是會(huì)出現(xiàn)類似“文件已修改超埋,是否保存”的提示
? ? excel->setProperty("DisplayAlerts", false);
? ? //獲取工作簿集合
? ? QAxObject *workbooks = excel->querySubObject("WorkBooks");
? ? //新建一個(gè)工作簿
? ? workbooks->dynamicCall("Add");
? ? //獲取當(dāng)前工作簿
? ? QAxObject *workbook = excel->querySubObject("ActiveWorkBook");
? ? //獲取工作表集合
? ? QAxObject *worksheets = workbook->querySubObject("Sheets");
? ? //獲取工作表集合的工作表1笔刹,即sheet1
? ? QAxObject *worksheet = worksheets->querySubObject("Item(int)",1);
? ? //設(shè)置標(biāo)題
? ? int cellrow=1;
? ? queryOneObject(worksheet,"A",cellrow,"姓名");
? ? queryOneObject(worksheet,"B",cellrow,"年齡");
? ? queryOneObject(worksheet,"C",cellrow,"號碼");
? ? queryOneObject(worksheet,"D",cellrow,"出生地");
? ? queryOneObject(worksheet,"E",cellrow,"愛好");
? ? queryOneObject(worksheet,"F",cellrow,"情況");
? ? QStringList Actionlist = mySet::getInstance()->mActionlist;
? ? QStringList statelist;
? ? statelist<<"未發(fā)起"<<"失敗"<<"成功";
? ? //設(shè)置表格數(shù)據(jù)
? ? for(int i=0;i
? ? ? ? stOneContent one= exlData.at(i);
? ? ? ? if(isaddexcel(one)==false){
? ? ? ? ? ? continue;
? ? ? ? }
? ? ? ? cellrow++;
? ? ? ? //設(shè)置要操作的單元格,如A1芍耘,A2,A3凤壁,A4
? ? ? ? queryOneObject(worksheet,"A",cellrow, one.tNumber);
? ? ? ? queryOneObject(worksheet,"B",cellrow, one.tName);
? ? ? ? queryOneObject(worksheet,"C",cellrow, one.tContent);
? ? ? ? queryOneObject(worksheet,"D",cellrow, Actionlist[one.tAction]);
? ? ? ? queryOneObject(worksheet,"E",cellrow, one.tReplyWords);
? ? ? ? queryOneObject(worksheet,"F",cellrow, statelist[one.tzhongCai]);
? ? }
? ? //保存至filepath,用QDir::toNativeSeparators將路徑中的"/"轉(zhuǎn)換為"\"跪另,
? ? workbook->dynamicCall("SaveAs(const QString&)",QDir::toNativeSeparators(filepath));
? ? workbook->dynamicCall("Close()");
? ? excel->dynamicCall("Quit()");//關(guān)閉excel
? ? delete excel;
? ? excel= nullptr;
? ? qDebug() << "\n導(dǎo)出成功";
? ? ui->label_Tip->hide();
? ? QMessageBox::information(nullptr,"提示", "導(dǎo)出完成");
}