Click here to Skip to main content
11,495,534 members (66,822 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++/CLI C# WinForm
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 12-Nov-12 0:44am
Edited 12-Nov-12 20:50pm
v3
Comments
Member 9562996 at 12-Nov-12 5:58am
   
What is that power sumbol near string?
christmars at 12-Nov-12 6:09am
   
Sorry I don't get what you mean, "sumbol"?
Richard MacCutchan at 12-Nov-12 7:46am
   
It's the C++/CLI reference symbol.

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
christmars at 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 at 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 at 13-Nov-12 5:30am
   
I have a better and easy link for this class:
http://www.c-sharpcorner.com/UploadFile/thiagu304/ExcelAutomation01052007080910AM/ExcelAutomation.aspx
qPCR4vir at 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(safe_cast(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 at 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
0 Dnyaneshwar@Pune 692
1 CHill60 318
2 Sascha Lefèvre 265
3 Nakul Chaudhari 236
4 Sergey Alexandrovich Kryukov 230
0 Sergey Alexandrovich Kryukov 10,322
1 OriginalGriff 8,821
2 Sascha Lefèvre 3,889
3 Maciej Los 3,422
4 Richard Deeming 2,560


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 13 Nov 2012
Copyright © CodeProject, 1999-2015
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