1 QString filepath = QFileDialog::getSaveFileName(this, QString::fromLocal8Bit("導出表格"), ".", tr("Microsoft Office(*.xlsx)"));//獲取保存路徑 2 if (!filepath.isEmpty()) { 3 QAxObject *excel = new QAxObject(this); 4 excel->setControl("Excel.Application");//鏈接Excel控件 5 excel->dynamicCall("SetVisible (bool Visible)", "false");//不顯示窗體 6 excel->setProperty("DisplayAlerts", false);//不顯示任何警告信息。若是爲true那麼在關閉是會出現相似「文件已修改,是否保存」的提示 7 8 QAxObject *workbooks = excel->querySubObject("WorkBooks");//獲取工做簿集合 9 workbooks->dynamicCall("Add");//新建一個工做簿 10 QAxObject *workbook = excel->querySubObject("ActiveWorkBook");//獲取當前工做簿 11 QAxObject *worksheets = workbook->querySubObject("Sheets");//獲取工做表集合 12 QAxObject *worksheet = worksheets->querySubObject("Item(int)", 1);//獲取工做表集合的工做表1,即sheet1 13 workbook->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(filepath));//保存至filepath,注意必定要用QDir::toNativeSeparators將路徑中的"/"轉換爲"\",否則必定保存不了。 14 workbook->dynamicCall("Close()");//關閉工做簿 15 excel->dynamicCall("Quit()");//關閉excel 16 delete excel; 17 excel = NULL; 18 } 19 return true;