libopc读页数超过10的问题及解决方法

博客内容涉及一个XML处理的问题,指出在[Content_Types].xml文件中,由于xmlStrcmp函数按字符而非数字顺序比较导致工作表顺序错误。作者尝试修改函数但引发其他问题。最终解决方案是从workbook.xml.rels文件读取数据,通过查找workbookpart索引来正确获取工作表顺序。这个方法成功解决了读写混乱的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于xmlStrcmp函数比较字符串的时候不是按照数字比较的,而是按照字符比较的因此存在此问题。

在[Content_Types].xml文件中存储的顺序不正确。当前的内容如下:

<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
    <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
    <Default Extension="xml" ContentType="application/xml"/>
    <Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
    <Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
    <Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>
    <Override PartName="/xl/worksheets/Sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>

<Override PartName="/xl/worksheets/Sheet10.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
    <Override PartName="/xl/worksheets/Sheet2.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
    <Override PartName="/xl/worksheets/Sheet3.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
    <Override PartName="/xl/worksheets/Sheet4.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
    <Override PartName="/xl/worksheets/Sheet5.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
    <Override PartName="/xl/worksheets/Sheet6.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
    <Override PartName="/xl/worksheets/Sheet7.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
    <Override PartName="/xl/worksheets/Sheet8.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
    <Override PartName="/xl/worksheets/Sheet9.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
</Types>

读取的时候,uri是从[Content_Types].xml文件中按照顺序读取的。第二页的数据是第10页的数据,因此造成混乱。

尝试修改xmlStrcmp,按照数字顺序比较。但是会造成其他的问题。比如,写xlsx后打开失败。

最后的方案,从workbook.xml.rels文件中读取uri。读写都成功!

增加代码如下:

//查找workbook part 索引

int CXlsxHelper::FindPartIndex(opcContainer* c, const xmlChar* type)
{
    int ret = -1;

    for(unsigned int u = 0U; u < c->part_items; u++)
    {
        if(xmlStrcmp(c->part_array[u].type, type) == 0)
        {
            ret = u;
            break;
        }
    }

    return ret;
}

//按照类型和顺序读取target

const xmlChar* CXlsxHelper::FindPartRelationShipName(opcContainer* c, const xmlChar* type, int partIndex, unsigned int* ub)
{
    xmlChar* ret(nullptr);

    for(unsigned int u = (ub == nullptr ? 0 : *ub); u < c->part_array[partIndex].relation_items; u++)
    {
        if(xmlStrcmp(c->part_array[partIndex].relation_array[u].relation_type, type) == 0)
        {
            ret = c->part_array[partIndex].relation_array[u].target_ptr;
            if(ub) *ub = u + 1;
            break;
        }
    }

    return ret;
}

workbook.xml.rels文件内容

<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
    <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/Sheet1.xml"/>
    <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/Sheet2.xml"/>
    <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/Sheet3.xml"/>
    <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/Sheet4.xml"/>
    <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/Sheet5.xml"/>
    <Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/Sheet6.xml"/>
    <Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/Sheet7.xml"/>
    <Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/Sheet8.xml"/>
    <Relationship Id="rId9" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/Sheet9.xml"/>
</Relationships>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值