使用QAxObject读excel

// .h
class ExcelHelper : public QObject
{
    Q_OBJECT
public:
    explicit ExcelHelper(QObject *parent = 0);
    ~ExcelHelper();
  

    bool newExcel(const QString &fileName);
    QVariantList ReadExcel(const QString &fileName);

private:
    QAxObject *ExcelApplication = Q_NULLPTR;
    QAxObject *WorkBooks = Q_NULLPTR;
    QAxObject *WorkBook = Q_NULLPTR;
    QAxObject *Sheets = Q_NULLPTR;
    QAxObject *Sheet = Q_NULLPTR;
};

//.cpp
ExcelHelper::~ExcelHelper()
{
    if(ExcelApplication != Q_NULLPTR)
    {
        ExcelApplication->deleteLater();
        ExcelApplication = Q_NULLPTR;
    }
}

bool ExcelHelper::newExcel(const QString &fileName)
{
    ExcelApplication = new QAxObject("Excel.Application");
    if (ExcelApplication->isNull())
    {
        if (ExcelApplication != Q_NULLPTR) //网络中很多使用excel==Q_NULLPTR判断,是错误的
        {
            ExcelApplication->dynamicCall("Quit()");
            delete ExcelApplication;
            ExcelApplication = Q_NULLPTR;
        }
        QMessageBox::critical(0, "错误信息", "没有找到EXCEL应用程序");
        return false;
    }
    ExcelApplication->dynamicCall("SetVisible(bool)", false);//false不显示窗体
    ExcelApplication->setProperty("DisplayAlerts", false);//不显示任何警告信息。
    WorkBooks = ExcelApplication->querySubObject("Workbooks");
    if(WorkBooks == Q_NULLPTR)
    {
        QMessageBox::critical(0, "错误信息", "取得Excel的Workbooks失败");
       return false;
    }

    QFile file(fileName);
    if (file.exists())
    {
        WorkBook = WorkBooks->querySubObject("Open(const QString &)", fileName);
    }
    else
    {
        WorkBooks->dynamicCall("Add");
        WorkBook = ExcelApplication->querySubObject("ActiveWorkBook");
    }
    if(WorkBook == Q_NULLPTR)
    {
        QMessageBox::critical(0, "错误信息", "打开Excel的Workbook失败");
        return false;
    }

    //默认有一个sheet
    Sheets = WorkBook->querySubObject("Sheets");
    if(Sheets == Q_NULLPTR)
    {
        QMessageBox::critical(0, "错误信息", "打开Excel的Sheets失败");
        return false;
    }
    Sheet = Sheets->querySubObject("Item(int)", 1);
    if(Sheet == Q_NULLPTR)
    {
        QMessageBox::critical(0, "错误信息", "打开Excel的Sheet失败");
        return false;
    }
    return true;
}

QVariantList ExcelHelper::ReadExcel(const QString &fileName)
{
    QVariantList allDataList;
    if(!newExcel(fileName))
        return allDataList;

    QAxObject * range = Sheet->querySubObject("UsedRange");
    if(range == Q_NULLPTR)
    {
        WLog(LOG_ERR,"range == Q_NULLPTR,save excel[%s] err",fileName.toLocal8Bit().data());
    }
    if(range != Q_NULLPTR)
    {
        // 直接用dynamicCall调用Value()函数获取全部数据,取出来的数据相当于QList>类型
        QVariant var = range->dynamicCall("Value");
        allDataList = var.toList();

        /* 有博客用以下的方法读取全部数据,试了行列数都读出来了,到最后调property("Value")时
            返回来的QList是空的,一直都没有数据。
            见:
            Qt使用QAxObject快速批量读取Excel内容
            https://blog.csdn.net/sandeepin/article/details/71246494
        */
        /*QAxObject * cols = range->querySubObject("Columns");
        QAxObject * rows = range->querySubObject("Rows");
        int colCnt = 0, rowCnt = 0;
        if(cols != Q_NULLPTR)
        {
            colCnt = cols->property("Count").toInt();
        }
        if(rows != Q_NULLPTR)
        {
            rowCnt = rows->property("Count").toInt();
        }
        WLog(LOG_DEBUG,"excel行数:%d,列数:%d",colCnt,rowCnt);
        QString name; 
        int first = colCnt/26;
        while(first>0)
        {
            name += QString("%1").arg(QChar('A'+first-1));
            first /= 26;
        }
        int second = colCnt%26;
        name += QString("%1").arg(QChar('A'+second-1));
        QString rangeStr = QString("A1:%1%2").arg(name).arg(rowCnt);
        QAxObject *allData = range->querySubObject("Range(QString)", rangeStr);//调了没起作用
        //QAxObject *allData = Sheet->querySubObject("Range(QString)", rangeStr);//调了没起作用
        allDataList = allData->property("Value").toList(); //返回值列表为空 */
    }
    return allDataList;
}
// 调用
void ReadExcel(const QString &filename)
{
        ExcelHelper helper;
        QVariantList ls = helper.ReadExcel(filename);
        if(ls.isEmpty())
        {
            return;
        }

        // 将获得的数据转为QList>
        QList> dataLs;
        for(int row=0; row

 

你可能感兴趣的:(qt)