Poniżej jest przykład odczytu konkretnych pól z arkusza Excel, proszę przeanalizować też kod...

user image

void CReadExcelDlg::OnButton1() 
    CDatabase database;
    CString sSql;
    CString sItem1, sItem2;
    CString sDriver;
    CString sDsn;
    CString sFile = "ReadExcel.xls";        // the file name. Could also be something like C:\\Sheets\\WhatDoIKnow.xls

    // Clear the contents of the listbox

    // Retrieve the name of the Excel driver. This is 
    // necessary because Microsoft tends to use language
    // specific names like "Microsoft Excel Driver (*.xls)" versus
    // "Microsoft Excel Treiber (*.xls)"
    sDriver = GetExcelDriver();
    if( sDriver.IsEmpty() )
        // Blast! We didn´t find that driver!
        AfxMessageBox("No Excel ODBC driver found");

    // Create a pseudo DSN including the name of the Driver and the Excel file
    // so we don´t have to have an explicit DSN installed in our ODBC admin

        // Open the database using the former created pseudo DSN

        // Allocate the recordset
        CRecordset recset( &database );

        // Build the SQL string
        // Remember to name a section of data in the Excel sheet using "Insert->Names" to be
        // able to work with the data like you would with a table in a "real" database. There
        // may be more than one table contained in a worksheet.
        sSql =  "SELECT field_1, field_2 "      
                "FROM demo_table "                                       "ORDER BY field_1";

        // Execute that query (implicitly by opening the recordset)

        // Browse the result
        while( !recset.IsEOF() )
            // Read the result line

            // Insert result into the list
            m_ctrlList.AddString( sItem1 + " --> "+sItem2);

            // Skip to the next resultline

        // Close the database

    CATCH(CDBException, e)
        // A database exception occured. Pop out the details...
        AfxMessageBox("Database error: "+e->m_strError);

// Get the name of the Excel-ODBC driver
CString CReadExcelDlg::GetExcelDriver()
    char szBuf[2001];
    WORD cbBufMax = 2000;
    WORD cbBufOut;
    char *pszBuf = szBuf;
    CString sDriver;

    // Get the names of the installed drivers ("odbcinst.h" has to be included )
   if(!SQLGetInstalledDrivers(szBuf,cbBufMax,& cbBufOut))
        return "";

    // Search for the driver...
        if( strstr( pszBuf, "Excel" ) != 0 )
            // Found !
            sDriver = CString( pszBuf );
        pszBuf = strchr( pszBuf, '\0' ) + 1;
    while( pszBuf[1] != '\0' );

    return sDriver;

A ja szukam odpowiedzi jak dobrać się do całego arkusza:

sSql = "SELECT * "
"FROM ???????????"
"ORDER BY field_1";

żródło w VS C++ 6.0