Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to read an xlsx file using CDatabase

How to read an xlsx file using CDatabase

Scheduled Pinned Locked Moved C / C++ / MFC
databasec++helptutorial
8 Posts 4 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sampath579
    wrote on last edited by
    #1

    Hi, I have an xlsx file, which i need to read using CDatabase in c++.. Here is the sample application i tried but its throwing an exception while opening the file.

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

    // TODO: Place code here.
    
    CDatabase\* pExcelDatabase = NULL;//Pointer to database
    CString csExcelFile = \_T("D:\\\\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 (\*.xls,\*.xlsx);DSN='';DBQ=%s"), (LPCTSTR)csExcelFile);
    }
    else
    {
        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);
    MyRegisterClass(hInstance);
    
    // 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))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    
    return (int) msg.wParam;
    

    }

    Note: Sometimes, i have to open xls file too, thats the reason in the query i added ".xls" and ".xlsx" file. If i give just ".xls" in the query and input is ".xls" file, the above code is working fine. Any help is much appreciated. Thanks

    V S 2 Replies Last reply
    0
    • S Sampath579

      Hi, I have an xlsx file, which i need to read using CDatabase in c++.. Here is the sample application i tried but its throwing an exception while opening the file.

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

      // TODO: Place code here.
      
      CDatabase\* pExcelDatabase = NULL;//Pointer to database
      CString csExcelFile = \_T("D:\\\\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 (\*.xls,\*.xlsx);DSN='';DBQ=%s"), (LPCTSTR)csExcelFile);
      }
      else
      {
          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);
      MyRegisterClass(hInstance);
      
      // 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))
          {
              TranslateMessage(&msg);
              DispatchMessage(&msg);
          }
      }
      
      return (int) msg.wParam;
      

      }

      Note: Sometimes, i have to open xls file too, thats the reason in the query i added ".xls" and ".xlsx" file. If i give just ".xls" in the query and input is ".xls" file, the above code is working fine. Any help is much appreciated. Thanks

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      Sampath579 wrote:

      its throwing an exception while opening the file.

      What exception exactly is throwing?

      S 1 Reply Last reply
      0
      • S Sampath579

        Hi, I have an xlsx file, which i need to read using CDatabase in c++.. Here is the sample application i tried but its throwing an exception while opening the file.

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

        // TODO: Place code here.
        
        CDatabase\* pExcelDatabase = NULL;//Pointer to database
        CString csExcelFile = \_T("D:\\\\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 (\*.xls,\*.xlsx);DSN='';DBQ=%s"), (LPCTSTR)csExcelFile);
        }
        else
        {
            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);
        MyRegisterClass(hInstance);
        
        // 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))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
        
        return (int) msg.wParam;
        

        }

        Note: Sometimes, i have to open xls file too, thats the reason in the query i added ".xls" and ".xlsx" file. If i give just ".xls" in the query and input is ".xls" file, the above code is working fine. Any help is much appreciated. Thanks

        S Offline
        S Offline
        Sampath579
        wrote on last edited by
        #3

        Does there is any solution for this?

        D 1 Reply Last reply
        0
        • S Sampath579

          Does there is any solution for this?

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Try answering the question you were asked about the exception.

          Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
          Dave Kreskowiak

          S 2 Replies Last reply
          0
          • V Victor Nijegorodov

            Sampath579 wrote:

            its throwing an exception while opening the file.

            What exception exactly is throwing?

            S Offline
            S Offline
            Sampath579
            wrote on last edited by
            #5

            Unhandled exception at 0x75B9CA42 in WindowsProject1.exe: Microsoft C++ exception: CDBException at memory location 0x00EFF6CC.

            L 1 Reply Last reply
            0
            • S Sampath579

              Unhandled exception at 0x75B9CA42 in WindowsProject1.exe: Microsoft C++ exception: CDBException at memory location 0x00EFF6CC.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Unfortunately that gives no clue as to where the exception occurred. So you will need to run the code in the debugger and step through it until you can find where it occurs, and why. Also note you have the following code:

              catch (...)
              {
                  if (pExcelDatabase)
                  {
                      delete pExcelDatabase;
                  }
                  return NULL;
              }
              

              So you are catching any exception but ignoring it, which is not a good idea. You need to put some proper code in there to get the type and reason information from the exception.

              1 Reply Last reply
              0
              • D Dave Kreskowiak

                Try answering the question you were asked about the exception.

                Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                Dave Kreskowiak

                S Offline
                S Offline
                Sampath579
                wrote on last edited by
                #7

                Sorry for missing this question earlier.. i posted the exception details now.

                1 Reply Last reply
                0
                • D Dave Kreskowiak

                  Try answering the question you were asked about the exception.

                  Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                  Dave Kreskowiak

                  S Offline
                  S Offline
                  Sampath579
                  wrote on last edited by
                  #8

                  Figured it out on my own. Things to be asked or answered. 1) My application is 32 bit. 2) I have MDAC 64 bit on my windows 64bit OS. 3) When the CDatabase OpenEx executes with connection string i mentioned in my above code, it checks in ODBC32.exe in which the required driver (xlsx) is not available. 4) We must have to install the MDAC 32 bit and the required driver to read the xlsx file is will be available. Hope this answer will be helpful to others in future and thought of writing my analysis. Regards Sampath. Questioning is an Art and Answering will be done by only an Artist.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups