2005中怎么没有 Excel.Application 对象啊?
求一份 2005中的Excel操作方法 例子,什么样的都可以,随便,看看大概怎么用。

解决方案 »

  1.   

    首先的一步就是使用Tlbimp.exe这个工具将Excel9.0的对象库文件Excel8.olb转换成为dll,这样才能做为.Net平台Assembly来使用:)操作如下:TlbImp Excel9.olb Excel.dll 
    只要有了这个Excel.dll,现在我们就能使用Excel的各种操作函数了。 1. 创建一个新Excel的Application: Application exc = new Application();
    if (exc == null) {
    Console.WriteLine("ERROR: EXCEL couldn't be started");
    return 0;
    }
    2. 让这个工程可见: exc.set_Visible(0, true); 
    3. 获取WorkBooks集合: Workbooks workbooks = exc.Workbooks; 
    4. 加入新的WorkBook: _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0); 
    5. 获取WorkSheets集合: _Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
    if (worksheet == null) {
    Console.WriteLine ("ERROR in worksheet == null");
    }
    6. 给单元格设置变量: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);
    例程: using System;
    using System.Reflection; 
    using System.Runtime.InteropServices; 
    using Excel;class Excel {
             public static int Main() {
                     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;
             }
    }
    现在我们来看看如何使用数组,他有些类似于设置单元格。仅仅需要的改变只是args2[0] = array2; const int nCell = 5;
    Range range2 = worksheet.get_Range("A1", "E1");
                     int[] array2 = new int [nCell];
                     for (int i=0; i < array2.GetLength(0); i++) {
                              array2[i] = i+1;
                     }
                     Object[] args2 = new Object[1];
                     args2[0] = array2;
                     range2.GetType().InvokeMember("Value", BindingFlags.SetProperty,       null, range2, args2);
      

  2.   

    这个也行private DataSet ReadExcel(string strFileName)
    {
    string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source =" +strFileName + ";Extended Properties = Excel 8.0";
    OleDbConnection oleConnection = new OleDbConnection(strConnection);
    try
    {
    string strCommondText = " SELECT * FROM [Sheet1$] ";
    oleConnection.Open();
    DataSet dsRead = new DataSet();
    OleDbDataAdapter oleAdper = new OleDbDataAdapter(strCommondText,oleConnection);
    oleAdper.Fill(dsRead,"Pantent");
    return dsRead;
    }
    catch(System.Exception)
    {
    throw new ApplicationException("读取数据源文件时出错");
    }
    finally
    {
    oleConnection.Close();
    }
    }
      

  3.   

    首先的一步就是使用Tlbimp.exe这个工具将Excel9.0的对象库文件Excel8.olb转换成为dll,这样才能做为.Net平台Assembly来使用:)操作如下:TlbImp Excel9.olb Excel.dll 
    只要有了这个Excel.dll,现在我们就能使用Excel的各种操作函数了。 
    -----------------------------------------------------------------------------------
    首先,感谢你回答我的帖子,我是用2003操作的Excel,直接添加Excel,office引用组件,就可以找到Excel对象, 
    1、先建立 Excel.Application ,在操作。但是现在我用2005,也是先引用Excel,office组件, 
    也能找到Excel对象
    但是找不到 Excel.?? (这里没有Application了。) 没有Application对象,怎么操作Excel啊。请大家指点一下!!!!!!!!!! 再次感谢
      

  4.   

    首先的一步就是使用Tlbimp.exe这个工具将Excel9.0的对象库文件Excel8.olb转换成为dll,这样才能做为.Net平台Assembly来使用:)操作如下:TlbImp Excel9.olb Excel.dll 
    只要有了这个Excel.dll,现在我们就能使用Excel的各种操作函数了
    -----------------------------------------------------------------
    还有,上面这几句不太明白。
      

  5.   

    首先把COM组件"EXCEL9.OLB"拷贝到C盘的根目录下,然后输入下列命令:
    tlbimp excel9.olb   这样在C盘的根目录下面就产生了三个DLL文件:"Excel.dll"、"Office.dll"、"VBIDE.dll"。在产生了上面的三个文件后,这种转换就成功完成了。在程序里参照引入Excel.dll