直接了当,【获取Excel中2行数据,调用公式CORREL 】

解决方案 »

  1.   

    需要先声明一个excel实例的
    比如:Microsoft.Office.Interop.Excel.ApplicationClass excel=
    new Microsoft.Office.Interop.Excel.ApplicationClass();
    -然后就可以调用函数了
    bool b=excel.WorksheetFunction.IsNumber("ABC");
      

  2.   

    bool b=excel.WorksheetFunction.IsNumber("ABC");   这句话没看懂, “ABC”是什么意思?
      

  3.   

    bool b=excel.WorksheetFunction.IsNumber("ABC");   这句话没看懂, “ABC”是什么意思?我需要2组值,也就是需要2行, 2行应该是需要2个参数。
      

  4.   

    bool b=excel.WorksheetFunction.IsNumber("ABC");   这句话没看懂, “ABC”是什么意思?我需要2组值,也就是需要2行, 2行应该是需要2个参数。就是给个示例,调用的是IsNumber这个函数,“ABC”是输入参数
    如果需要输入行,输入Range对象就可以。
      

  5.   

    和Excel一样复制DataGridView数据
    //添加单元格的内容
     private void button1_Click(object sender, EventArgs e)
            {
                str = CopyDataGridView(dataGridView1);
                AddDataGridView(dataGridView2, str, Bool_Blank, Bool_All);
            }
      // 通过剪贴板复制DataGridView控件中所选中的内容.
            // <param DGView="DataGridView">DataGridView类</param>
                 public string CopyDataGridView(DataGridView DGView)
            {
                string tem_str = "";
                if (DGView.GetCellCount(DataGridViewElementStates.Selected) > 0)
                {
                    try
                    {
                        //将数据添加到剪贴板中
                        Clipboard.SetDataObject(DGView.GetClipboardContent());
                        //从剪贴板中获取信息
                        tem_str = Clipboard.GetText();
                    }
                    catch (System.Runtime.InteropServices.ExternalException)
                    {
                        return "";
                    }
                }
                return tem_str;
            }
    // 将字符串按指定的格式添加到DataGridView控件中(如果有被选中的单元格,则修改单元格中的内容)
      public void AddDataGridView(DataGridView DGView, string s, bool Blank, bool All)
            {
                string tem_str = s;
                int tem_n = 0;
                int RowCount = 0;//行数
                int CellCount = 0;//列数
                bool tem_bool = true;
                string tem_s = "";
                if (s.IndexOf("\r\n") != -1)//如果替换的为多行
                    while (tem_bool)//获取单元格的行数和列数
                    {
                        tem_s = "";
                        if (tem_str.IndexOf("\r\n") != -1)//如果获取的不是最后一行
                        {
                            tem_s = tem_str.Substring(0, tem_str.IndexOf("\r\n") + 2);//获取当前行中的数据
                            //获取当前行中能被识别的数据
                            tem_str = tem_str.Substring(tem_str.IndexOf("\r\n") + 2, tem_str.Length - tem_str.IndexOf("\r\n") - 2);
                            tem_n = 1;
                            while (tem_s.IndexOf("\t") > -1)//遍历当前行中的空格
                            {
                                //去除已读取的空格
                                tem_s = tem_s.Substring(tem_s.IndexOf("\t") + 1, tem_s.Length - tem_s.IndexOf("\t") - 1);
                                tem_n += 1;//获取列数
                            }
                            if (tem_n > CellCount)//判断当前列数是否为最大列数
                                CellCount = tem_n;//获取最大的列数
                        }
                        else//如果读取的是最后一行
                        {
                            tem_n = 1;
                            while (tem_s.IndexOf("\t") > -1)
                            {
                                tem_s = tem_s.Substring(tem_s.IndexOf("\t") + 1, tem_s.Length - tem_s.IndexOf("\t") - 1);
                                tem_n += 1;
                            }
                            if (tem_n > CellCount)
                                CellCount = tem_n;
                            tem_bool = false;//遍历结束
                        }
                        ++RowCount;//读取行数
                    }
                else//如果读取的为单行数据
                {
                    tem_n = 1;
                    tem_s = s;
                    while (tem_s.IndexOf("\t") > -1)
                    {
                        tem_s = tem_s.Substring(tem_s.IndexOf("\t") + 1, tem_s.Length - tem_s.IndexOf("\t") - 1);
                        tem_n += 1;
                    }
                    if (tem_n > CellCount)
                        CellCount = tem_n;
                    ++RowCount;//读取行数
                }
                string[,] Strarr = new string[RowCount, CellCount];//定义一个数组,用于记录复制的单元格信息            tem_str = s;
                tem_n = 0;
                //将单元格信息添加到数组中
                for (int i = 0; i < RowCount; i++)//遍历单元格的行
                {
                    for (int j = 0; j < CellCount; j++)//遍历单元格的列
                    {
                        tem_s = "";
                        if (tem_str.IndexOf("\r\n") != -1)//如果不是最后一行
                        {
                            if (tem_str.IndexOf("\t") <= -1)//设置读取数据的位置
                                tem_n = tem_str.IndexOf("\r");//最后一个数据的位置
                            else
                                tem_n = tem_str.IndexOf("\t");//不是最后一个数据的位置
                            tem_s = tem_str.Substring(0, tem_str.IndexOf("\r\n") + 2);//读取单元格数据
                        }
                        else//如果是最后一行
                        {
                            if (tem_str.IndexOf("\t") <= -1)//设置读取数据的位置
                                tem_n = tem_str.Length;//最后一个数据的位置
                            else
                                tem_n = tem_str.IndexOf("\t");//不是最后一个数据的位置
                            tem_s = tem_str;//读取单元格数据
                        }
                        if (tem_s.Length > 0)//如果当前行有数据
                        {
                            if (tem_s.Substring(0, 1) == "\t")//如果第一个字符为空
                                Strarr[i, j] = "";//向数组中添加一个空记录
                            else
                            {
                                Strarr[i, j] = tem_s.Substring(0, tem_n);//向数组中添加数据
                            }
                        }
                        else
                            Strarr[i, j] = "";//向数组中添加空记录
                        if (tem_s.Length > tem_n)//如果记录没有读取完
                            tem_str = tem_s.Substring(tem_n + 1, tem_s.Length - tem_n - 1);//获取没有读取的记录
                    }
                    if (s.IndexOf("\r\n") > -1)//如果不是最后一行数据
                    {
                        s = s.Substring(s.IndexOf("\r\n") + 2, s.Length - s.IndexOf("\r\n") - 2);//读取下一行数据
                        tem_str = s;
                    }
                }
                if (All)//如果要全部替换
                    DGView.Rows.Clear();//清空DataGridView控件
                if (DGView.SelectedRows.Count == 0 && DGView.SelectedCells.Count == 0)//如果DataGridView中没有数据
                {
                    DGView.ColumnCount = CellCount;//设置列数
                    string[] stra = new string[CellCount];//定义一个一维数组
                    //向DataGridView中添加行数据
                    for (int i = 0; i < RowCount; i++)//读取行
                    {
                        for (int j = 0; j < CellCount; j++)//读取列
                        {
                            if (Strarr[i, j] == "")//如果当前单元格为空
                            {
                                if (Blank)//如果用*号替换
                                    stra[j] = "*";//在空单元格中添加*号
                                else
                                    stra[j] = "";//以空格显示空单元格
                            }
                            else
                            {
                                stra[j] = Strarr[i, j];//记录当前行中的信息
                            }
                        }
                        DGView.Rows.Add(stra);//将行中的信息添加到DataGridView控件                }
                    DGView.AutoResizeColumns();//向DataGridView控件添加所有的单元格信息
                    DGView.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;//将所选择的单元格复制到剪贴板中
                }
                else//如果DataGridView中有数据
                {
                    int maxrow = 0;//记录DataGridView控件中最小单元格的行数
                    int maxcell = 0;//记录DataGridView控件中最小单元格的列数
                    for (int i = 0; i < DGView.SelectedCells.Count; i++)//获取选中单元格中最大单元格的行数和列数
                    {
                        if (DGView.SelectedCells[i].RowIndex > maxrow)//如果单元格的行数大于当前指定的行数
                            maxrow = DGView.SelectedCells[i].RowIndex;//记录当前单元格的行数
                        if (DGView.SelectedCells[i].ColumnIndex > maxcell)//如果单元格的列数大于当前指定的列数
                            maxcell = DGView.SelectedCells[i].ColumnIndex;//记录当前单元格的列数
                    }
                    int minrow = maxrow;//记录DataGridView控件中最大单元格的行数
                    int mincell = maxcell;//记录DataGridView控件中最大单元格的列数
                    for (int i = 0; i < DGView.SelectedCells.Count; i++)//获取选中单元格中最小单元格的行数和列数
                    {
                        if (DGView.SelectedCells[i].RowIndex < minrow)//如果单元格的行数小于当前指定的行数
                            minrow = DGView.SelectedCells[i].RowIndex;//记录当前单元格的行数
                        if (DGView.SelectedCells[i].ColumnIndex < mincell)//如果单元格的列数小于当前指定的列数
                            mincell = DGView.SelectedCells[i].ColumnIndex;//记录当前单元格的列数
                    }
                    //向DataGridView控件中添加选中单元格中最小单元格与最大单元格中的所有单元格
                    for (int i = 0; i < maxrow - (minrow - 1); i++)//遍历行数
                    {
                        if (i >= RowCount)//如果超出要添加的行数
                            break;//退出循环
                        for (int j = 0; j < maxcell - (mincell - 1); j++)//遍历列数
                        {
                            if (j >= CellCount)//如果超出要添加的列数
                                break;//退出循环
                            if (Strarr[i, j]=="")//如果添加的单元格为空
                            {
                                if (Blank)//如果用*号替换空格
                                    DGView.Rows[i + minrow].Cells[j + mincell].Value = "*";//用*号替换空单元格
                            }
                            else
                                DGView.Rows[i + minrow].Cells[j + mincell].Value = Strarr[i, j];//设置当前单元格的值
                        }
                    }
                }
            }
      

  6.   

    你在外部程序中是无法调用Excel自带函数的,连调用SUM或=都做不到
    你非要这么用,有2个办法
    1.自己实现这个方法,并在程序里直接调用
    2.先以字符串形式将公式写回EXCEL,更新,然后重新读出来里面的数值
      

  7.   

    另附上 解决方案:我的初衷只是想调用excel的函数,8楼的说法否定,应该是可直接调用内部函数。
      

  8.   

    double linestRet0 = excelApp.WorksheetFunction.Correl(ws.get_Range("C6", "G6"), ws.get_Range("C7", "G7"));
                excelApp.Cells[105, 3] = linestRet0.ToString();
    这段代码解决我的问题。
      

  9.   

    excelApp是什么玩意,第三方操作excel的类库?
    它不过是封装了excel的函数,然后你直接调用,不用你自己代码实现了而已
    并不是真的去调用office里的函数,更不是什么"excel文件里的函数"
    excel不过是个文件,里面的函数是需要office解析执行的