简体   繁体   English

Excel Interop 阅读工作表内容

[英]Excel Interop reading worksheet content

I know this is a very basic topic, but I've done research and yet haven't found a solution to my problem, which is getting the contents of a number of cells in a worksheet.我知道这是一个非常基本的主题,但我已经进行了研究,但还没有找到解决我的问题的方法,即获取工作表中多个单元格的内容。 This is what I have:这就是我所拥有的:

Excel::Application^ ExList = gcnew Excel::ApplicationClass();
ExList->DisplayAlerts = false;
ExList->Visible = false;
Workbook^  Wbook1  = ExList->Workbooks->Open(Glo::m_archive01, Type::Missing, false, Type::Missing, Type::Missing, Type::Missing,     Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing);
Worksheet^ Wsheet1 = safe_cast<Worksheet^>(ExList->ActiveSheet);
String^ m_new_section;//the variable to be displayed later on by means of MessageBox::Show(m_new_section);.

This is what I've tried and the compiler errors I've gotten:这是我尝试过的以及我得到的编译器错误:

m_new_section = Cells[5, 2]->Value;
error C2065: 'Cells' : undeclared identifier
error C2227: left of '->Value' must point to class/struct/union/generic type

m_new_section = ExList->Cells[5, 2]
error C2440: '=' : cannot convert from 'System::Object ^' to 'System::String ^'

m_new_section = ExList->Cells[5, 2]->Value;
error C2039: 'Value' : is not a member of 'System::Object'

m_new_section = Wsheet1->Cells[5, 2];
error C2440: '=' : cannot convert from 'System::Object ^' to 'System::String ^'

m_new_section = Wsheet1->Cells[5, 2]->Value;
error C2039: 'Value' : is not a member of 'System::Object'

m_new_section = ExList->Cells[5, 2]->Value.ToString();
error C2039: 'Value' : is not a member of 'System::Object'
error C2228: left of '.ToString' must have class/struct/union

This one actually "worked"这个实际上“有效”

m_new_section = Wsheet1->Cells[5, 2]->ToString();

but all MessageBox showed for all the cells read was this string: "System._ComObject".但是为读取的所有单元格显示的所有 MessageBox 都是这个字符串:“System._ComObject”。

What am I missing?我错过了什么? A reference?参考? Like I said before, I'm new at this and coding in c++/cli.就像我之前说的,我是新来的,在 c++/cli 中编码。 My only references are examples I've found written in c#.我唯一的参考资料是我找到的用 c# 编写的示例。

At this point, I'd appreciate any heLp.在这一点上,我很感激任何帮助。 THANK YOU!谢谢你!

"Funny how time teaches us patience. The older we get, the greater our capacity for waiting" - Elizabeth Taylor. “有趣的是时间教会了我们耐心。我们年纪越大,等待的能力就越大”——伊丽莎白·泰勒。

String^ m_new_section;
Excel::Range^ er1 = Wsheet1->Range["E2:E2", Type::Missing];
m_new_section = er1->Text->ToString();

That's all that needed to be done.这就是所有需要做的事情。 Again, THANKS FOR YOUR TIME!再次感谢您的时间! Thanks, Chris!谢谢,克里斯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM