EasyXLS

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

/* ------------------------------------------------------------------
 * Tutorial 35
 * 
 * This tutorial shows how to import Excel sheet to List in C++.
 * The data is imported from a specific Excel sheet (For this example
 * we use the Excel file generated in Tutorial 09).
 * ------------------------------------------------------------------ */

#include "EasyXLS.h"
#include <conio.h>

int main()
{
    printf("Tutorial 35\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 sheet to List
            printf("\nReading file: C:\\Samples\\Tutorial09.xlsx\n");
            EasyXLS::IListPtr rows = workbook->easy_ReadXLSXSheet_AsList_3(
                "C:\\Samples\\Tutorial09.xlsx", "First Tab");

            // 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