Qt读取excel

来源:互联网 发布:微信支付退款接口 php 编辑:程序博客网 时间:2024/05/17 08:50
1.工程配置:
项目->属性
所有配置:链接器->常规:附加库目录: E:\Qt\lib;"$(QTDIR)\lib"
 Debug:    链接器->常规:附加库目录: E:\Qt\lib;"$(QTDIR)\lib"
                 链接器->输入:附加依赖项:QAxServer.lib
                                                               QAxServerd.lib
                                                               QAxContainerd.lib
Release:    链接器->常规:附加库目录: E:\Qt\lib;"$(QTDIR)\lib"

2.包含头文件
#include <QApplication>
#include <QAxObject>
#include <QVariant>
              
3.
    QAxObject *excel;
    QAxObject *workbooks;
    QAxObject *workbook;
readexcel::readexcel(int n)
{
    excel = new QAxObject("Excel.Application");
    if(!excel)
        return;

    excel->dynamicCall("SetVisible(bool)",false);//不显示窗体
    workbooks = excel->querySubObject("WorkBooks");//获取工作薄集合
    //打开已经存在的工作薄
    workbook = workbooks->querySubObject("Open(QString,QVarient)",QString(QString::fromUtf8("G:/Study/College Course/Graduation Project/readVRML/weldingspot/weldingspot.xlsx")));

    get_line_n(n); //获取第n排焊点坐标

    workbook->dynamicCall("Close (Boolean)", false);
}

readexcel::~readexcel()
{
    delete excel;
}

void readexcel::get_line_n(int n)
{
    QAxObject *worksheet = workbook->querySubObject("WorkSheets(int)",n);//打开第一个sheet

    QAxObject *usedrange = worksheet->querySubObject("UsedRange");//获取该sheet的使用范围对象
    QAxObject *rows = usedrange->querySubObject("Rows");//行
    QAxObject *columns = usedrange->querySubObject("Columns");//列

    /*获取行数和列数*/
    int intRowStart = usedrange->property("Row").toInt();
    int intColStart = usedrange->property("Column").toInt();
    int intRows = rows->property("Count").toInt();
    int intCols = columns->property("Count").toInt();

    /*获取excel的内容*/
    int i,j;
    for(i=intRowStart;i<intRowStart+intRows;i++)
    {
        for(j=intColStart;j<intColStart+intCols;j++)
        {
            QAxObject *cell = worksheet->querySubObject("Cells(int,int)",i,j);//获取单元格的值
            line[i-1][j-1] = cell->dynamicCall("Value2()").toDouble();
        }
    }
}
0 0
原创粉丝点击