Click here to Skip to main content
15,918,243 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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
Updated 12-Nov-12 19:50pm
v3
Comments
Red Chocolate 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

Take a look at Accessing Excel Spreadsheets via C++[^] here on CodeProject; it may help you.
 
Share this answer
 
Comments
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:
http://www.c-sharpcorner.com/UploadFile/thiagu304/ExcelAutomation01052007080910AM/ExcelAutomation.aspx
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900