十八、QT之导出数据到EXCEL

ExcelAPI.h

#ifndef EXCEL_H
#define EXCEL_H

#include <QObject>
#include <QMutex>
#include <QStringList>

class ExcelAPI : public QObject
{
    Q_OBJECT
private:
    explicit ExcelAPI(QObject *parent = 0);
    static ExcelAPI *_instance;
    QStringList html;

    void checkBorder(bool border);

public:
    static ExcelAPI *Instance() {
        static QMutex mutex;
        if (!_instance) {
            QMutexLocker locker(&mutex);
            if (!_instance) {
                _instance = new ExcelAPI;
            }
        }
        return _instance;
    }

    void ToExcel(QString fileName, QString sheetName, QString title, QString subTitle,
                 QList<QString> columnNames, QList<int> columnWidths,
                 QStringList content, bool border, bool check);

    void ToExcel(QString fileName, QString sheetName, QString title,
                 QList<QString> columnNames, QList<int> columnWidths,
                 QStringList subTitle1, QStringList subTitle2,
                 QStringList content, bool border, bool check);

};

#endif // EXCEL_H

ExcelAPI.cpp

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

#include "excelapi.h"
#include "myhelper.h"

ExcelAPI *ExcelAPI::_instance = 0;
ExcelAPI::ExcelAPI(QObject *parent) :
    QObject(parent)
{
}

void ExcelAPI::checkBorder(bool border)
{
    if (border) {
        html.append("  <Borders>\n");
        html.append("   <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>\n");
        html.append("   <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>\n");
        html.append("   <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>\n");
        html.append("   <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>\n");
        html.append("  </Borders>\n");
    }
}

void ExcelAPI::ToExcel(QString fileName, QString sheetName, QString title, QString subTitle,
                       QList<QString> columnNames, QList<int> columnWidths,
                       QStringList content, bool border, bool check)
{
    if (sheetName.length() == 0) {
        sheetName = "Sheet1";
    }

    //计算行数列数
    int columnCount = columnNames.count();
    int rowCount = content.count();

    //逐个拼接xml字符,再保存为xls文件
    //清空原有数据,确保每次都是新的数据
    html.clear();

    //固定头部信息
    html.append("<?xml version=\"1.0\"?>\n");
    html.append("<?mso-application progid=\"Excel.Sheet\"?>\n");
    html.append("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\n");
    html.append(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n");
    html.append(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\n");
    html.append(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"\n");
    html.append(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n");

    //文档信息
    html.append(" <DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">\n");
    html.append("  <LastAuthor></LastAuthor>\n");
    html.append(QString("  <Created>%1</Created>\n").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")));
    html.append("  <Version>11.6360</Version>\n");
    html.append(" </DocumentProperties>\n");

    html.append(" <OfficeDocumentSettings xmlns=\"urn:schemas-microsoft-com:office:office\">\n");
    html.append("  <AllowPNG/>\n");
    html.append(" </OfficeDocumentSettings>\n");

    html.append(" <ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">\n");
    html.append("  <WindowHeight>9795</WindowHeight>\n");
    html.append("  <WindowWidth>21435</WindowWidth>\n");
    html.append("  <WindowTopX>120</WindowTopX>\n");
    html.append("  <WindowTopY>75</WindowTopY>\n");
    html.append("  <WindowTopY>75</WindowTopY>\n");
    html.append("  <ProtectWindows>False</ProtectWindows>\n");
    html.append(" </ExcelWorkbook>\n");

    //样式表
    html.append(" <Styles>\n");
    html.append("  <Style ss:ID=\"Default\" ss:Name=\"Normal\">\n");
    html.append("   <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/>\n");
    html.append("   <Borders/>\n");
    html.append("   <Font ss:FontName=\"微软雅黑\" x:CharSet=\"134\" ss:Size=\"10\" ss:Color=\"#000000\"/>\n");
    html.append("   <Interior/>\n");
    html.append("   <NumberFormat/>\n");
    html.append("   <Protection/>\n");
    html.append("  </Style>\n");

    //标题样式居中大号加粗字体
    html.append("  <Style ss:ID=\"s_title\">\n");
    html.append("   <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/>\n");
    checkBorder(border);
    html.append("   <Font ss:FontName=\"微软雅黑\" x:CharSet=\"134\" ss:Size=\"11\" ss:Color=\"#000000\" ss:Bold=\"1\"/>\n");
    html.append("  </Style>\n");

    //标题样式--左对齐普通字体
    html.append("  <Style ss:ID=\"s_subtitle\">\n");
    html.append("   <Alignment ss:Horizontal=\"Left\" ss:Vertical=\"Center\"/>\n");
    checkBorder(border);
    html.append("   <Font ss:FontName=\"微软雅黑\" x:CharSet=\"134\" ss:Size=\"10\" ss:Color=\"#000000\" ss:Bold=\"0\"/>\n");
    html.append("  </Style>\n");

    //正文样式
    html.append("  <Style ss:ID=\"s_normal\">\n");
    checkBorder(border);
    html.append(" </Style>\n");

    //正文样式--文字红色
    html.append("  <Style ss:ID=\"s_red\">\n");
    checkBorder(border);
    html.append("  <Font ss:FontName=\"微软雅黑\" x:CharSet=\"134\" ss:Size=\"10\" ss:Color=\"#FF0000\" ss:Bold=\"0\"/>\n");
    html.append(" </Style>\n");

    html.append(" </Styles>\n");

    //工作表名称
    html.append(QString(" <Worksheet ss:Name=\"%1\">\n").arg(sheetName));

    //表格开始
    html.append(QString("  <Table ss:ExpandedColumnCount=\"%1\" x:FullColumns=\"1\"\n").arg(columnCount));
    html.append("   x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"18\">\n");

    //设置字段宽度
    for (int i = 0; i < columnCount; i++) {
        html.append(QString("   <Column ss:AutoFitWidth=\"0\" ss:Width=\"%1\"/>\n").arg(columnWidths.at(i)));
    }

    //表格标题
    if (title.length() > 0) {
        html.append("   <Row ss:AutoFitHeight=\"0\" ss:Height=\"22\">\n");
        html.append(QString("    <Cell ss:MergeAcross=\"%1\" ss:StyleID=\"s_title\"><Data ss:Type=\"String\">%2</Data></Cell>\n")
                    .arg(columnCount - 1).arg(title));
        html.append("   </Row>");
    }

    //表格副标题
    if (subTitle.length() > 0) {
        html.append("   <Row ss:AutoFitHeight=\"0\" ss:Height=\"18\">\n");
        html.append(QString("    <Cell ss:MergeAcross=\"%1\" ss:StyleID=\"s_subtitle\"><Data ss:Type=\"String\">%2</Data></Cell>\n")
                    .arg(columnCount - 1).arg(subTitle));
        html.append("   </Row>");
    }

    //逐个添加字段名称
    if (columnCount > 0) {
        html.append("   <Row ss:AutoFitHeight=\"0\">\n");
        for (int i = 0; i < columnCount; i++) {
            html.append(QString("    <Cell ss:StyleID=\"s_normal\"><Data ss:Type=\"String\">%1</Data></Cell>\n")
                        .arg(columnNames.at(i)));
        }
        html.append("   </Row>\n");
    }

    //逐个添加数据
    for (int i = 0; i < rowCount; i++) {
        html.append("   <Row ss:AutoFitHeight=\"0\">\n");

        QString temp = content.at(i);
        QStringList value = temp.split(";");

        if (check) {
            //如果是历史记录则文字红色
            if (value.at(value.count() - 1) == "历史记录") {
                foreach (QString str, value) {
                    html.append(QString("    <Cell ss:StyleID=\"s_red\"><Data ss:Type=\"String\">%1</Data></Cell>\n").arg(str));
                }
            } else {
                foreach (QString str, value) {
                    html.append(QString("    <Cell ss:StyleID=\"s_normal\"><Data ss:Type=\"String\">%1</Data></Cell>\n").arg(str));
                }
            }
        } else {
            foreach (QString str, value) {
                html.append(QString("    <Cell ss:StyleID=\"s_normal\"><Data ss:Type=\"String\">%1</Data></Cell>\n").arg(str));
            }
        }

        html.append("   </Row>\n");
    }

    html.append("  </Table>\n");

    //固定结尾格式
    html.append("  <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">\n");
    html.append("   <PageSetup>\n");
    html.append("    <Header x:Margin=\"0.3\"/>\n");
    html.append("    <Footer x:Margin=\"0.3\"/>\n");
    html.append("    <PageMargins x:Bottom=\"0.75\" x:Left=\"0.7\" x:Right=\"0.7\" x:Top=\"0.75\"/>\n");
    html.append("   </PageSetup>\n");
    html.append("   <Unsynced/>\n");
    html.append("   <Selected/>\n");
    html.append("   <ProtectObjects>False</ProtectObjects>\n");
    html.append("   <ProtectScenarios>False</ProtectScenarios>\n");
    html.append("  </WorksheetOptions>\n");
    html.append(" </Worksheet>\n");
    html.append("</Workbook>\n");

    //写入文件
    QFile file(fileName);
    if (file.open(QFile::WriteOnly | QIODevice::Text)) {
        QTextStream text(&file);
        text.setCodec("UTF-8");
        text << html.join("");
    }
}

void ExcelAPI::ToExcel(QString fileName, QString sheetName, QString title,
                       QList<QString> columnNames, QList<int> columnWidths,
                       QStringList subTitle1, QStringList subTitle2,
                       QStringList content, bool border, bool check)
{
    if (sheetName.length() == 0) {
        sheetName = "Sheet1";
    }

    //计算列数
    int columnCount = columnNames.count();

    //逐个拼接xml字符,再保存为xls文件
    //清空原有数据,确保每次都是新的数据
    html.clear();

    //固定头部信息
    html.append("<?xml version=\"1.0\"?>\n");
    html.append("<?mso-application progid=\"Excel.Sheet\"?>\n");
    html.append("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\n");
    html.append(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n");
    html.append(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\n");
    html.append(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"\n");
    html.append(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n");

    //文档信息
    html.append(" <DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">\n");
    html.append(QString("  <LastAuthor>%1</LastAuthor>\n").arg(""));
    html.append(QString("  <Created>%1</Created>\n").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")));
    html.append("  <Version>11.6360</Version>\n");
    html.append(" </DocumentProperties>\n");

    html.append(" <OfficeDocumentSettings xmlns=\"urn:schemas-microsoft-com:office:office\">\n");
    html.append("  <AllowPNG/>\n");
    html.append(" </OfficeDocumentSettings>\n");

    html.append(" <ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">\n");
    html.append("  <WindowHeight>9795</WindowHeight>\n");
    html.append("  <WindowWidth>21435</WindowWidth>\n");
    html.append("  <WindowTopX>120</WindowTopX>\n");
    html.append("  <WindowTopY>75</WindowTopY>\n");
    html.append("  <WindowTopY>75</WindowTopY>\n");
    html.append("  <ProtectWindows>False</ProtectWindows>\n");
    html.append(" </ExcelWorkbook>\n");

    //样式表
    html.append(" <Styles>\n");
    html.append("  <Style ss:ID=\"Default\" ss:Name=\"Normal\">\n");
    html.append("   <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/>\n");
    html.append("   <Borders/>\n");
    html.append("   <Font ss:FontName=\"微软雅黑\" x:CharSet=\"134\" ss:Size=\"10\" ss:Color=\"#000000\"/>\n");
    html.append("   <Interior/>\n");
    html.append("   <NumberFormat/>\n");
    html.append("   <Protection/>\n");
    html.append("  </Style>\n");

    //标题样式居中大号加粗字体
    html.append("  <Style ss:ID=\"s_title\">\n");
    html.append("   <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/>\n");
    checkBorder(border);
    html.append("   <Font ss:FontName=\"微软雅黑\" x:CharSet=\"134\" ss:Size=\"11\" ss:Color=\"#000000\" ss:Bold=\"1\"/>\n");
    html.append("  </Style>\n");

    //标题样式--左对齐普通字体
    html.append("  <Style ss:ID=\"s_subtitle\">\n");
    html.append("   <Alignment ss:Horizontal=\"Left\" ss:Vertical=\"Center\"/>\n");
    checkBorder(border);
    html.append("   <Font ss:FontName=\"微软雅黑\" x:CharSet=\"134\" ss:Size=\"10\" ss:Color=\"#000000\" ss:Bold=\"0\"/>\n");
    html.append("  </Style>\n");

    //正文样式
    html.append("  <Style ss:ID=\"s_normal\">\n");
    checkBorder(border);
    html.append(" </Style>\n");

    //正文样式--文字红色
    html.append("  <Style ss:ID=\"s_red\">\n");
    checkBorder(border);
    html.append("  <Font ss:FontName=\"微软雅黑\" x:CharSet=\"134\" ss:Size=\"10\" ss:Color=\"#FF0000\" ss:Bold=\"0\"/>\n");
    html.append(" </Style>\n");

    html.append(" </Styles>\n");

    //工作表名称
    html.append(QString(" <Worksheet ss:Name=\"%1\">\n").arg(sheetName));

    //表格开始
    html.append(QString("  <Table ss:ExpandedColumnCount=\"%1\" x:FullColumns=\"1\"\n").arg(columnCount));
    html.append("   x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"18\">\n");

    //设置字段宽度
    for (int i = 0; i < columnCount; i++) {
        html.append(QString("   <Column ss:AutoFitWidth=\"0\" ss:Width=\"%1\"/>\n").arg(columnWidths.at(i)));
    }

    //表格标题
    if (title.length() > 0) {
        html.append("   <Row ss:AutoFitHeight=\"0\" ss:Height=\"22\">\n");
        html.append(QString("    <Cell ss:MergeAcross=\"%1\" ss:StyleID=\"s_title\"><Data ss:Type=\"String\">%2</Data></Cell>\n")
                    .arg(columnCount - 1).arg(title));
        html.append("   </Row>");
    }

    //循环添加副标题/字段名/内容
    int count = content.count();
    for (int i = 0; i < count; i++) {
        //加个空行隔开
        html.append("   <Row ss:AutoFitHeight=\"0\">\n");
        html.append("   </Row>");

        //副标题1
        if (subTitle1.count() > 0 && subTitle1.count() > i) {
            if (subTitle1.at(i).length() > 0) {
                html.append("   <Row ss:AutoFitHeight=\"0\" ss:Height=\"18\">\n");
                html.append(QString("    <Cell ss:MergeAcross=\"%1\" ss:StyleID=\"s_subtitle\"><Data ss:Type=\"String\">%2</Data></Cell>\n")
                            .arg(columnCount - 1).arg(subTitle1.at(i)));
                html.append("   </Row>");
            }
        }

        //副标题2
        if (subTitle2.count() > 0 && subTitle2.count() > i) {
            if (subTitle2.at(i).length() > 0) {
                html.append("   <Row ss:AutoFitHeight=\"0\" ss:Height=\"18\">\n");
                html.append(QString("    <Cell ss:MergeAcross=\"%1\" ss:StyleID=\"s_subtitle\"><Data ss:Type=\"String\">%2</Data></Cell>\n")
                            .arg(columnCount - 1).arg(subTitle2.at(i)));
                html.append("   </Row>");
            }
        }

        //逐个添加字段名称
        if (columnCount > 0) {
            html.append("   <Row ss:AutoFitHeight=\"0\">\n");
            for (int j = 0; j < columnCount; j++) {
                html.append(QString("    <Cell ss:StyleID=\"s_normal\"><Data ss:Type=\"String\">%1</Data></Cell>\n").arg(columnNames.at(j)));
            }
            html.append("   </Row>\n");
        }

        QStringList list = content.at(i).split(";");

        //逐个添加数据
        int rowCount = list.count();
        for (int j = 0; j < rowCount; j++) {
            html.append("   <Row ss:AutoFitHeight=\"0\">\n");
            QString temp = list.at(j);
            QStringList value = temp.split("|");
            int valueCount = value.count();

            if (check) {
                //如果是历史记录则文字红色
                if (value.at(valueCount - 1) == "1") {
                    for (int k = 0; k < valueCount - 1; k++) {
                        html.append(QString("    <Cell ss:StyleID=\"s_red\"><Data ss:Type=\"String\">%1</Data></Cell>\n")
                                    .arg(value.at(k)));
                    }
                } else {
                    for (int k = 0; k < valueCount - 1; k++) {
                        html.append(QString("    <Cell ss:StyleID=\"s_normal\"><Data ss:Type=\"String\">%1</Data></Cell>\n")
                                    .arg(value.at(k)));
                    }
                }
            } else {
                for (int k = 0; k < valueCount; k++) {
                    html.append(QString("    <Cell ss:StyleID=\"s_normal\"><Data ss:Type=\"String\">%1</Data></Cell>\n")
                                .arg(value.at(k)));
                }
            }

            html.append("   </Row>\n");
        }
    }

    html.append("  </Table>\n");

    //固定结尾格式
    html.append("  <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">\n");
    html.append("   <PageSetup>\n");
    html.append("    <Header x:Margin=\"0.3\"/>\n");
    html.append("    <Footer x:Margin=\"0.3\"/>\n");
    html.append("    <PageMargins x:Bottom=\"0.75\" x:Left=\"0.7\" x:Right=\"0.7\" x:Top=\"0.75\"/>\n");
    html.append("   </PageSetup>\n");
    html.append("   <Unsynced/>\n");
    html.append("   <Selected/>\n");
    html.append("   <ProtectObjects>False</ProtectObjects>\n");
    html.append("   <ProtectScenarios>False</ProtectScenarios>\n");
    html.append("  </WorksheetOptions>\n");
    html.append(" </Worksheet>\n");
    html.append("</Workbook>\n");

    //写入文件
    QFile file(fileName);
    if (file.open(QFile::WriteOnly | QIODevice::Text)) {
        QTextStream text(&file);
        text.setCodec("UTF-8");
        text << html.join("");
    }
}

使用案例

void frmAlarm::on_btnExcel_clicked()
{
    QString type = "所有告警记录";
    QString defaultName = QString("%1/%2.xls").arg(App::AppPath).arg(STRDATETIME);
    QString file =  QFileDialog::getSaveFileName(this, "保存文件", defaultName, "Excel(*.xls)");

    if (file.length() == 0) {
        return;
    }

    QStringList content = GetContent();
    int rowCount = content.count();

    //判断数据量是否超过10000条,超过则弹出提示
    if (rowCount > 10000) {
        QString msg = QString("要导出的数据共 %1 条,请耐心等待!确定要导出数据吗?").arg(rowCount);
        if (myHelper::ShowMessageBoxQuestion(msg) != 1) {
            return;
        }
    }

    QList<int> columnWidths;
    columnWidths.append(150);
    columnWidths.append(120);
    columnWidths.append(180);
    columnWidths.append(100);
    columnWidths.append(150);

    ExcelAPI::Instance()->ToExcel(file, type, type, "", columnNames, columnWidths, content, true, false);

    QString msg = QString("导出%1到Excel").arg(type);
    DBHelper::AddEventInfoUser(msg);

    if (myHelper::ShowMessageBoxQuestion(msg + "成功!确定现在就打开吗?") == 1) {
        QString url = QString("file:///%1").arg(file);
        QDesktopServices::openUrl(QUrl(url, QUrl::TolerantMode));
    }
}
  • 3
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Qt导出数据Excel可以使用QTableWidget和QXlsxLibrary库来完成。 首先,需要在Qt项目中引入QXlsxLibrary库。可以通过在.pro文件中添加"LIBS += -L/path/to/QXlsxLibrary -lQXlsx"来链接库文件。 然后,可以使用QTableWidget来创建一个表格,并填充数据。可以使用setRowCount()和setColumnCount()方法设置表格的行数和列数,然后使用setItem()方法将每个单元格的数据填入表格中。 接下来,通过调用QXlsx::Document的相关方法来将QTableWidget中的数据导出Excel文件中。首先,创建一个QXlsx::Document对象,然后使用write()方法将表格数据写入Excel文件中。例如,可以使用writeString()方法将字符数据写入单元格中,使用writeNumber()方法将数值数据写入单元格中。 最后,可以使用saveAs()方法将Excel文件保存到本地磁盘中。可以指定文件路径和文件名来保存文件。 总结起来,通过使用QTableWidget和QXlsxLibrary库,我们可以将Qt中的数据导出Excel文件中。首先创建表格并填充数据,然后使用QXlsx::Document将数据写入Excel文件,最后保存文件到本地磁盘中。 ### 回答2: Qt是一个跨平台的C++图形用户界面应用程序开发框架,提供了丰富的库函数和工具来帮助我们开发应用程序。在Qt中,我们可以使用Qt的QAxObject类来操作Microsoft Excel应用程序,从而将数据导出Excel中。 首先,我们需要在Qt项目中包含下列头文件: ```cpp #include <QAxObject> ``` 然后,我们可以使用下面的代码来导出数据Excel中: ```cpp QAxObject* excel = new QAxObject("Excel.Application"); // 创建Excel应用程序对象 excel->setProperty("Visible", false); // 不显示Excel界面 QAxObject* workbooks = excel->querySubObject("Workbooks"); // 获取工作簿集合 QAxObject* workbook = workbooks->querySubObject("Add"); // 新建一个工作簿 QAxObject* worksheets = workbook->querySubObject("Worksheets"); // 获取工作表集合 QAxObject* worksheet = worksheets->querySubObject("Item(int)", 1); // 获取第一个工作表 QAxObject* range = worksheet->querySubObject("Range(const QString&)", "A1"); // 获取单元格范围对象 range->setProperty("Value", "Hello, Excel!"); // 将数据写入单元格 workbook->dynamicCall("SaveAs(const QString&)", "C:/example.xlsx"); // 保存工作簿 workbook->dynamicCall("Close()"); // 关闭工作簿 excel->dynamicCall("Quit()"); // 退出Excel应用程序 delete excel; ``` 以上代码通过QAxObject类与Excel应用程序进行交互,实现了在工作簿的第一个工作表中写入一条数据,并将工作簿保存为"C:/example.xlsx"文件。在实际使用时,我们可以根据需要调整写入的数据内容、保存的文件名和路径等参数。 总结起来,通过Qt的QAxObject类与Excel应用程序进行交互,我们可以方便地将数据导出Excel中,实现对Excel文件的创建、写入和保存等操作。 ### 回答3: QT是一个跨平台的开发框架,可以用于开发图形界面应用程序。在QT中,如果想要将数据导出Excel中,可以使用一些库或者API来实现。 一种方法是使用QAxObject库,该库可以用来控制COM对象,对于操作Excel非常方便。首先,需要在代码中包含QAxObject头文件。然后,可以创建一个QAxObject对象,并通过调用它的方法和属性来实现对Excel的操作。 首先,使用QAxObject::setControl()方法设置Excel的类标识符,并使用QAxObject::queryInterface()方法获取Excel的接口对象。然后,打开Excel文件,可以通过调用接口对象的方法来实现。例如,使用接口对象的setProperty()方法设置单元格的值,调用接口对象的dynamicCall()方法保存文件。最后,调用QAxObject的release()方法释放Excel对象。 另一种方法是使用QStandardItemModel和QTableView。首先,创建一个QStandardItemModel对象,并设置表格的行列和表头。然后,通过循环遍历数据,使用QStandardItemModel的setItem()方法将数据添加到表格中。接下来,将QStandardItemModel与一个QTableView对象关联起来,并将数据显示在界面上。最后,使用QStandardItemModel的save()方法将数据保存为一个CSV文件。可以打开Excel,并导入该CSV文件,从而实现将数据导出Excel中。 总之,在QT中将数据导出Excel可以通过使用QAxObject库或者QStandardItemModel与QTableView结合来实现。具体选择哪种方法取决于个人需求和习惯。希望以上的回答能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值