将xlsx文件转换为csv文件c ++

时间:2015-01-15 13:45:54

标签: c++ csv xlsx libopc

我正在尝试使用LibOPC读取xlsx文件并写入csv文件。 xlsx文件包含各种数据类型,文件结构很重要。 我使用以下代码来读取共享字符串,但我不确定如何将字符串和其他数据类型与正确的结构相关联:

#include "stdafx.h"
#include <opc/opc.h>
#include <stdio.h>
// basic file operations 
#include <iostream> 
#include <fstream> 

using namespace std;

FILE* pFile;

static void dumpSharedrows(mceTextReader_t *reader)
{
#if 0
    xmlChar *ln = xmlStrdup(xmlTextReaderLocalName(reader->reader));
    const char * lnvalue = (const char*)ln;
    if ((strcmp(lnvalue, "si")) == 0) {
        //id3++; 
    }
#endif
    mce_skip_attributes(reader);
    //mce_start_attributes(reader){ }
    //mce_end_attributes(reader);

    mce_start_children(reader)
    {
        mce_start_element(reader, NULL, NULL)
        {
            dumpSharedrows(reader);
        }
        mce_end_element(reader);

        mce_start_text(reader)
        {

            for (const xmlChar *txt = xmlTextReaderConstValue(reader->reader); 0 != *txt; txt++)
            {
                putc(*txt, pFile);
            }
            putc(',', pFile);
            //  putc(*txt, stdout);
            //putc('\n', stdout);
            //remove((const char *)xmlTextReaderConstValue(reader->reader));

            //LOGI("%s", xmlTextReaderConstValue(reader->reader));

        }
        mce_end_text(reader);
    }
    mce_end_children(reader);

}






int _tmain(int argc, _TCHAR* argv[])
{

    const char *filename = "template.csv";
        const char *mode = "w+";
        fopen_s(&pFile, filename, mode);
    opcInitLibrary();
    opcContainer *c = opcContainerOpen(_X("template.xlsx"), OPC_OPEN_READ_ONLY, NULL, NULL);
    if (NULL != c) {
        mceTextReader_t reader;

        if (OPC_ERROR_NONE == opcXmlReaderOpen(c, &reader, _X("xl/sharedStrings.xml"), NULL, 0, 0))
        {
            mce_start_document(&reader)
            {
                mce_start_element(&reader, NULL, NULL)
                {
                    dumpSharedrows(&reader);
                                }
                mce_end_element(&reader);
            }
            mce_end_document(&reader); 
                        mceTextReaderCleanup(&reader);
        }
        opcContainerClose(c, OPC_CLOSE_NOW);
    }
    opcFreeLibrary();
    fclose(pFile);
    return 0;
}

非常感谢任何帮助或建议。

0 个答案:

没有答案