当前位置:网站首页>Openxlsx field reading problem

Openxlsx field reading problem

2022-07-05 07:35:00 sukhoi27smk

In the reading excel I found that some fields cannot be read , Through the excel After decompressing the file, we found , Normal read and no

The fields that can be read normally are sharedString.xml The format stored in is different , Take one of the fields , Here's the picture :

Normally read

Unreadable

 

The difference can be seen by comparison , Then look for openxlsx Treatment of this piece , Find the following code by debugging the code :

const char* XLSharedStrings::getString(uint32_t index) const
{
    auto iter = xmlDocument().document_element().children().begin();
    std::advance(iter, index);
    return iter->first_child().text().get();
}

By analyzing the code context , And combine getString function , It is found that the contents of this field are stored in multiple subordinate nodes , Therefore, the above storage format cannot be processed , We got a problem , Just make a compatible , The modified code is as follows :

const char* XLSharedStrings::getString(uint32_t index) const
{
    auto iter = xmlDocument().document_element().children().begin();
    std::advance(iter, index);
	static std::string t;
	t = "";
	t = iter->first_child().text().get();

	pugi::xml_node iter_r = iter->first_child();
	while (!iter_r.empty())
	{
		if (!strcmp(iter_r.name(), "r"))
		{
			pugi::xml_node iter_t = iter_r.first_child();
			while (!iter_t.empty())
			{
				if (!strcmp(iter_t.name(), "t"))
				{
					t = t + iter_t.text().get();
				}
				iter_t = iter_t.next_sibling();
			}
		}
		iter_r = iter_r.next_sibling();
	}
	
	return t.c_str();
}

原网站

版权声明
本文为[sukhoi27smk]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140553428153.html