【问题标题】:Excel Interop reading worksheet contentExcel 互操作读取工作表内容
【发布时间】:2015-09-12 14:26:13
【问题描述】:

我知道这是一个非常基本的主题,但我已经进行了研究,但还没有找到解决我的问题的方法,即获取工作表中多个单元格的内容。这就是我所拥有的:

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);.

这是我尝试过的以及我得到的编译器错误:

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

这个确实“有效”

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

但所有读取的单元格显示的所有 MessageBox 都是这个字符串:“System._ComObject”。

我错过了什么?参考?就像我之前说的,我是这方面的新手,并且使用 c++/cli 进行编码。我唯一的参考资料是我发现的用 c# 编写的示例。

在这一点上,我会很感激任何帮助。谢谢你!

【问题讨论】:

  • 那么,Stokes 先生,您输入了什么,先生?

标签: c# excel c++-cli interop cells


【解决方案1】:

“有趣的是,时间教会了我们耐心。我们年纪越大,等待的能力就越大”——伊丽莎白·泰勒。

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

这就是所有需要做的。再次感谢您的宝贵时间!谢谢,克里斯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多