表格和excel例子如下图所示:
一、QTableWidget保存表格数据到Excel文件中
代码如下:
(pro文件中添加QT += axcontainer)
#include <QAxObject>
void MainWindow::saveTableToExcel() {
QDateTime current_date_time =QDateTime::currentDateTime();
QString excelName = "data_" + current_date_time.toString("yyyy-MM-dd_hh-mm-ss");
QString filePath = QFileDialog::getSaveFileName(this, "Save Data", excelName, "Microsoft Excel 2013(*.xlsx)");
int row = ui->originDataTable->rowCount();
int col = ui->originDataTable->columnCount();
QAxObject* excel = new QAxObject(this);
//excel->setControl("ket.Application");//wps
excel->setControl("Excel.Application"); //Excel
excel->dynamicCall("SetVisible(bool Visible)", false);
excel->setProperty("DisplayAlerts", false);
QAxObject* workbooks = excel->querySubObject("WorkBooks");
workbooks->dynamicCall("Add");
QAxObject* workbook = excel->querySubObject("ActiveWorkBook");
QAxObject* worksheets = workbook->querySubObject("Sheets");
QAxObject* worksheet = worksheets->querySubObject("Item(int)", 1);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
QAxObject* Range = worksheet->querySubObject("Cells(int,int)", i + 1, j + 1);
Range->dynamicCall("SetValue(const QString &)", ui->originDataTable->item(i, j)->text());
}
}
workbook->dynamicCall("SaveAs(const QString &)", QDir::toNativeSeparators(filePath));
if (excel != NULL) {
excel->dynamicCall("Quit()");
delete excel;
excel = NULL;
}
QMessageBox::information(this, QStringLiteral("提示"), "保存成功");
}
二、读Excel文件内容到QTableWidget表格中
#include <QAxObject>
void MainWindow::saveTableToExcel() {
QString strFile = QFileDialog::getOpenFileName(this,QStringLiteral("选择Excel文件"),"","Exel file(*.xls *.xlsx)");
if (strFile.isEmpty()){
return;
}
QAxObject excel("Excel.Application");
excel.setProperty("Visible", false);
QAxObject *work_books = excel.querySubObject("WorkBooks");
//打开指定文件
work_books->dynamicCall("Open (const QString&)", strFile);
QAxObject *work_book = excel.querySubObject("ActiveWorkBook");
QString ExcelName;
static int row_count = 0, column_count = 0;
QAxObject *work_sheet = work_book->querySubObject("Sheets(int)", 1);
QAxObject *used_range = work_sheet->querySubObject("UsedRange");
QAxObject *rows = used_range->querySubObject("Rows");
row_count = rows->property("Count").toInt();
QAxObject *column = used_range->querySubObject("Columns");
column_count = column->property("Count").toInt();
//这里先清空QTableWidget表格数据
ui->originDataTable->clearContents();
ui->setRowCount(0);
for (int i = 1; i <= row_count; i++) {
QStringList dataList;
for (int j = 1; j <= column_count;j++) {
QAxObject *range = work_sheet->querySubObject("Cells(int,int)",i,j); //获取cell的值
QString strVal = range->dynamicCall("Value2()").toString();
dataList << strVal;
}
int row = ui->originDataTable->rowCount();
ui->originDataTable->insertRow(row);
for (int col = 0; col < dataList.size(); ++col) {
QTableWidgetItem *pItem = new QTableWidgetItem(dataList[col]);
ui->originDataTable->setItem(row, col, pItem);
}
}
work_book->dynamicCall("Close(Boolean)", false); //关闭文件
excel.dynamicCall("Quit(void)"); //退出
}
总结:
QAxObject读取excel较为方便,不必使用第三方库;缺点是读取excel文件时非常慢,一个内容很少的excel文件读取都需要几秒钟。 用第三方库读取效率会高一些,我用的是xlsx的库,需要的可以在评论区留下自己的邮箱,我将在有空时回复并发到邮箱。