QT读取excel

 

void TestReadExcel::readExcel()
{
 QAxObject *excel = NULL;
    QAxObject *workbooks = NULL;
    QAxObject *workbook = NULL;

    excel = new QAxObject("Excel.Application");
    if (!excel)
    {
        QMessageBox::critical(this, "错误信息", "EXCEL对象丢失");
        return;
    }
    excel->dynamicCall("SetVisible(bool)", false);
    workbooks = excel->querySubObject("WorkBooks");
    workbook = workbooks->querySubObject("Open(QString, QVariant)", QString(tr("d:\\test.xls")));
    QAxObject * worksheet = workbook->querySubObject("WorkSheets(int)", 1);//打开第一个sheet

 //QAxObject * worksheet = workbook->querySubObject("WorkSheets");//获取sheets的集合指针
 //int intCount = worksheet->property("Count").toInt();//获取sheets的数量


    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 intCols = columns->property("Count").toInt();
    int intRows = rows->property("Count").toInt();
    /*获取excel内容*/
    for (int i = intRowStart; i < intRowStart + intRows; i++)  //行
    {
        for (int j = intColStart; j < intColStart + intCols; j++)  //列
        {
            QAxObject * cell = worksheet->querySubObject("Cells(int,int)", i, j );  //获取单元格
           // qDebug() << i << j << cell->property("Value");         //*****************************出问题!!!!!!
   qDebug() << i << j <<cell->dynamicCall("Value2()").toString(); //正确
        }
    }
 workbook->dynamicCall("Close (Boolean)", false);

 //同样,设置值,也用dynamimcCall("SetValue(const QVariant&)", QVariant(QString("Help!")))这样才成功的。。

 //excel->dynamicCall("Quit (void)");
 delete excel;//一定要记得删除,要不线程中会一直打开excel.exe
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值