我从C#开始,以及在Interop上的使用。
我的目标是创建一个Windows应用程序,该应用程序可以返回Excel的值。
我的问题是在进行循环时,由于“ return”关闭了循环,因此我仅获得了第一个值。
[我做了什么为了解决这个问题,我在函数中使用了“ IEnumerable”,同时保持了“字符串”类型。
函数的返回 SDExpFormsApp1.Excel + d__6
我不明白为什么返回值是后跟“ d__6”的类的名称
我试图在这里找到一些有趣的东西:https://docs.microsoft.com/fr-fr/dotnet/api/system.collections.generic.ienumerator-1?view=netcore-3.1
我的活动功能
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";
}
ToString
对于集合/枚举不重载,并且默认情况下返回类型名称,并且不会枚举基础IEnumerable
,因此您需要使用string.Join
:
string AttrName = string.Join("", excel.ReadCell2(8, 1));