学习Windows excel转json工具实现 2

该代码实现了一个C++工具,利用xlnt库读取Excel文件,将数据转换成json格式。主要逻辑包括读取每个sheet的数据,存储到类中,然后生成json字符串。转换过程中检查并处理不同类型的数据,如整数、字符串和嵌套表格。
摘要由CSDN通过智能技术生成

Windows excel转json工具实现 主要源文件

介绍主要的逻辑代码 一些简单的就没有展示了

//ExcelOperator.h 转sjon的主要类
//读取excel 分为每个sheet记录数据到类内
void ExcelOperator::ReadExcel(TSTR filePath)
{
    xlnt::workbook wb;//xlnt 库
    wb.load(filePath.c_str());
    COUT << _T("sheet count:") << wb.sheet_count() << std::endl;
    int sheetCount = wb.sheet_count();
    ClearSheet();
    for (int sheetIndex = 0; sheetIndex < sheetCount; ++sheetIndex)
    {
        auto ws = wb.sheet_by_index(sheetIndex);
        xlnt::range range = ws.rows(false);
        if (range[0][0].to_string().length() == 0)
            continue;
        TSTR title;
        StrChangeT(ws.title(), title);
        COUT << _T("sheet title : ") << title << std::endl;
        //这里是分成了每个Sheet为一个记录对象
        if (m_SheetMap.find(title) != m_SheetMap.end())
        {
            COUT << _T("error seam sheet title:") << title << std::endl;
            continue;
        }
        SheetOperator* sheetOp = new SheetOperator();
        m_SheetMap.insert(std::make_pair(title, sheetOp));
        int rowIndex = 0;
        //通过xlnt 获取excel数据 存入SheetOperator里
        for (auto row : range)
        {
            ++rowIndex;
            sheetOp->m_ExcelData.insert(std::make_pair(rowIndex, std::map<int, TSTR>()));
            int colIndex = 0;
            if (row[0].to_string().length() != 0)
            {
                for (auto cell : row)
                {
                    ++colIndex;
                    TSTR tcell;
                    StrChangeT(cell.to_string(), tcell);
                    sheetOp->m_ExcelData[rowIndex].insert(std::make_pair(colIndex, tcell));
                }
            }
        }
    }
    int pos = filePath.find_last_of(_T("\\"));
    int pos2 = filePath.find_last_of(_T("."));
    if (pos != TSTR::npos && pos2 != TSTR::npos)
    {
        m_ConfigName = filePath.substr(pos+1, pos2 - pos - 1);//文件名
    }
    else
    {
        m_ConfigName = _T("NotName");
    }
    m_OutPath = filePath.substr(0, pos);//默认的输出路径
}
//创建json文本的主函数
//根据加载的excel数据生成json字符串
void ExcelOperator::CreateConfig()
{
    TSS context;
    context << "{";
    //遍历每个sheet 
    for (auto sheetIte = m_SheetMap.begin(); sheetIte != m_SheetMap.end(); ++sheetIte)
    {
        SheetOperator* pOp = sheetIte->second;
        if (!pOp)
            continue;
        if (pOp->m_ExcelData.size() == 0)
        {
            COUT << _T("create config error not data ") << std::endl;
            return;
        }
        auto fieldIte = pOp->m_ExcelData.find(DATAROW_FIELDTYPE);
        if (fieldIte == pOp->m_ExcelData.end())
        {
            COUT << _T(" not find field type row") << std::endl;
            return;
        }
        auto filedNameIte = pOp->m_ExcelData.find(DATAROW_USNAME);
        if (filedNameIte == pOp->m_ExcelData.end())
        {
            COUT << _T(" not find field us name row") << std::endl;
            return;
        }
        const std::map<int, TSTR> &fieldTypeMap = fieldIte->second;
        const std::map<int, TSTR>& fieldNameMap = filedNameIte->second;
        context << _T(&#
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值