How to read an xlsx file using CDatabase
-
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
-
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
Sampath579 wrote:
its throwing an exception while opening the file.
What exception exactly is throwing?
-
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
Does there is any solution for this?
-
Does there is any solution for this?
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 -
Sampath579 wrote:
its throwing an exception while opening the file.
What exception exactly is throwing?
Unhandled exception at 0x75B9CA42 in WindowsProject1.exe: Microsoft C++ exception: CDBException at memory location 0x00EFF6CC.
-
Unhandled exception at 0x75B9CA42 in WindowsProject1.exe: Microsoft C++ exception: CDBException at memory location 0x00EFF6CC.
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.
-
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 KreskowiakSorry for missing this question earlier.. i posted the exception details now.
-
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 KreskowiakFigured 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.