Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用excel实现可数收益率函数_C#_.net_Winforms_Ienumerable - Fatal编程技术网

C# 用excel实现可数收益率函数

C# 用excel实现可数收益率函数,c#,.net,winforms,ienumerable,C#,.net,Winforms,Ienumerable,我从C#开始,也从互操作的使用开始 我的目标是创建一个可以返回Excel值的windows应用程序 我的问题是,在进行循环时,我只得到了第一个值,因为“return”关闭了循环 为了解决这个问题,我在函数中使用了“IEnumerable”,同时保留了“string”类型 函数返回sdexpformsap1.Excel+d_u6 我不明白为什么报税表后面跟着“d_uu6”的是班级的名字 我试图在这里找到一些有趣的东西: 我的活动功能 private void button1_Click(obje

我从C#开始,也从互操作的使用开始

我的目标是创建一个可以返回Excel值的windows应用程序

我的问题是,在进行循环时,我只得到了第一个值,因为“return”关闭了循环

为了解决这个问题,我在函数中使用了“IEnumerable”,同时保留了“string”类型

函数返回sdexpformsap1.Excel+d_u6

我不明白为什么报税表后面跟着“d_uu6”的是班级的名字

我试图在这里找到一些有趣的东西:

我的活动功能

private void button1_Click(object sender, EventArgs e)
    {
        openFileDialog1.ShowDialog();
        string filename = openFileDialog1.FileName;
        int sheet = int.Parse(textBox1.Text);
        Excel excel = new Excel(filename, sheet);
        string SuperClass = excel.ReadCell(1, 1);
        string ClassName = excel.ReadCell(2, 1);
        string ClassLabel = excel.ReadCell(3, 1);
        string AttrName = excel.ReadCell2(8, 1).ToString();
        richTextBox1.Text =  "" + AttrName;

    }
我的函数读取excel

public IEnumerable<string> ReadCell2(int i, int j)
    {
        try
        {

            while (ws.Cells[i, j].Value2 != null)
            {
                Console.WriteLine(ws.Cells[i, j].Value2);
                yield return ws.Cells[i, j].Value2;
                i++;
            }
        }
        finally
        {

        }
        yield return "nothing";
    }
public IEnumerable ReadCell2(inti,intj)
{
尝试
{
while(ws.Cells[i,j].Value2!=null)
{
Console.WriteLine(ws.Cells[i,j].Value2);
收益率返回ws.Cells[i,j].Value2;
i++;
}
}
最后
{
}
收益率“零”;
}

ToString
通常不会为集合/可枚举项重载,默认情况下返回类型名称,并且不会枚举底层的
IEnumerable
,因此您需要使用
字符串。Join

string AttrName = string.Join("", excel.ReadCell2(8, 1));

至于为什么
ToString
在这种特殊情况下返回
sdexpformsap1.Excel+d_u6
,这是因为
yield return
实际上是一种语法糖,编译器将为您生成
IEnumerable
实现,其中包含类型名作为其自身的一部分。例如,您可以在问题的答案中了解更多信息。

@SantaaClauss很乐意提供帮助!我试图在list
var AttrName=string.join(“/”,excel.ReadCell2(8,1))中进行转换后连接和拼接;var AttrName2=AttrName.Split('/').ToList()可以返回收益率为的列表?@SantaaClauss为什么需要
列表
?您可以通过
ToList
IEnumerable
转换为列表。我需要一个lsit来返回
String:01 String:02 String:03
,但不能只返回一个字符串