日期:2014-05-18  浏览次数:20896 次

想显示excel中的某一个数据,却显示system._comobject
这是我的程序,从主程序中贴出来有关的一段
private void button1_Click(object sender, EventArgs e)
  {
  try
  {
  string path;
  path = @"d:\succeccful.xls";
  Excel.Application xlapp;
  Excel.Workbook xlbook;
  Excel.Worksheet xlsheet;
  xlapp = new Excel.ApplicationClass();
  xlbook = xlapp.Workbooks._Open(path,Missing.Value,Missing.Value,Missing.Value,Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
  xlapp.DisplayAlerts = false;
  xlapp.Cells[2, 2] = "123";
  MessageBox.Show((xlapp.Cells[2, 2]).ToString ());
  xlbook.SaveAs(path, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
  xlapp.Quit();
  xlapp = null;
  }
  catch (Exception ex) { MessageBox.Show(ex.Message); }
  }
执行后显示的是system._comobject
可是我前面不是定义为123吗??求解???

------解决方案--------------------
我也想知道,帮你项先,期待答案!!!!
------解决方案--------------------
需要强制转换一下,如
C# code

((Excel.Range)xlapp.Cells[2, 2]).Text    //(只读)
((Excel.Range)xlapp.Cells[2, 2]).Value   //(读写)
((Excel.Range)xlapp.Cells[2, 2]).Value2  //(读写)