read excel cell  
Author Message
onkel elton





PostPosted: Visual C++ Language, read excel cell Top

hi there,

i try to read a excel cell value. and i don't know how.
i can open the excel file but after that.... maybe you can help me. i try to read the value and show it in an edit field.

i am using visual studio c++ 6.0 ; excel 2003
i included all excel.h classes.

here is what i tried so far.

greetings
daniel


void CAutoProjektDlg::OnGetvalue()
{
// Commonly used OLE variants.
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);


_Application app;
Workbooks books;
_Workbook book;

Worksheets sheets;
_Worksheet sheet;
Range range;
Font font;
Range cols;

COleVariant Data;

int Value=0;

long cell;
long *pcell = &cell;

// Start Excel and get Application object.

if(!app.CreateDispatch("Excel.Application"))

{
AfxMessageBox("Couldn't start Excel and get Application object.");
return;
}

//Get a new workbook.
books = app.GetWorkbooks();

//open a.xls file
book = books.Open("E:\\Documents and Settings\\221149\\My Documents\\Visual Studio 6\\a.xls",
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional, covOptional);

sheets= book.GetSheets();

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

range = sheet.GetRange(COleVariant("A1"),COleVariant("A1"));

// range.GetValue(Data);

// COleSafeArray saRet(range.GetValue(covOptional));
// COleVariant vData;
// saRet.GetElement(pcell, Data);

/*
When debugging applications involving COleVariants, it's helpful to have a way to convert
the variant to a displayable string. One way to do this is to use the CString::Format method.
For example:

COleVariant vVal(1000, VT_I4);
CString strVal;
//
strVal.Format("Value = %ld\n", vVal.lVal);
*/
CString strVal;
strVal.Format ("Value = %ld\n", Data.lVal);

//show and update edit field
m_Edit1= strVal;
UpdateData(false);


}





Visual C++12  
 
 
Adamus Turner





PostPosted: Visual C++ Language, read excel cell Top

You access the cells in Excel through Sheet1.Range("A1")

Adamus



 
 
onkel elton





PostPosted: Visual C++ Language, read excel cell Top

but nothing is happening when i type Sheet1.Range("A1").
do you know in which class i can find it



 
 
Adamus Turner





PostPosted: Visual C++ Language, read excel cell Top

In your code, and I'm guessing here, but Sheets("Sheet1").Range("A1").Value

Adamus



 
 
Sahir Shah





PostPosted: Visual C++ Language, read excel cell Top

Reading a cell value can be done this way :

void CExcelAutomationDlg::OnButton1()
{
   if(!AfxOleInit()) {
       AfxMessageBox("OLE init failed ");
        return;
   }  
    COleVariant   covOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
   _Application xlApp;
    Workbooks xlBooks;
   _Workbook xlBook;
    Worksheets xlSheets;
    _Worksheet xlSheet;
    Range xlRange;
    VARIANT v = {0};
    if(!xlApp.CreateDispatch("Excel.Application"))   {
     AfxMessageBox("Failed.");
      return;
    }
     xlBooks = xlApp.GetWorkbooks();
     xlBook =  xlBooks.Open("c:\\Test\\Book1.xls" ,
                       covOpt , covOpt , covOpt ,
                       covOpt , covOpt , covOpt ,
                       covOpt , covOpt , covOpt ,
                       covOpt , covOpt , covOpt ,
                       covOpt,  covOpt);
     xlApp.SetVisible(true);
     xlSheets = xlBook.GetWorksheets();
     xlSheet = xlSheets.GetItem(COleVariant((short)1));
     xlRange = xlSheet.GetRange( COleVariant("A1"), COleVariant("A1"));
     v = xlRange.GetValue2();
     CString str =  v.bstrVal;
     Edit1.SetWindowText(str);  
    // or if you prefer it this way 
     m_Edit2 =  v.bstrVal;
}

BTW,  VC6 related  questions are off topic to this forum. In  future please use the newsgroups. Thank you for your cooperation. 

 Regards

  Sahir Shah


 
 
Brian Kramer





PostPosted: Visual C++ Language, read excel cell Top

Also, Excel automation is completely covered in the newsgroup area. Please restrict usage of this forum to C++ language issues.

Thanks.