Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to copy excel data in my Cstring Array ..i trying this code



C++
     CString strpath;
     m_wndCtrl.GetWindowTextW(strpath);//path of excel
     LPCTSTR str=(LPCTSTR)strpath;
     CApplication app;  // app is an _Application object.
     CWorkbook book;
     CWorksheet sheet;
     CWorkbooks books;
     CWorksheets sheets;
     CRange range;
     char buf[1024];
     VARIANT ret;
     LPDISPATCH lpDisp; // IDispatch *; pointer reused many times.
     long count; // Count of the number of worksheets.
     int i;

     // Common OLE variants. These are easy variants to use for
     // calling arguments.
     COleVariant
                covTrue((short)TRUE),
                covFalse((short)FALSE),
                covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

     // Start Excel and get Application object.
     if(!app.CreateDispatch(_T("Excel.Application")))
     {
     // AfxMessageBox(_T("Couldn't CreateDispatch on Excel"));
      return;
     }


     // Set visible.
    // app.set_Visible(TRUE);

     // Get Workbooks collection.
     lpDisp = app.get_Workbooks();  // Get an IDispatch pointer

     ASSERT(lpDisp);               // or fail.
     books.AttachDispatch( lpDisp );  // Attach the IDispatch pointer
                                      // to the books object.

        // Open a workbook.
        lpDisp = books.Open(str,
                       covOptional, covOptional, covOptional, covOptional,
                       covOptional, covOptional, covOptional, covOptional,
                       covOptional, covOptional, covOptional, covOptional,
                       covOptional, covOptional); // Excel 2000 has 13 parameters
        ASSERT(lpDisp);  // It worked!



     // Attach to a Workbook object.
     book.AttachDispatch( lpDisp );  // Attach the IDispatch pointer
                                     // to the book object.

     // Get sheets.
     lpDisp = book.get_Sheets();
     ASSERT(lpDisp);
     sheets.AttachDispatch(lpDisp);

     // Get the number of worksheets in this book.
     count = sheets.get_Count();
     sprintf(buf, "%ld worksheets in this Workbook.", count);/*
     ::MessageBox(NULL, buf, "Sheet Count", MB_OK | MB_SETFOREGROUND);*/

     // Enumerate through worksheets in book and activate in
     // succession.

sheet = sheets.get_Item(COleVariant((short)1));

range = sheet.get_Range(COleVariant(TEXT("A1")),COleVariant(TEXT("B1")));//.u.UsedRange;

 // Close the workbook without saving changes
    //and quit Microsoft Excel.
CString as[100][100];
 ret = range.get_Value(covOptional);
  COleSafeArray sa(ret);
  long lNumRows;
  long lNumCols;
    sa.GetUBound(1, &lNumRows);
    sa.GetUBound(2, &lNumCols);
    for(i=0; i<lNumRows; i++)
     {
          for(int j=0; j<lNumCols; j++)
          {

              as[i][j]=range.get_Cells[i][j];

          }
    }




     CString crt;
     crt.Format(_T("%d"),i);// End of Processing logic.
 AfxMessageBox(crt+_T("File Successfully Added"));
  book.Close(COleVariant((short)FALSE), covOptional, covOptional);
    app.Quit();


pls help me

thank you
Posted
Comments
super 14-Jul-15 8:56am    
What happens when you use the above code?
Mahdi Nejadsahebi 10-Jan-17 1:55am    
have a good time dear friend
You know that the excel is a kind of database and you should connect and work with it like a database.
This tutorial can helps you to do this :
https://www.codeproject.com/Tips/691696/Query-Database-From-MFC-Database-Viewer
Mahdi Nejadsahebi 10-Jan-17 1:56am    
you just have to change the "access database" to "excel database" sql connection string.

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