在C#中处理COM时出现“Interop type cannot be embedded”错误
你好!我是C#新手,尝试通过COM对象连接到Excel。我使用的是Visual Studio 2010,.NET Framework 4.0项目。
我通过References中的COM选项卡添加了对Microsoft.Office.Interop.Excel库的引用。编写了简单的代码:
```csharp
using Excel = Microsoft.Office.Interop.Excel;
class Program
{
static void Main()
{
var excelApp = new Excel.Application();
excelApp.Visible = true;
excelApp.Quit();
}
}
```
编译时出现错误:
“Interop type 'Microsoft.Office.Interop.Excel.ApplicationClass' cannot be embedded. Use the applicable interface instead.”
我搜索过,但没搞明白。为什么会出现这个错误?“不能嵌入”是什么意思?如何修复?我只需要打开Excel并将数据写入单元格。请帮帮我,我已经折腾两天了。
提前感谢!
<<>>
错误原因
该错误是由于C# 4.0的新功能——类型嵌入引起的。