首先在.pro文件中添加以下代碼:QT+=axcontainer
代碼是轉載的,找不到源博客鏈接
void excel_01::on_pushButton_clicked()
{//導出ui->progressBar->setValue(0); //設置進度條的值為0QString fileName = QFileDialog::getSaveFileName(this,tr("Excle file"),QString("./paper_list.xlsx"),tr("Excel Files(*.xlsx)")); //設置保存的文件名if(fileName != ""){ ui->progressBar->show(); //進度條需要在ui文件中加個progressBar控件ui->progressBar->setValue(10);QAxObject *excel = new QAxObject;if(excel->setControl("Excel.Application")){excel->dynamicCall("SetVisible (bool Visible)",false);excel->setProperty("DisplayAlerts",false);QAxObject *workbooks = excel->querySubObject("WorkBooks"); //獲取工作簿集合workbooks->dynamicCall("Add"); //新建一個工作簿QAxObject *workbook = excel->querySubObject("ActiveWorkBook"); //獲取當前工作簿QAxObject *worksheet = workbook->querySubObject("Worksheets(int)", 1);QAxObject *cell;/*添加Excel表頭數據*/for(int i = 1; i <= ui->tableWidget->columnCount(); i++){cell=worksheet->querySubObject("Cells(int,int)", 1, i);cell->setProperty("RowHeight", 40);cell->dynamicCall("SetValue(const QString&)", ui->tableWidget->horizontalHeaderItem(i-1)->data(0).toString());if(ui->progressBar->value()<=50){ui->progressBar->setValue(10+i*5);}}/*將form列表中的數據依此保存到Excel文件中*/for(int j = 2; j<=ui->tableWidget->rowCount()+1;j++){for(int k = 1;k<=ui->tableWidget->columnCount();k++){cell=worksheet->querySubObject("Cells(int,int)", j, k);if(ui->tableWidget->item(j-2,k-1)!=NULL){cell->dynamicCall("SetValue(const QString&)",ui->tableWidget->item(j-2,k-1)->text()+ "\t");}}if(ui->progressBar->value()<80){ui->progressBar->setValue(50+j*5);}}/*將生成的Excel文件保存到指定目錄下*/workbook->dynamicCall("SaveAs(const QString&)",QDir::toNativeSeparators(fileName)); //保存至fileNameworkbook->dynamicCall("Close()"); //關閉工作簿excel->dynamicCall("Quit()"); //關閉exceldelete excel;excel=NULL;ui->progressBar->setValue(100);if (QMessageBox::question(NULL,QString::fromUtf8("完成"),QString::fromUtf8("文件已經導出,是否現在打開?"),QMessageBox::Yes|QMessageBox::No)==QMessageBox::Yes){QDesktopServices::openUrl(QUrl("file:///" + QDir::toNativeSeparators(fileName)));}ui->progressBar->setValue(0);ui->progressBar->hide();}}}void excel_01::on_pushButton_2_clicked()
{//導入ui->progressBar->setValue(0); //設置進度條的值為0QString path = QFileDialog::getOpenFileName(this,"open","../","execl(*.xlsx)");//指定父對象(this),“open”具體操作,打開,“../”默認,之后可以添加要打開文件的格式if(path.isEmpty()==false){//文件對象QFile file(path);//打開文件,默認為utf8變量,bool flag = file.open(QIODevice::ReadOnly);if(flag == true)//打開成功{ui->progressBar->show(); //進度條需要在ui文件中加個progressBar控件ui->progressBar->setValue(10);QAxObject *excel = new QAxObject(this);//建立excel操作對象excel->setControl("Excel.Application");//連接Excel控件excel->setProperty("Visible", false);//不顯示窗體看效果excel->setProperty("DisplayAlerts", false);//不顯示警告看效果/*********獲取COM文件的一種方式************/QAxObject *workbooks = excel->querySubObject("WorkBooks");//獲取工作簿(excel文件)集合workbooks->dynamicCall("Open(const QString&)", path);//path至關重要,獲取excel文件的路徑//打開一個excel文件QAxObject *workbook = excel->querySubObject("ActiveWorkBook");QAxObject *worksheet = workbook->querySubObject("WorkSheets(int)",1);//訪問excel中的工作表中第一個單元格QAxObject *usedRange = worksheet->querySubObject("UsedRange");//sheet的范圍/*********獲取COM文件的一種方式************///獲取打開excel的起始行數和列數和總共的行數和列數int intRowStart = usedRange->property("Row").toInt();//起始行數int intColStart = usedRange->property("Column").toInt(); //起始列數QAxObject *rows, *columns;rows = usedRange->querySubObject("Rows");//行columns = usedRange->querySubObject("Columns");//列int intRow = rows->property("Count").toInt();//行數int intCol = columns->property("Count").toInt();//列數//起始行列號//qDebug()<<intRowStart;//qDebug()<<intColStart;//行數和列數//qDebug()<<intRow;//qDebug()<<intCol;int a,b;a=intRow-intRowStart+1,b=intCol-intColStart+1;QByteArray text[a][b];QString exceldata[a][b];int coerow=0,coecol=0;for (int i = intRowStart; i < intRowStart + intRow; i++,coerow++){coecol=0;//務必是要恢復初值的for (int j = intColStart; j < intColStart + intCol; j++,coecol++){auto cell = excel->querySubObject("Cells(Int, Int)", i, j );QVariant cellValue = cell->dynamicCall("value");text[coerow][coecol]=cellValue.toByteArray();//QVariant轉換為QByteArrayexceldata[coerow][coecol]=QString(text[coerow][coecol]);//QByteArray轉換為QStringif(ui->progressBar->value()<=60){ui->progressBar->setValue(10+i*5);}//qDebug()<<exceldata[coerow][coecol]<<coerow<<" "<<coecol;}}ui->tableWidget->setRowCount(a-1);for(int i=1;i<a;++i)for(int j=0;j<b;++j){ui->tableWidget->setItem(i-1, j, new QTableWidgetItem(exceldata[i][j]));if(ui->progressBar->value()<=80){ui->progressBar->setValue(60+i*5);}}workbook->dynamicCall( "Close(Boolean)", false );excel->dynamicCall( "Quit(void)" );delete excel;ui->progressBar->setValue(100);QMessageBox::warning(this,tr("讀取情況"),tr("讀取完成!"),QMessageBox::Yes);ui->progressBar->hide();ui->progressBar->setValue(0);}file.close();}
}