EasyXLS

How to import data from Excel to List in C++

/* -------------------------------------------------------------------
 * Tutorial 34
 * 
 * This tutorial shows how to import Excel to List in C++. The data
 * is imported from the active sheet of the Excel file (the Excel file
 * generated in Tutorial 09).
 * ------------------------------------------------------------------- */
 
#include "EasyXLS.h"
#include <conio.h>

int main()
{
    printf("Tutorial 34\n----------\n");

    HRESULT hr;

    // Initialize COM
    hr = CoInitialize(0);

    // Use the SUCCEEDED macro and get a pointer to the interface
    if (SUCCEEDED(hr))
    {
        // Create a pointer to the interface that imports Excel files
        EasyXLS::IExcelDocumentPtr workbook;
        hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
                              NULL,
                              CLSCTX_ALL,
                              __uuidof(EasyXLS::IExcelDocument),
                              (void**) &workbook) ;

        if (SUCCEEDED(hr)){

            // Import Excel file to List
            printf("\nReading file: C:\\Samples\\Tutorial09.xlsx\n");
            EasyXLS::IListPtr rows = workbook->easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Tutorial09.xlsx");

            // Confirm import of Excel file
            _bstr_t sError = workbook->easy_getError();
            if (strcmp(sError, "") == 0){

                // Display imported List values
                for (int rowIndex=0; rowIndex<rows->size(); rowIndex++)
                {
                    EasyXLS::IListPtr row = (EasyXLS::IListPtr) rows->elementAt(rowIndex);
                    for (int cellIndex=0; cellIndex<row->size(); cellIndex++)
                    {
                        printf("At row %d, column %d the value is '%s'\n", (rowIndex+ 1), (cellIndex+ 1),
                                    (LPCSTR)((_bstr_t)row->elementAt(cellIndex)));
                    }
                }
                printf("\nPress Enter to exit ...");
            }
            else
            {
                printf("\nError reading file C:\\Samples\\Tutorial09.xls %s\n", (LPCSTR)sError);
            }

            // Dispose memory
            workbook->Dispose();
        }
        else{
            printf("Object is not available!");
        }
    }
    else{
        printf("COM can't be initialized!");
    }

    // Uninitialize COM
    CoUninitialize();

    _getch();
    return 0;
}

C++ and .NET Framework
This tutorial is valid for C++ applications that cannot use .NET Framework. It is recommended, for better performances, that if the C++ application already uses or can use .NET Framework to make use of this similar code sample.

Overloaded methods
For methods with same name but different parameters, only the first method overload retains the original name. Subsequent overloads are uniquely renamed by appending to the method name '_2', '_3', etc (method, method_2, method_3), an integer that corresponds to the order of declaration that can be found in EasyXLS.h, a file that comes with EasyXLS installation.

EasyXLS Excel libraries:

.NET
Excel Library for C++
full COM+ version to import, export or convert Excel files
Excel Reader for C++
COM+ version to read and import Excel files
Download EasyXLS™ Excel Library for C++

File formats:

MS Excel 97 - 2003
MS Excel 2007 - 2019
MS Excel 2021
Office 365
XLSXXLSMXLSBXLS
XMLHTMLCSVTXT