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

这个问题你见过没
我在form2中写了个cursheet用来存放选定的当前选定的EXCEL数据表,可是在form3中调用老是提示这样一个错误:无法将类型为“Excel.WorksheetClass”的 COM 对象强制转换为接口类型“Excel._Worksheet”。此操作失败的原因是对 IID 为“{000208D8-0000-0000-C000-000000000046}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 不支持此接口 (异常来自 HRESULT:0x80004002 (E_NOINTERFACE))。
那位帮助解决下,谢了。
代码如下:
namespace MyTest
{
public partial class Form2 : Form
{
public Excel.Worksheet[] xSheet=new Excel.Worksheet[0];

public Excel.Worksheet curSheet = new Excel.Worksheet();//被选定的工作表
public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
if (openFileDlg.ShowDialog() == DialogResult.OK)
{
Excel.Application xApp = new Excel.Application();
string strFileName = openFileDlg.FileName;

Excel.Workbook xBook = xApp.Workbooks._Open(@strFileName,
Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
for (int i = 1; i < xBook.Sheets.Count+1; i++)
{
Array.Resize(ref xSheet, xBook.Sheets.Count+1);
xSheet[i-1] = (Excel.Worksheet)xBook.Sheets[i];
string s = xSheet[i-1].Name;
lstboxSheets.Items.Add(s);
}
//Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets[1];
//string s = xSheet.Cells[1, 1].ToString();
//Excel.Range rng = (Excel.Range)xSheet.Cells[1, 1];
//dgvInputScore[0, 0].Value = rng.Value2;
}
}

private void btnOK_Click(object sender, EventArgs e)
{

for (int i = 0; i < xSheet.Length - 1; i++)
{
if (xSheet[i].Name == lstboxSheets.SelectedItem.ToString())
{
curSheet = xSheet[i];

//Excel.Range rng = (Excel.Range)curSheet.Cells[1,1];
//string s = rng.Value2.ToString();
////curSheet.Activate();
break;
}
}

//this.Close();
}

}



namespace MyTest
{
public partial class Form3 : Form
{
Form2 frm2 = new Form2();
public Form3()
{
InitializeComponent();
}

private void Form3_Load(object sender, EventArgs e)
{

DataGridViewComboBoxColumn dgvcbc = new DataGridViewComboBoxColumn();
//dgvcbc.DataSource=;
//int j = frm2.curSheet.UsedRange.Columns.Count;
int j = frm2.curSheet.UsedRange.Cells.Rows.Count;//就是这里提示的
for (int i = 1; i < frm2.curSheet.UsedRange.Columns.Count; i++)
{
Excel.Range rng = (Excel.Range)frm2.curSheet.UsedRange.Cells[1, i];
dgvcbc.Items.Add(rng.Value2.ToString());
}
dgvConfigField.Columns.Insert(0, dgvcbc);
}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}


------解决方案--------------------
没有啊