Qt 将表格中的数据保存到Excel
首先,需要在.pro文件中添加如下语句
CONFIG += qaxcontainer #导出excel
然后在.cpp文件中添加如下语句
#include <QTableWidget> #include <QFileDialog> #include <QDesktopServices> #include <QMessageBox> #include <QAxObject>
最后是实现代码
//第一个参数是页面上的表格,第二个是导出文件的表头数据 void FaJianDialog::Table2ExcelByHtml(QTableWidget *table,QString title) { QString fileName = QFileDialog::getSaveFileName(table, "保存",QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),"Excel 文件(*.xls *.xlsx)"); if (fileName!="") { QAxObject *excel = new QAxObject; if (excel->setControl("Excel.Application")) //连接Excel控件 { excel->dynamicCall("SetVisible (bool Visible)","false");//不显示窗体 excel->setProperty("DisplayAlerts", false);//不显示任何警告信息。如果为true那么在关闭是会出现类似“文件已修改,是否保存”的提示 QAxObject *workbooks = excel->querySubObject("WorkBooks");//获取工作簿集合 workbooks->dynamicCall("Add");//新建一个工作簿 QAxObject *workbook = excel->querySubObject("ActiveWorkBook");//获取当前工作簿 QAxObject *worksheet = workbook->querySubObject("Worksheets(int)", 1); int i,j; //QTablewidget 获取数据的列数 int colcount=table->columnCount(); //QTablewidget 获取数据的行数 int rowscount=table->rowCount() //QTableView 获取列数 //int colount=ui->tableview->model->columnCount(); //QTableView 获取行数 //int rowcount=ui->tableview->model->rowCount(); QAxObject *cell,*col; //标题行 cell=worksheet->querySubObject("Cells(int,int)", 1, 1); cell->dynamicCall("SetValue(const QString&)", title); cell->querySubObject("Font")->setProperty("Size", 18); //调整行高 worksheet->querySubObject("Range(const QString&)", "1:1")->setProperty("RowHeight", 30); //合并标题行 QString cellTitle; cellTitle.append("A1:"); cellTitle.append(QChar(colcount - 1 + 'A')); cellTitle.append(QString::number(1)); QAxObject *range = worksheet->querySubObject("Range(const QString&)", cellTitle); range->setProperty("WrapText", true); range->setProperty("MergeCells", true); range->setProperty("HorizontalAlignment", -4108);//xlCenter range->setProperty("VerticalAlignment", -4108);//xlCenter //列标题 for(i=0;i<colcount;i++) { QString columnName; columnName.append(QChar(i + 'A')); columnName.append(":"); columnName.append(QChar(i + 'A')); col = worksheet->querySubObject("Columns(const QString&)", columnName); col->setProperty("ColumnWidth", table->columnWidth(i)/6); cell=worksheet->querySubObject("Cells(int,int)", 2, i+1); //QTableWidget 获取表格头部文字信息 columnName=table->horizontalHeaderItem(i)->text(); //QTableView 获取表格头部文字信息 // columnName=ui->tableView_right->model()->headerData(i,Qt::Horizontal,Qt::DisplayRole).toString(); cell->dynamicCall("SetValue(const QString&)", columnName); cell->querySubObject("Font")->setProperty("Bold", true); cell->querySubObject("Interior")->setProperty("Color",QColor(191, 191, 191)); cell->setProperty("HorizontalAlignment", -4108);//xlCenter cell->setProperty("VerticalAlignment", -4108);//xlCenter } //数据区 //QTableWidget 获取表格数据部分 for(i=0;i<rowcount;i++){ for (j=0;j<colcount;j++) { worksheet->querySubObject("Cells(int,int)", i+3, j+1)->dynamicCall("SetValue(const QString&)", table->item(i,j)?table->item(i,j)->text():""); } } //QTableView 获取表格数据部分 // for(i=0;i<rowcount;i++) //行数 // { // for (j=0;j<colcount;j++) //列数 // { // QModelIndex index = ui->tableView_right->model()->index(i, j); // QString strdata=ui->tableView_right->model()->data(index).toString(); // worksheet->querySubObject("Cells(int,int)", i+3, j+1)->dynamicCall("SetValue(const QString&)", strdata); // } // } //画框线 QString lrange; lrange.append("A2:"); lrange.append(colcount - 1 + 'A'); lrange.append(QString::number(table->rowCount() + 2)); range = worksheet->querySubObject("Range(const QString&)", lrange); range->querySubObject("Borders")->setProperty("LineStyle", QString::number(1)); range->querySubObject("Borders")->setProperty("Color", QColor(0, 0, 0)); //调整数据区行高 QString rowsName; rowsName.append("2:"); rowsName.append(QString::number(table->rowCount() + 2)); range = worksheet->querySubObject("Range(const QString&)", rowsName); range->setProperty("RowHeight", 20); workbook->dynamicCall("SaveAs(const QString&)",QDir::toNativeSeparators(fileName));//保存至fileName workbook->dynamicCall("Close()");//关闭工作簿 excel->dynamicCall("Quit()");//关闭excel delete excel; excel=NULL; //QMessageBox::information(this,tr("提示"),tr("数据已保存")); if (QMessageBox::question(NULL,"完成","文件已经导出,是否现在打开?",QMessageBox::Yes|QMessageBox::No)==QMessageBox::Yes) { QDesktopServices::openUrl(QUrl("file:///" + QDir::toNativeSeparators(fileName))); } } else { QMessageBox::warning(NULL,"错误","未能创建 Excel 对象,请安装 Microsoft Excel。",QMessageBox::Apply); } } }
至此,到处基本完成
出处:https://www.cnblogs.com/leocc325/p/12830233.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)