Click here to Skip to main content
15,877,574 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to read xlsx (created by openxml) file in C++ using CDatabase. Here is the code i tried and its failing.

I posted this question yesterday but team deleted as it duplicate, but i am unable to see the question in the forum and hence posting once again.

Any help is much appreciated. Thank You.

What I have tried:

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)

    // TODO: Place code here.

    CDatabase* pExcelDatabase = NULL;//Pointer to database
    CString csExcelFile = _T("D:\\Test\\ExcelFile.xlsx");
    CString csDSN;
    //check for file existence
    if (TRUE == ::PathFileExists(csExcelFile))// checks whether the file exists or not
        csDSN.Format(_T("DRIVER=Microsoft Excel Driver (*.xlsx);DSN='';DBQ=%s"), (LPCTSTR)csExcelFile);
        return NULL;

    //Create the database
    pExcelDatabase = new CDatabase();
    HRESULT hr;
    try // to handle exceptions on opening database
        hr = pExcelDatabase->OpenEx(csDSN,CDatabase::noOdbcDialog);//open the database
    catch (...)
        if (pExcelDatabase)
            delete pExcelDatabase;
        return NULL;
    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_WINDOWSPROJECT1, szWindowClass, MAX_LOADSTRING);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
        return FALSE;

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINDOWSPROJECT1));

    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))

    return (int) msg.wParam;
Updated 2-Aug-22 4:35am

This is the third time you have posted this question. As explained before you should not repost. If you have additionla information then please edit your original question.

I explained yesterday how to find your original question: look in Your Messages. And if you still cannot find it How to read an xlsx file using CDatabase - C / C++ / MFC Discussion Boards[^].
Share this answer
Sampath579 31-Jul-22 23:17pm    
Thanks Richard. Now i can find my old question with the link above you shared.
Please delete this post. But seriously i cannot see this question under "My Questions" tab.. Not sure whats the glitch.
Richard MacCutchan 1-Aug-22 3:27am    
That is because it is under "My Messages", as I mentioned above.
Figured it out on my own after a week investigation.

Replace the connection string


"DRIVER=Microsoft Excel Driver (*.xlsx);DSN='';DBQ=%s"), (LPCTSTR)csExcelFile


Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=C:\MyExcel.xlsx;

and install MDAC 32bit since my application is 32 bit and the problem will be resolved.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900