请问我下面的代码运行后为什么会出现这样的错误:
 “Application”是不明确的引用
Application.Run(new Form1()); //这行出错,“Application”是不明确的引用
我把using Excel;去掉又正常了?
请问高手为什么会这样的????=============================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Excel;
namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1()); //这行出错,“Application”是不明确的引用
} private void Form1_Load(object sender, System.EventArgs e)
{
CreateExcel();
} public static void CreateExcel()
{
MessageBox.Show("asd");
for(int i = 0 ; i<1 ; i++)
{
string staFile = "D:\\test\\x";
System.Reflection.Missing miss = System.Reflection.Missing.Value;
Excel.ApplicationClass m_objExcel = new Excel.ApplicationClass();
m_objExcel.Visible = false;
Excel.Workbooks m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
Excel.Workbook m_objBook = (Excel.Workbook)(m_objBooks.Add(miss));
Excel.Worksheet m_objSheet = (Excel.Worksheet)m_objBook.ActiveSheet;
    
//向Excel文件中写入数据
Excel.Range er = m_objSheet.get_Range((object)"A1",System.Reflection.Missing.Value);
er.Value2 = "dfadfa"; m_objBook.SaveAs(staFile+i.ToString()+".xls", miss, miss, miss, miss,miss, Excel.XlSaveAsAccessMode.xlNoChange, miss,miss,miss, miss, miss);
    
m_objBook.Close(false, miss, miss);
m_objBooks.Close();
m_objExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(er);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
GC.Collect();
}
}
}
}

解决方案 »

  1.   

    using Excel;
    是引入命名空间,
    这个命名空间不是系统的,可能是程序定义的,也可能是程序引用的.dll,
    现在找不到这个命名空间,所以运行会出错,而你这段程序没有用到这个命名空间里的方法,所以去掉也没有关系
      

  2.   

    因为Excel命名空间也有Application这个类,而平常所用的Application类是System.Windows.Forms.Application类,所以是不明确引用。将Application.Run(new Form1()); 写成全称就ok了:
    System.Windows.Forms.Application.Run(new Form1());
      

  3.   

    asp.net操作Excel总结 
    http://www.cnblogs.com/renyu732/archive/2005/06/15/174866.html