生活随笔
收集整理的這篇文章主要介紹了
Qt tableWidget导入\导出Excel表格 自用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
首先在.pro文件中添加以下代碼:QT+=axcontainer
代碼是轉(zhuǎn)載的,找不到源博客鏈接
void excel_01
::on_pushButton_clicked()
{ui
->progressBar
->setValue(0); QString fileName
= QFileDialog
::getSaveFileName(this,tr("Excle file"),QString("./paper_list.xlsx"),tr("Excel Files(*.xlsx)")); if(fileName
!= ""){ ui
->progressBar
->show(); 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
;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);}}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);}}workbook
->dynamicCall("SaveAs(const QString&)",QDir
::toNativeSeparators(fileName
)); workbook
->dynamicCall("Close()"); excel
->dynamicCall("Quit()"); delete excel
;excel
=NULL;ui
->progressBar
->setValue(100);if (QMessageBox
::question(NULL,QString
::fromUtf8("完成"),QString
::fromUtf8("文件已經(jīng)導(dǎo)出,是否現(xiàn)在打開(kāi)?"),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); QString path
= QFileDialog
::getOpenFileName(this,"open","../","execl(*.xlsx)");if(path
.isEmpty()==false){QFile
file(path
);bool flag
= file
.open(QIODevice
::ReadOnly
);if(flag
== true){ui
->progressBar
->show(); ui
->progressBar
->setValue(10);QAxObject
*excel
= new QAxObject(this);excel
->setControl("Excel.Application");excel
->setProperty("Visible", false);excel
->setProperty("DisplayAlerts", false);QAxObject
*workbooks
= excel
->querySubObject("WorkBooks");workbooks
->dynamicCall("Open(const QString&)", path
);QAxObject
*workbook
= excel
->querySubObject("ActiveWorkBook");QAxObject
*worksheet
= workbook
->querySubObject("WorkSheets(int)",1);QAxObject
*usedRange
= worksheet
->querySubObject("UsedRange");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();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();exceldata
[coerow
][coecol
]=QString(text
[coerow
][coecol
]);if(ui
->progressBar
->value()<=60){ui
->progressBar
->setValue(10+i
*5);}}}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();}
}
總結(jié)
以上是生活随笔為你收集整理的Qt tableWidget导入\导出Excel表格 自用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。