工具 vs2003.net
     office2003
如何在c#中直接调用比如 sum,round,len 等函数
注:excel中的和System.Math  中的函数很多名称相同操作结果却不同的,需要直接调用excel中的
-----------------
用tlbimp  进行的转换  
将excel.exe 文件转为excel.dll文件 然后添加到了com组件里
然后具体的操作该怎么写?
using Excel(.后没有出现提示);

解决方案 »

  1.   

    首先的一步就是使用Tlbimp.exe这个工具将Excel9.0的对象库文件Excel8.olb转换成为dll,这样才能做为.Net平台Assembly来使用:)操作如下:TlbImp Excel9.olb Excel.dll 
    只要有了这个Excel.dll,现在我们就能使用Excel的各种操作函数了。 
    下面就让我们具体看看C#是如何使用这些东东吧。 
    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;
             }
    }
      

  2.   

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

  3.   

    谢谢楼上的朋友,
     但我要的不是这些操作, 我想直接调用excel的内部函数呀
    比如 调用Sysstem.Math  下的函数一样
      

  4.   

    没这个接口,你可以先把数据写到cell里面,在写一个公式进去,最后取公式的结果。
      

  5.   

    using Excel;
    private static Excel.ApplicationClass  ex=new Excel.ApplicationClass();
    int[] ax=new int[]{0,2,3,4,5,6};
    int[] ay=new int[]{1,2,3,4,5,6};
    object result=ex.WorksheetFunction.Intercept(ax,ay);
    //必须为object类   然后再转换类型
    //其中Intercept就是excel中的一个直线拟合回归线性函数。
      

  6.   

    lwjvince(vince)
    ---------
    你的方法在转换的时候有问题比如  object类型 传值怎么做到啊
    需要装箱,拆箱。。好复杂啊。
    xiaomatian(趴趴熊◎%#……※×) 
    ---------------
    麻烦讲详细点 谢谢。
      

  7.   

    在使用WorksheetFunction调用excel内部函数的时候它出现的是最大容量数  比如SUM 函数最大只能处理30个数值,而在调用的时候仅仅只出现了最大调用数。
    private double AVEDEV(string[] number)
    {
    int i;
    ArrayList arrList=new ArrayList ();
    IsNumberControls iNor=new IsNumberControls ();
    for(i=0;i<number.Length ;i++)
    {
    if(iNor.IsNumber (number[i]==true))
    arrList.Add(Convert.ToDouble (number[i]));
    }
    double []arr_new=(double[])arrList.ToArray(typeof(double));
    //这下一行:AveDev中必须处理30个数值才能通过。不能使用数组。  该怎么样才能解决这个问题?
    object result=PubComControls.ex .WorksheetFunction .AveDev (arr_new);
    return(double.Parse (result.ToString ()));
    }