QT Creator implements the export of a table in a database, and writes the table header in Chinese when exporting

Specific implementation functions:
Realize exporting a certain form in a certain database to a form file that can be opened by execl. The general suffix is ​​.xls. I am demonstrating here in the Ubuntu environment, and the Windows environment should also be OK

To achieve this function, we need the following steps:

1. Open the database and make a query.
2. Create an Excel file and write the header and data.
3. Save the Excel file.

The following is to write the Demo in the main
The specific implementation code is as follows:
#include <QApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlField>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // 打开数据库
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("/path/to/database.db");
    if (!db.open()) {
        QMessageBox::critical(nullptr, "Error", db.lastError().text());
        return -1;
    }

    // 执行查询
    QSqlQuery query("SELECT * FROM table_name", db);

    // 创建输出文件
    QString fileName = "/path/to/output.xls";
    QFile file(fileName);
    if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
        QMessageBox::critical(nullptr, "Error", "Failed to create file");
        return -1;
    }

    // 写入表头
    QSqlRecord record = query.record();
    int columnCount = record.count();
    QString header;
    header.append("编号\t父编号\t名称\t级别\t路径名称\t描述\t发布时间\n"); // 表头中文,使用Tab符分隔
    file.write(header.toUtf8());

    // 写入数据
    QString content;
    while (query.next()) {
        for (int i = 0; i < columnCount; i++) {
            QSqlField field = record.field(i);
            QVariant value = query.value(i);
            content.append(value.toString());
            if (i != columnCount - 1) {
                content.append('\t');
            }
        }
        content.append('\n');
    }
    file.write(content.toUtf8());

    file.close();

    return 0;
}
Among them, the following parameters need to be modified according to the actual situation: - `/path/to/database.db`: database file path and file name - `table_name`: name of the table to be exported - `/path/to/output.xls`: Output Excel file path and file name This code will export all the data of the table `table_name` from the SQLite database, write it into an Excel file, and use the Chinese table header. The format of the Excel file is `xls`, which can be opened with various software such as Excel and WPS. In the process of exporting Excel, you can also use other third-party libraries and plug-ins, such as QXlsx, QtXlsxWriter, etc.


In actual application, it is encapsulated in a function:
// 引用就不写了

// 导出任务信息
void DataBank::ExportTaskInformation()
{
    QString dbName = "../mission_history.db";
    // 连接数据库
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(dbName);
    if(!db.open())
    {
        qDebug() << "连接mission_history.db失败";
        return;
    }

    // 执行查询
    QSqlQuery query("SELECT * FROM mission_history", db);
    QFile file("mission_history.xls");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        qDebug() << "无法打开文件进行写入";
        return ;
    }

    // 创建输出流,写入表头
    QSqlRecord record = query.record();
    int columnCount = record.count();
    QString header;
    header.append("序号\t订单编号\t站点列表\t开始时间\t结束时间\t任务顺序\t下单方式\t任务状态\t组合信息\n"); // 表头中文,使用Tab符分隔
    file.write(header.toUtf8());

    // 写入数据
    QString content;
    while (query.next())
    {
        for (int i = 0; i < columnCount; i++)
        {
            QSqlField field = record.field(i);
            QVariant value = query.value(i);
            content.append(value.toString());
            if (i != columnCount - 1)
            {
                content.append('\t');
            }
        }
        content.append('\n');
    }
    file.write(content.toUtf8());

    file.close();
    db.close();

    qDebug() << "已将任务列表导出到 mission_history.xls";

    return ;
}

Guess you like

Origin blog.csdn.net/qq_39085747/article/details/129885728