刚学c#,现在写一个关于EXCEL的例程,可以从指定的EXCEL指定的SHEET中读取信息(存放DATAGRIDVIEW中),但我想把DATAGRIDVIEW修改后的记录重新存会EXCEL.此时就要在cs文件头增加 (using excel;),可总提示
The type or namespace name 'excel' could not be found (are you missing a using directive or an assembly reference?) C:\visual\chapter6\DataImport\DataImport\Form1.cs
我现在有EXCEL.DLL文件了。
  在线等..

解决方案 »

  1.   

    VS2005 里加入引用后,可以用
    using Excel = Microsoft.Office.Interop.Excel;
      

  2.   

    引用COM对象。
    see:http://www.eggheadcafe.com/articles/20021012.asp
      

  3.   

    添加引用 在Com那页 找到Microsoft Excel 12.0 Ojbect Library
      

  4.   

    添加引用
    com
    Microsoft Excel 12.0 Ojbect LibraryLS 正解
      

  5.   

    谢谢各位,我已经成功加载"Microsoft Excel 12.0 Ojbect Library",现在我也已经生成excel.dll了,这个excel.dll放在项目根目录下。现在开始执行下面这段代码,发现问题多多哦
    头文件已经包含“using Excel;”其是有效的:public static int Excel() {
    Application exc = new Application();
    if (exc == null) {
    Console.WriteLine("ERROR: EXCEL couldn"t be started!");
    return 0;
    }exc.set_Visible(0, true); 
    Workbooks workbooks = exc.Workbooks;
    _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0); 
    Sheets sheets = workbook.Worksheets;_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
    if (worksheet == null) {
    Console.WriteLine ("ERROR: worksheet == null");
    }Range range1 = worksheet.get_Range("C1", Missing.Value);
    if (range1 == null) {
    Console.WriteLine ("ERROR: range == null");
    }
    const int nCells = 1;
    Object[] args1 = new Object[1];
    args1[0] = nCells;
    range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null,range1, args1);
    return 100;
    }
    }
    问题如下:
    Error 1 'Application' is an ambiguous reference between 'System.Windows.Forms.Application' and 'Excel.Application' C:\visual\chapter6\DataImport\DataImport\Form1.cs 67 10 DataImportError 2 'Application' is an ambiguous reference between 'System.Windows.Forms.Application' and 'Excel.Application' C:\visual\chapter6\DataImport\DataImport\Form1.cs 67 32 DataImportError 3 The type 'System.Windows.Forms.Application' has no constructors defined C:\visual\chapter6\DataImport\DataImport\Form1.cs 67 28 DataImport
      

  6.   

    如果我把
    Application exc = new Application();
    改为:
     Excel.Application exc = new Excel.Application();
    就不存在上面三个问题,可是又有很多其它问题。
      

  7.   

    我这样改,编译时就没有问题了:
     private void ChangeExcel()
            {
             Excel.Application exc = new Excel.Application();
             if (exc == null)
             {
               Console.WriteLine("ERROR: EXCEL couldn't be started!");
             }
             exc.Visible = true;
             Workbooks workbooks = exc.Workbooks;
             _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
             Sheets sheets = workbook.Worksheets;
             _Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
             if (worksheet == null)
             {
                 Console.WriteLine("ERROR: worksheet == null");
             }         Range range1 = worksheet.get_Range("C1", Missing.Value);
             if (range1 == null)
             {
                 Console.WriteLine("ERROR: range == null");
             }
             const int nCells = 1;
             Object[] args1 = new Object[1];
             args1[0] = nCells;
             range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range1, args1);
            }