QtXlsx tablewidget导出为xlsx文件
时间: 2024-05-05 21:15:48 浏览: 194
QT中用QTableWdiget导出Excel表格
1. 安装QtXlsx模块
在Qt Creator中,选择“工具”->“选项”->“构建和运行”,在“Qt版本”选项卡中,选择安装了QtXlsx模块的Qt版本。
2. 创建表格数据
在Qt Creator中创建一个新的Qt Widgets应用程序,添加一个QTableWidget控件。使用以下代码在QTableWidget控件中添加数据:
```c++
ui->tableWidget->setRowCount(5);
ui->tableWidget->setColumnCount(3);
ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "Name" << "Age" << "Gender");
ui->tableWidget->setItem(0, 0, new QTableWidgetItem("Tom"));
ui->tableWidget->setItem(0, 1, new QTableWidgetItem("20"));
ui->tableWidget->setItem(0, 2, new QTableWidgetItem("Male"));
ui->tableWidget->setItem(1, 0, new QTableWidgetItem("Jerry"));
ui->tableWidget->setItem(1, 1, new QTableWidgetItem("18"));
ui->tableWidget->setItem(1, 2, new QTableWidgetItem("Female"));
ui->tableWidget->setItem(2, 0, new QTableWidgetItem("Mike"));
ui->tableWidget->setItem(2, 1, new QTableWidgetItem("25"));
ui->tableWidget->setItem(2, 2, new QTableWidgetItem("Male"));
ui->tableWidget->setItem(3, 0, new QTableWidgetItem("Lucy"));
ui->tableWidget->setItem(3, 1, new QTableWidgetItem("22"));
ui->tableWidget->setItem(3, 2, new QTableWidgetItem("Female"));
ui->tableWidget->setItem(4, 0, new QTableWidgetItem("John"));
ui->tableWidget->setItem(4, 1, new QTableWidgetItem("30"));
ui->tableWidget->setItem(4, 2, new QTableWidgetItem("Male"));
```
3. 导出为xlsx文件
使用以下代码将QTableWidget控件中的数据导出为xlsx文件:
```c++
QXlsx::Document xlsx;
for(int i = 0; i < ui->tableWidget->rowCount(); i++)
{
for(int j = 0; j < ui->tableWidget->columnCount(); j++)
{
QTableWidgetItem *item = ui->tableWidget->item(i, j);
if(item != nullptr)
{
xlsx.write(i+1, j+1, item->text());
}
}
}
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Excel Files (*.xlsx)"));
if(!fileName.isEmpty())
{
xlsx.saveAs(fileName);
QMessageBox::information(this, tr("Export"), tr("Export Successful!"));
}
```
在导出xlsx文件之前,使用QXlsx::Document类创建一个xlsx文档,并将QTableWidget控件中的数据写入到该文档中。使用QFileDialog类选择要保存的位置和文件名。如果用户选择了文件名,则将xlsx文档保存为该文件名。最后,使用QMessageBox类显示导出成功的消息。
完整代码:
mainwindow.h
```c++
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTableWidget>
#include <QFileDialog>
#include <QMessageBox>
#include "xlsxdocument.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
```
mainwindow.cpp
```c++
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableWidget->setRowCount(5);
ui->tableWidget->setColumnCount(3);
ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "Name" << "Age" << "Gender");
ui->tableWidget->setItem(0, 0, new QTableWidgetItem("Tom"));
ui->tableWidget->setItem(0, 1, new QTableWidgetItem("20"));
ui->tableWidget->setItem(0, 2, new QTableWidgetItem("Male"));
ui->tableWidget->setItem(1, 0, new QTableWidgetItem("Jerry"));
ui->tableWidget->setItem(1, 1, new QTableWidgetItem("18"));
ui->tableWidget->setItem(1, 2, new QTableWidgetItem("Female"));
ui->tableWidget->setItem(2, 0, new QTableWidgetItem("Mike"));
ui->tableWidget->setItem(2, 1, new QTableWidgetItem("25"));
ui->tableWidget->setItem(2, 2, new QTableWidgetItem("Male"));
ui->tableWidget->setItem(3, 0, new QTableWidgetItem("Lucy"));
ui->tableWidget->setItem(3, 1, new QTableWidgetItem("22"));
ui->tableWidget->setItem(3, 2, new QTableWidgetItem("Female"));
ui->tableWidget->setItem(4, 0, new QTableWidgetItem("John"));
ui->tableWidget->setItem(4, 1, new QTableWidgetItem("30"));
ui->tableWidget->setItem(4, 2, new QTableWidgetItem("Male"));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QXlsx::Document xlsx;
for(int i = 0; i < ui->tableWidget->rowCount(); i++)
{
for(int j = 0; j < ui->tableWidget->columnCount(); j++)
{
QTableWidgetItem *item = ui->tableWidget->item(i, j);
if(item != nullptr)
{
xlsx.write(i+1, j+1, item->text());
}
}
}
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Excel Files (*.xlsx)"));
if(!fileName.isEmpty())
{
xlsx.saveAs(fileName);
QMessageBox::information(this, tr("Export"), tr("Export Successful!"));
}
}
```
运行程序,点击“导出”按钮,选择保存位置和文件名,点击“保存”按钮,即可将QTableWidget控件中的数据导出为xlsx文件。
阅读全文