Visual Studio forcing me to use full namespace even with reference added and "using" added

时间:2015-12-10 02:07:37

标签: c# excel office-automation

Im using VS2013 community copying and I have downloaded the excel library and it still forces me to write the whole namespace each time saying it cant distinguish between winforms.application() and excel.application. It also says that the "missing" in "missing.value" does not have context.

This code is copied almost entirely from the MS website.

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 { }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

这是因为您使用的是Application类,它存在于两个名称空间中:System.Windows.FormsMicrosoft.Office.Interop.Excel。完全限定名称允许确定必须使用哪一个。

相关问题