public  void ConvertDataToExcel<T>(T[][] data, string xlsSaveFileName)
        {
            //在做这些前,将Excel添加到引用中来!!!
            Excel.Application excel = new Excel.Application();
            if (excel == null)
            {
                MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                Excel.Workbook xlBook = excel.Workbooks.Add(true);
                Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];
                int rows = data.GetLength(0);//行
                //int cols = data.GetLength(1);//列                //导入数据
                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < data[i].Length; j++)
                    {
                        if (data[i][j] != null)
                        {
                            excel.Cells[i + 1, j + 1] = data[i][j].ToString().Trim();
                        }
                    }
                }
                try
                {
                    xlBook.Saved = true;
                    xlBook.SaveCopyAs(xlsSaveFileName);
                  
                }
                catch(Exception error)
                {
                    MessageBox.Show(error.Message, "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                finally
                {
                    excel.Quit();
                    excel = null;
                    GC.Collect();
                }
            }
        }
这个方法的目的是新建一个excel文件,并将传递进来的二维数组写入到excel里,在调用这个方法时我写的语句为
if (z >= z_caster) ConvertDataToExcel(T[i,j] , whole); 
报错为错误 1 无法从用法中推导出方法“my_first_windows.main_struct.ConvertDataToExcel<T>(T[][], string)”的类型实参。请尝试显式指定类型实参。
请问我该如何调用上面的方法, 该传递什么形式的参数。请指教。最好有源代码。
谢谢大家了   

解决方案 »

  1.   

    不是T[i,j],参进去的应该是一个数组对象class ArrayClass2D
    {
        static void PrintArray(int[,] arr)
        {
            // Display the array elements:
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    System.Console.WriteLine("Element({0},{1})={2}", i, j, arr[i, j]);
                }
            }
        }
        static void Main()
        {
            // Pass the array as a parameter:
            PrintArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } });
        }
    }