Qt读取excel的代码,已经封装好了


核心代码:

QAxObject* excel = new QAxObject("Excel.Application");
    excel->setProperty("Visible", false);
    QAxObject* workbooks = excel->querySubObject("WorkBooks");
    workbooks->dynamicCall("Open (const QString&)", QString("c:\\副本1.xls")); //filename
    QAxObject* workbook = excel->querySubObject("ActiveWorkBook");
    QAxObject* worksheets = workbook->querySubObject("WorkSheets");
    QAxObject* worksheet = workbook->querySubObject("Worksheets(int)", 1); //worksheet number
    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();
    QAxObject * cell;
    for (int i = intRowStart; i < intRowStart + intRows; i++)
    {
        for (int j = intColStart; j < intColStart + intCols; j++)
        {
            cell = excel->querySubObject("Cells(Int, Int)", i, j );
            QVariant cellValue = cell->dynamicCall("value");

            cout << "Content " << cellValue.toString().toStdString() << endl; //value of the cell
        }
    }
    excel->setProperty("DisplayAlerts", 0);
    workbook->dynamicCall("Save(void)");
    workbook->dynamicCall("Close (Boolean)", false);
    excel->setProperty("DisplayAlerts",1);

    delete excel;


========================================

========================================


封装好的类如下:


--

readexcel.h

#ifndef READEXCEL_H
#define READEXCEL_H

#include <map>
#include <string>
#include <vector>
#include <iostream>
#include <QAxObject>
#include <QString>
#include <QFile>


class Position
{
public:
    Position() {
        m_row = 0;
        m_col = 0;
    }

    Position(int row, int col) {
        m_row = row;
        m_col = col;
    }
public:
    int m_row;
    int m_col;

public:
    bool operator<(const Position & ct) const   // 两个const是必需的。
    {
        if (m_row < ct.m_row) {
            return true;
        } else if (m_row == ct.m_row) {
            if ( m_col < ct.m_col ) {
                return true;
            }
        }
        return false;
    }
};

class ReadExcel
{
public:
    ReadExcel();
    ~ReadExcel();

public:
    //打开excel文件
    bool openExcel(const QString& filename);
    //获取 指定单元格的数据
    std::string getCellData(const int& row, const int& col);
    //获取 行数,列数
    void getInfo(int& row, int& col) const;

private:
    void getALLfromExcel();

private:
    int m_row;    //行
    int m_col;    //列
    QString m_filename;
    QAxObject* m_excel;
    std::map<Position, std::string> m_mapdata;
    Position p;
};

#endif // READEXCEL_H


--

readexcel.cpp

#include "readexcel.h"
#include <QDebug>
#include <iostream>

ReadExcel::ReadExcel()
    :m_row(0), m_col(0), m_filename("")
{
    m_excel = new QAxObject("Excel.Application");
}

ReadExcel::~ReadExcel()
{
    if (!m_mapdata.empty()) {
        m_mapdata.clear();
    }
    delete m_excel;
}

//
bool ReadExcel::openExcel(const QString &filename)
{
    if (filename.isEmpty()) {
        m_row = 0;
        m_col = 0;
        return false;
    }
    QFile file(filename);
    if (!file.exists()){
        m_row = 0;
        m_col = 0;
        return false;
    };
    if (!m_mapdata.empty()) {
        m_mapdata.clear();
    }
    m_filename = filename;
    try {
        getALLfromExcel();
    } catch (...) {
        return false;
    }

    return true;
}

void ReadExcel::getInfo(int &row, int &col) const
{
    row = m_row;
    col = m_col;
}

std::string ReadExcel::getCellData(const int &row, const int &col)
{
    if (row >= 1 && row <= m_row && col >= 1 && col <= m_col) {
        p.m_row = row;
        p.m_col = col;
        return m_mapdata[p];
    } else {
        return NULL;
    }
}

void ReadExcel::getALLfromExcel()
{
    m_excel->setProperty("Visible", 0);
    QAxObject* workbooks = m_excel->querySubObject("WorkBooks");
    workbooks->dynamicCall("Open (const QString&)", m_filename);
    QAxObject* workbook = m_excel->querySubObject("ActiveWorkBook");
    QAxObject* worksheets = workbook->querySubObject("WorkSheets");
    QAxObject* worksheet = workbook->querySubObject("Worksheets(int)", 1); //worksheet number
    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();
    m_row = intRows;
    m_col = intCols;
    QAxObject * cell;
    for (int i = intRowStart; i < intRowStart + intRows; i++)
    {
        for (int j = intColStart; j < intColStart + intCols; j++)
        {
            Position pos(i, j);
            cell = m_excel->querySubObject("Cells(Int, Int)", i, j );
            QVariant cellValue = cell->dynamicCall("value");
            m_mapdata.insert(std::pair<Position, std::string>(pos, cellValue.toString().toStdString()));
        }
    }
    m_excel->setProperty("DisplayAlerts", 0);
    workbook->dynamicCall("Save(void)");
    workbook->dynamicCall("Close (Boolean)", false);
    m_excel->setProperty("DisplayAlerts",1);
}







----




  • 7
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值