1

I am trying to read an xlsx file and write to a csv file using LibOPC. The xlsx file contains various data types and the structure of the file is important. I have used the following code to read the sharedstrings, but I'm not sure how to relate the strings and other data types to the correct structure:

#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;
}

Any help or suggestions would be greatly appreciated.

Jak
  • 471
  • 5
  • 20
  • if you're building a small converter, why not use a more appropriate tool for that job? C#/Ruby/whatever else, but not C++ – Dmitry Ledentsov Jan 15 '15 at 13:51
  • That is becoming an option, but I'd still need to use it from within my MFC/C++ application. – Jak Jan 15 '15 at 13:53
  • 2
    Spawn an executable to do the task for you in your MFC application. I use this in linux: https://github.com/dilshod/xlsx2csv – drescherjm Jan 15 '15 at 14:02
  • you can bind or call whatever language from your mfc application too. Just to illustrate the difference between the levels of abstraction: http://ideone.com/25eLm6 – Dmitry Ledentsov Jan 15 '15 at 14:05
  • This seems to be the easiest route: http://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line – Jak Jan 15 '15 at 14:23

0 Answers0