Click here to Skip to main content
15,440,341 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: FileMapping book exmple goes in loop Pin
Mircea Neacsu8-Aug-22 11:47
mvaMircea Neacsu8-Aug-22 11:47 
Questiondefining a function with #define Pin
coco24330-Jul-22 9:03
Membercoco24330-Jul-22 9:03 
AnswerRe: defining a function with #define Pin
Mircea Neacsu30-Jul-22 9:54
mvaMircea Neacsu30-Jul-22 9:54 
GeneralRe: defining a function with #define Pin
trønderen30-Jul-22 12:46
Membertrønderen30-Jul-22 12:46 
GeneralRe: defining a function with #define Pin
Greg Utas30-Jul-22 13:58
mveGreg Utas30-Jul-22 13:58 
AnswerRe: defining a function with #define Pin
Randor 30-Jul-22 9:55
professional Randor 30-Jul-22 9:55 
GeneralRe: defining a function with #define Pin
coco2433-Aug-22 9:01
Membercoco2433-Aug-22 9:01 
QuestionHow to read an xlsx file using CDatabase Pin
Sampath57927-Jul-22 19:54
MemberSampath57927-Jul-22 19:54 
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
AnswerRe: How to read an xlsx file using CDatabase Pin
Victor Nijegorodov27-Jul-22 20:07
MemberVictor Nijegorodov27-Jul-22 20:07 
GeneralRe: How to read an xlsx file using CDatabase Pin
Sampath57931-Jul-22 23:27
MemberSampath57931-Jul-22 23:27 
GeneralRe: How to read an xlsx file using CDatabase Pin
Richard MacCutchan31-Jul-22 23:38
mveRichard MacCutchan31-Jul-22 23:38 
GeneralRe: How to read an xlsx file using CDatabase Pin
Dave Kreskowiak31-Jul-22 17:30
mveDave Kreskowiak31-Jul-22 17:30 
GeneralRe: How to read an xlsx file using CDatabase Pin
Sampath57931-Jul-22 23:40
MemberSampath57931-Jul-22 23:40 
GeneralRe: How to read an xlsx file using CDatabase Pin
Sampath5791-Aug-22 23:21
MemberSampath5791-Aug-22 23:21 
QuestionHow to use UTF8 characters in C++ Pin
JohnCodding27-Jul-22 3:00
MemberJohnCodding27-Jul-22 3:00 
AnswerRe: How to use UTF8 characters in C++ Pin
Mircea Neacsu27-Jul-22 3:54
mvaMircea Neacsu27-Jul-22 3:54 
GeneralRe: How to use UTF8 characters in C++ Pin
JohnCodding27-Jul-22 19:40
MemberJohnCodding27-Jul-22 19:40 
AnswerRe: How to use UTF8 characters in C++ Pin
Richard MacCutchan27-Jul-22 4:40
mveRichard MacCutchan27-Jul-22 4:40 
QuestionI Moved this question to the proper forum "Managed C++/CLI" - Sorry for posting in the wrong place :¬) Pin
madusmacus27-Jul-22 2:33
Membermadusmacus27-Jul-22 2:33 
AnswerRe: I hope there is a Managed C++/CLR person left to help me with Parallel::For Pin
Victor Nijegorodov27-Jul-22 4:02
MemberVictor Nijegorodov27-Jul-22 4:02 
GeneralRe: I hope there is a Managed C++/CLR person left to help me with Parallel::For Pin
madusmacus27-Jul-22 4:24
Membermadusmacus27-Jul-22 4:24 
QuestionUpgrade my skills... Pin
Kornfeld Eliyahu Peter21-Jul-22 0:45
professionalKornfeld Eliyahu Peter21-Jul-22 0:45 
AnswerRe: Upgrade my skills... Pin
Richard MacCutchan21-Jul-22 1:00
mveRichard MacCutchan21-Jul-22 1:00 
PraiseRe: Upgrade my skills... Pin
Kornfeld Eliyahu Peter21-Jul-22 1:12
professionalKornfeld Eliyahu Peter21-Jul-22 1:12 
GeneralRe: Upgrade my skills... Pin
Richard MacCutchan21-Jul-22 1:21
mveRichard MacCutchan21-Jul-22 1:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.