• Qt操作excel


    QAxWidget excel("Excel.Application");
    1) 显示当前窗口:
    excel.setProperty("Visible", true);
    2) 更改 Excel 标题栏:
    excel.setProperty("Caption", "Invoke Microsoft Excel");
    3) 添加新工作簿:
    QAxObject * workbooks = excel.querySubObject("WorkBooks");
    workbooks->dynamicCall("Add");
    4) 打开已存在的工作簿:
    workbooks->dynamicCall("Open (const QString&)", QString("c:/test.xls"));
    5) 获取活动工作簿:
    QAxObject * workbook = excel.querySubObject("ActiveWorkBook");
    6) 获取所有的工作表:
    QAxObject * worksheets = workbook->querySubObject("WorkSheets");
    7) 获取工作表数量:
    int intCount = worksheets->property("Count").toInt();
    8) 获取第一个工作表:
    QAxObject * worksheet = workbook->querySubObject("Worksheets(int)", 1);
    9) 获取cell的值:
    QAxObject * range = worksheet->querySubObject("Cells(int,int)", 1, 1 );

     

     

    示例代码:

    #include <QtGui>
    #include <QAxObject>
    #include <QAxWidget>
    #include <qaxselect.h>  //备注:在我(GUOBBS,不是原作者)的测试中,已删掉此行
    int main(int argc, char **argv)
    {
        QApplication a(argc, argv);
        QAxSelect select;
        select.show();
        QAxWidget excel("Excel.Application");
        excel.setProperty("Visible", true);
        QAxObject * workbooks = excel.querySubObject("WorkBooks");
        workbooks->dynamicCall("Open (const QString&)", QString("c:/test.xls"));
        QAxObject * workbook = excel.querySubObject("ActiveWorkBook");
        QAxObject * worksheets = workbook->querySubObject("WorkSheets");
        int intCount = worksheets->property("Count").toInt();
        for (int i = 1; i <= intCount; i++)
        {
            int intVal;
            QAxObject * worksheet = workbook->querySubObject("Worksheets(int)", i);
            qDebug() << i << worksheet->property("Name").toString();
            QAxObject * range = worksheet->querySubObject("Cells(1,1)");
            intVal = range->property("Value").toInt();
            range->setProperty("Value", QVariant(intVal+1));
            QAxObject * range2 = worksheet->querySubObject("Range(C1)");
            intVal = range2->property("Value").toInt();
            range2->setProperty("Value", QVariant(intVal+1));
        }
        QAxObject * worksheet = workbook->querySubObject("Worksheets(int)", 1);
        QAxObject * usedrange = worksheet->querySubObject("UsedRange");
        QAxObject * rows = usedrange->querySubObject("Rows");
        QAxObject * columns = usedrange->querySubObject("Columns");
        int intRowStart = usedrange->property("Row").toInt();
        int intColStart = usedrange->property("Column").toInt();
        int intCols = columns->property("Count").toInt();
        int intRows = rows->property("Count").toInt();
        for (int i = intRowStart; i < intRowStart + intRows; i++)
        {
            for (int j = intColStart; j <= intColStart + intCols; j++)
            {
                QAxObject * range = worksheet->querySubObject("Cells(int,int)", i, j );
                qDebug() << i << j << range->property("Value");
            }
        }
        excel.setProperty("DisplayAlerts", 0);
        workbook->dynamicCall("SaveAs (const QString&)", QString("c:/xlsbyqt.xls"));
        excel.setProperty("DisplayAlerts", 1);
        workbook->dynamicCall("Close (Boolean)", false);
        excel.dynamicCall("Quit (void)");
        return a.exec();
    }

    以上文章来源:http://blog.csdn.net/tingsking18/article/details/5677353

    补充说明:

    工程*.pro的文件需要加上下面这行以加载COM组件

    CONFIG += qaxcontainer

  • 相关阅读:
    sqlmap注入分类
    sqlmap简单中文说明
    【Python Learning第一篇】Linux命令学习及Vim命令的使用
    模拟退火算法从原理到实战【基础篇】
    平面上给定n条线段,找出一个点,使这个点到这n条线段的距离和最小。
    使用VMWareWorkstation10搭建学习环境笔记
    洛谷P1313 计算系数【快速幂+dp】
    浅析Numpy.genfromtxt及File I/O讲解
    持续交付中高效率与高质量
    持续集成CI与自动化测试
  • 原文地址:https://www.cnblogs.com/guobbs/p/3915602.html
Copyright © 2020-2023  润新知