C++通过Excel读取错误码与错误信息及解决方案

C++通过Excel读取错误码与错误信息及解决方案

定义excel错误码表格

image.png

定义枚举与Excel相对应

   //枚举顺序必须和Excel表格中顺序一致
    //枚举的值必须与表格中的错误码一致
    enum MZCode:int
    {
        SUCCESS = 0,
        RECIPE_NOT_EXIST = 10001,
        PATTERN_MATCH_FAIL = 10002,
    };
    //将枚举注册进元对象系统
    Q_ENUM(MZCode)

构造函数进行枚举赋值MZRCode::MZRCode(QObject *parent)

使用枚举作为返回值,获取枚举错误信息与解决方案(调用)

    qDebug()<<"获取的数值类型"<< MZ_RESULT(MZRCode::MZCode::RECIPE_NOT_EXIST);
    qDebug()<<"获取的错误代码"<< MZ_RESULT_INFO(MZRCode::MZCode::RECIPE_NOT_EXIST);
    qDebug()<<"获取的解决方案"<< MZ_RESULT_SOLUTION(MZRCode::MZCode::RECIPE_NOT_EXIST);

头文件

#ifndef MZRCODE_H
#define MZRCODE_H

#include <QObject>

class MZRCode_d;
class MZRCode : public QObject
{
    Q_OBJECT
public:
    //枚举顺序必须和Excel表格中顺序一致
    //枚举的值必须与表格中的错误码一致
    enum MZCode:int
    {
        SUCCESS = 0,
        RECIPE_NOT_EXIST = 10001,
        PATTERN_MATCH_FAIL = 10002,
    };
    //将枚举注册进元对象系统
    Q_ENUM(MZCode)
public:
    explicit MZRCode(QObject *parent = nullptr);
    /**
     * @brief instance  单例
     * @return
     */
    static MZRCode* instance();
    /**
     * @brief getResultCode  通过枚举获得错误码
     * @param code
     * @return
     */
    int getResultCode(MZCode code);
    /**
     * @brief getResultInfo  通过枚举获取到相关的错误信息
     * @param code
     * @return
     */
    QString getResultInfo(MZCode code);
    /**
     * @brief getResultSolution 通过具体的枚举值获取到解决方案
     * @param code
     * @return
     */
    QString getResultSolution(MZCode code);
private:
    MZRCode_d* d = nullptr;
};
#define MZ_RESULT(code) MZRCode::instance()->getResultCode(code)
#define MZ_RESULT_SOLUTION(code) MZRCode::instance()->getResultSolution(code)
#define MZ_RESULT_INFO(code) MZRCode::instance()->getResultInfo(code)
#endif // MZRCODE_H

源文件

#include "mzrcode.h"
#include <memory>
#include <mutex>
#include "xlsxdocument.h"
#include <QMetaEnum>
class MZRCode_d
{
public:
    //QMap存储错误码与错误信息以及解决方案
    QMap<int, QMap<QString, QString>> codeMap;
    QMetaEnum metaEnum;
    bool isInit = false;
};

MZRCode::MZRCode(QObject *parent)
    : QObject{parent}
{
    d = new MZRCode_d{};
    if(!d->isInit)
    {
        QString filePath = "D:/test.xlsx";
        QXlsx::Document xlsx(filePath);
        QStringList sheetNames = xlsx.sheetNames();
        if (sheetNames.isEmpty())
        {
            return;
        }
        xlsx.selectSheet(sheetNames.first());
        int startRow = 2;
        int col = 1;
        //从元对象系统找到枚举值
        d->metaEnum =  QMetaEnum::fromType<MZCode>();
        int keyCount = d->metaEnum.keyCount();
        for (int keyIndex = 0; keyIndex < keyCount ; ++keyIndex)
        {
            int codeValue = d->metaEnum.value(keyIndex);
            int xlsxValue = xlsx.read(startRow, col).toInt();
            if(codeValue == xlsxValue)
            {
                d->codeMap[codeValue]["codeInfo"] = xlsx.read(startRow, col+1).toString();
                d->codeMap[codeValue]["Solution"] = xlsx.read(startRow, col+2).toString();
            }
            startRow++;
        }
        d->isInit = true;
    }
}

static std::unique_ptr<MZRCode> m_MZRCode = nullptr;
static std::mutex m_Mutex;
MZRCode *MZRCode::instance()
{
    if(m_MZRCode == nullptr)
    {
        std::lock_guard<std::mutex> lock(m_Mutex);
        if(m_MZRCode == nullptr)
        {
            m_MZRCode = std::make_unique<MZRCode>(new MZRCode);
        }
    }
    return m_MZRCode.get();
}
int MZRCode::getResultCode(MZCode code)
{
    // 通过值获取键
    const char* key = d->metaEnum.valueToKey(code);
    bool ok = false;
    int value = d->metaEnum.keyToValue(key,&ok);
    return ok?value: static_cast<int>(code);
}

QString MZRCode::getResultInfo(MZCode code)
{
    int keyCode = getResultCode(code);
    if(d->codeMap.contains(keyCode))
    {
       return d->codeMap[keyCode]["codeInfo"];
    }
    return "当前暂时无解决方案";
}

QString MZRCode::getResultSolution(MZCode code)
{
    int keyCode = getResultCode(code);
    if(d->codeMap.contains(keyCode))
    {
        return d->codeMap[keyCode]["Solution"];
    }
    return "当前暂无错误信息,请联系工程师进行修正";
}




测试

image.png

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值