一、配置QtXlsx
1.1 下载解压QtXlsxWriter(在github下载即可)
网址:https://github.com/dbzhang800/QtXlsxWriter
1.2 使用qt运行
-
点击qtxlsx.pro运行QtXlsxWriter
-
选择DesktopQt51211MSVC201564bit编译器(选择自己本地电脑qt安装的编译器即可)
-
编译运行(debug或release都行)
1.3 移植
1、拷贝 D:\xlsxtool\QtXlsxWriter-master\build-qtxlsx-DesktopQt51211MSVC201564bit-Debug\include\ 里的QtXlsx到C:\Qt\Qt5.12.11\5.12.11\msvc201564\include。
2、拷贝 D:\xlsxtool\QtXlsxWriter-master\QtXlsxWriter-master\src\xlsx的所有头文件到C:\Qt\Qt5.12.11\5.12.11\msvc201564\include\QtXlsx中。
3、拷贝 D:\xlsxtool\QtXlsxWriter-master\build-qtxlsx-DesktopQt51211MSVC201564bit-Debug\lib 的Qt5Xlsx.lib,Qt5Xlsxd.lib,Qt5Xlsx.prl,Qt5Xlsxd.prl到C:\Qt\Qt5.12.11\5.12.11\msvc201564\lib。
4、拷贝 D:\xlsxtool\QtXlsxWriter-master\build-qtxlsx-DesktopQt51211MSVC201564bit-Debug\mkspecs\modules中的qtlibxlsx.pri文件到C:\Qt\Qt5.12.11\5.12.11\msvc201564\mkspecs\modules中。
5、拷贝D:\xlsxtool\QtXlsxWriter-master\build-qtxlsx-DesktopQt51211MSVC201564bit-Debug\lib 的Qt5Xlsx.dll,Qt5Xlsxd.dll到C:\Qt\Qt5.12.11\5.12.11\msvc201564\bin中
1.4 打开vs配置项目属性
如上配置完成后就能调用#include 了。
二、execl的导入导出
//导入动作槽函数
void SMachine::on_importAct_slot()
{QString filePath = QFileDialog::getOpenFileName(this, tr("打开文件"), m_programPath, "*.xlsx *.xls");if (!filePath.isEmpty()) {openProgramFile(filePath); //调用实际执行导入操作的函数}
}//导出动作作槽函数
void SMachine::on_exportAct_slot()
{if (m_tableWidget == nullptr) return;// 获取表格数据QList<QStringList> tableData;for (int row = 0; row < m_tableWidget->rowCount(); row++) {QStringList rowData;for (int column = 0; column < m_tableWidget->columnCount(); column++) {QTableWidgetItem* item = m_tableWidget->item(row, column);if (item != NULL) {rowData.append(item->text());}else {rowData.append("");}}tableData.append(rowData);}// 创建Excel文件QXlsx::Document xlsx;//1、遍历表头int columnCount = m_tableWidget->columnCount();for (int col = 0; col < columnCount; ++col){QTableWidgetItem* headerItem = m_tableWidget->horizontalHeaderItem(col);if (headerItem){QString text = headerItem->text();xlsx.write(1, col + 1, headerItem->text());}elsexlsx.write(1, col + 1, "");}//2、遍历内容for (int row = 0; row < tableData.size(); row++){for (int column = 0; column < tableData[row].size(); column++)xlsx.write(row + 2, column + 1, tableData[row][column]);}//3、另存为QString filePath = QFileDialog::getSaveFileName(this, tr("另存为"), m_programPath, "*.xlsx *.xls");if (filePath.isEmpty()) return;xlsx.saveAs(filePath);//4、顺便打开已导出(即已另存为)的文件openProgramFile(filePath);
}//根据文件名执行导入excel文件操作的函数
void SMachine::openProgramFile(QString file)
{if (file.isEmpty()) return;QFileInfo fileInfo(file);m_programName = fileInfo.fileName(); //更新文件名if (m_tableWidget != nullptr){ui.tableLayout->removeWidget(m_tableWidget);m_tableWidget->clearContents();m_tableWidget->clear();}//初始化表if (m_tableWidget == nullptr){m_tableWidget = new QTableWidget(this);//m_tableWidget->verticalHeader()->setHidden(true); //隐藏垂直表头}//设置表头QTableWidgetItem* headerItem;QStringList headerText;headerText << tr("序号") << tr("X轴") << tr("Y轴") << tr("角度") << tr("站位") << tr("脚位") << tr("元件名称") << tr("元件类型") << tr("脚宽");m_tableWidget->setColumnCount(headerText.count());for (int i = 0; i < m_tableWidget->columnCount(); i++) {headerItem = new QTableWidgetItem(headerText.at(i));QFont font = headerItem->font();font.setPointSize(9); //设置字体字号headerItem->setTextColor(Qt::blue); //设置颜色headerItem->setFont(font);m_tableWidget->setHorizontalHeaderItem(i, headerItem);}m_tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); //设置列宽自适应//打开文件Document xlsx(file);QXlsx::Workbook* workBook = xlsx.workbook();QXlsx::Worksheet* workSheet = static_cast<QXlsx::Worksheet*>(workBook->sheet(0));//第一个标签页QString value;int rows = workSheet->dimension().rowCount();int cols = workSheet->dimension().columnCount();for (int i = 2; i <= rows; i++){int row = m_tableWidget->rowCount();m_tableWidget->insertRow(row);for (int j = 1; j <= cols; j++){QXlsx::Cell* cell = workSheet->cellAt(i, j);if (cell != nullptr){value = cell->value().toString();QTableWidgetItem* item = new QTableWidgetItem(value);m_tableWidget->setItem(i - 2, j - 1, item);}}}ui.tableLayout->addWidget(m_tableWidget);ui.programLE->setText(m_programName);
}
三、调用本地系统默认程序打开/修改execl文件
void SMachine::on_modifyAct_slot()
{QString filePath = QFileDialog::getOpenFileName(this, "选择 Excel 文件", m_programPath, "Excel 文件 (*.xlsx *.xls)");if (filePath.isEmpty()) {QMessageBox::warning(this, "警告", "未选择文件!");return;}//利用系统默认程序打开文件bool success = QDesktopServices::openUrl(QUrl::fromLocalFile(filePath));if (!success) {QMessageBox::critical(this, "错误", "无法打开 Excel 文件!");}
}