在另一个函数中获取 var 的数组值

Get the array values of var inside a function in another

我对函数调用有理解上的问题。 我在 'excel' class 中的第一个功能是查找名为“* ID *”的单元格。

public IEnumerable<string> FindID()
{
    try
    {
        while (true)
        {
            if (ws.Cells[i, j].Value2 == "*ID*")
            {
                string ID = ws.Cells[i, j].Value2;
                yield return ID;
                yield break;
            }
            else
            {
                i++;
                if (ws.Cells[i, j].Value2 == "*MAIN INFO END*")
                {
                    i = 1;
                    j++;
                }
            }
        }
    }
    finally
    {
    }
}

我的第二个函数循环放置在字符串中的列,例如“名称”或“可搜索”以查找列中的所有值,并将所有值连接到每行的唯一字符串中。

public IEnumerable<string> AttributeToForm(int i, int j)
{
    try
    {
        while (true)
        {
            if (ws.Cells[i, j].Value2 != null)
            {
                FindID();
                string label = ID;
                string name = ws.Cells[i, j + 1].Value2;
                string searchable = ws.Cells[i, j + 3].Value2;
                string FormField = "<FormField :span=\"6\" data-bwid =\"" + name + "\" name=\"" + name + "\" label=\"" + label + "\" ";
                if (searchable != null) {

                    FormField += string.Join("", "searchable");
                }
                if(FormField != null)
                {
                    FormField += string.Join("", "/>" + "\n");
                }
                else
                {

                }
                yield return label;
                i++;
            }
            else
            {
                yield break;
            }
        }
    }
    finally
    {
    }
}

BUT...我真正想要的是在我的 'AttributeToForm' 函数中使用我的 'FindID' 中包含的字符串 'ID'功能.. 为了到达那里,我尝试 运行 "FindID" 函数并在字符串 中检索 ID。然而,结果仍然是我的字符串 class "ID",所以 "nothing".

class Excel
{
    private  int i = 1;
    private  int j = 1;
    private string ID = "nothing";...

对于你的问题,你想制作一个获取所有列的方法。

您可以试试下面的代码。

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Excel.Application app = new Excel.Application();
        Excel.Workbook workbook = null;
        Excel.Worksheet worksheet = null;


        private void button1_Click(object sender, EventArgs e)
        {
            var id = FindColumn("id");
            var name = FindColumn("name");
            var searchable = FindColumn("search");

        }

        public IEnumerable<string> FindColumn(string name)
        {
            List<string> list = new List<string>();
            try
            {

                int row = worksheet.Cells.Find("*", System.Reflection.Missing.Value, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlPrevious, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Row;
                int column = worksheet.Cells.Find("*", System.Reflection.Missing.Value, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlPrevious, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Column;
                int a = 1;
                int b = 1;
                int c = 1;
                for (int i = 1; i < row + 1; i++)
                {
                    for (int j = 1; j < column + 1; j++)
                    {
                        string value = worksheet.Cells[i, j].Text;
                        if (value == name)
                        {
                            a = i;
                            b = j;
                        }
                        string vlaue2 = worksheet.Cells[i, b].Text;
                        if (vlaue2 == "*MAIN INFO END")
                        {
                            c = i;
                            break;
                        }

                    }
                }
                for (int j = a + 1; j < c; j++)
                {
                    list.Add(worksheet.Cells[j, b].Text);
                }

                return list.AsEnumerable();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                Console.WriteLine(ex.ToString());
                return null;
            }


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            workbook = app.Workbooks.Open("D:\1.xlsx");
            worksheet = workbook.ActiveSheet;

        }
    }

我的Excel档案:(和你差不多)

结果:

@Jack J Jun - MSFT

我的excel看起来像这样