Click here to Skip to main content
12,943,528 members (53,918 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello friends,

I tried to use the namespace Microsoft.Interop.Office.Excel to read excel cell values to my windows forms application. But I got confusion.

My code:

using namespace Microsoft::Office::Interop::Excel;
#define Excel Microsoft::Office::Interop::Excel
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  Excel::Application^ xlApp =gcnew Excel::ApplicationClass();
  Excel::Workbook^ mybook = xlApp->Workbooks->Open("C:\\Liste.xlsx",Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing,Type::Missing, Type::Missing);
  xlApp->Visible = false;
  Excel::Worksheet^ mysheet= safe_cast<Worksheet^>(xlApp->ActiveSheet);
  mysheet = (Excel::Worksheet^)mybook->Worksheets->Item[1]; 
  String^ tmp=mysheet->Cells[2, 3]->ToString();

It has no error. But what I get for the "tmp" is always a "System._ComObject".
Not what is expected, also the value of the row 2, column 3 from the opened xlsx-File.

I couldn't unterstand it. Could you help?
Posted 11-Nov-12 23:44pm
Updated 12-Nov-12 19:50pm
Member 9562996 12-Nov-12 5:58am
What is that power sumbol near string?
christmars 12-Nov-12 6:09am
Sorry I don't get what you mean, "sumbol"?
Richard MacCutchan 12-Nov-12 7:46am
It's the C++/CLI reference symbol.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Take a look at Accessing Excel Spreadsheets via C++[^] here on CodeProject; it may help you.
christmars 12-Nov-12 8:10am
Thank you at first. I've read this article already. Maybe I shoule read it one more time and carefully? I'll see if it covers my prolem.
christmars 13-Nov-12 5:21am
Hi, after reading and testing, I finally found that this article doesn't solve the problem. The using of namespace "Microsoft::Office::Interop::Excel" under c++/cli is such bad, that some declared methods of msdn don't go to work properlly...
christmars 13-Nov-12 5:30am
I have a better and easy link for this class:
qPCR4vir 14-Nov-12 14:25pm
uff, yes, Microsoft::Office::Interop::Excel" under c++/cli have no documentation. I have this problem, and I dont know what to do. Evrething return just Object^. The best I got is :
Excel::Range^ cell= safe_cast<Range^>(safe_cast<Worksheet^>(mysheet)->Cells[2,3]);
please, could you try?:
cell->Value2->ToString() ;
cell->Value[0]->ToString() ;
the problem is that Value return an Object^ too. That have to be a "Variant" and I can not find how to manipulate it to take the string (it have to have a field .vt set to VT_BSTR, and the field .bstrVal have to containd a pointer to wchar_t for the BSTR...) I dont see how to cast Object^ to Variant, and then take the pointer .bstrVal for the constructor of String. (or a library function to do that)
christmars 14-Nov-12 14:42pm
hey! It's not that complicated. I suggest you to do this:
String^ a = (String^) Cells->Value2;

Also the part is (String^), as a "enforced" type conversion. When the cell value has a double or int type, use this>> Convert::ToString(...->Cells->Value2).
It'll work. If you still have problem, call back! ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,643
CHill60 2,970
Maciej Los 2,348
Jochen Arndt 1,900
ppolymorphe 1,765

Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 13 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100