Visual Studio迫使我使用完整的命名空间,即使添加了引用并添加了“using”

我使用VS2013社区复制和我已经下载的Excel库,它仍然强迫我写整个命名空间,每次都说不能区分winforms.application()和excel.application。 它还说,“missing.value”中的“失踪”没有上下文。

此代码几乎完全从MS网站复制。

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.Office.Interop.Excel; namespace ExcelAuto { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //Initiate Excel Objects //XlMSApplication oXL; Microsoft.Office.Interop.Excel.Application oXL; Workbook oWB; Worksheet oSheet; Range oRng; try { //Start Excel and get Application Object. oXL = new Microsoft.Office.Interop.Excel.Application(); oXL.Visible = true; //Get a new workbook oWB= (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(Missing.Value)); } catch { } } } } 

发生这种情况是因为您正在使用两个名称空间中存在的Application类: System.Windows.FormsMicrosoft.Office.Interop.Excel 。 完全合格的名称允许确定使用哪一个。