如何将 excel 中的 table 放入 C# 中的可滚动项目列表中?

How to put a table from excel into a scrollable list of items in C#?

我在 excel sheet 中有一个 table,有两列和大约 400 行。很简单。我现在只想将该数据放入 winforms 表单上的可滚动列表中。

我已经这样做了一段时间并且变得很沮丧。我找到的所有内容都在向我展示如何将一个项目一个一个地添加到列表视图中,我认为有更简单的方法。

此列表是静态的,永远不需要更改。我该怎么做?

试试这个:

Type ExcelType = Type.GetTypeFromProgID("Excel.Application");
        dynamic Excel = Activator.CreateInstance(ExcelType);
        int Index = 1;
        Excel.WorkBooks.Open("Your workbooks file location here...");

        do
        {
            listBox1.Items.Add(Excel.Range("A" + Index).Value);
            Index += 1;
        } while (Excel.Range("A" + Index).Value != null);
        Excel.Quit();
        Excel = null;

在源代码中保存数据通常不是一个好主意。我想,没有'never changes'这样的东西。我知道您不想实施 Excel-Stuff,但您可以将数据放在 Json 中,随便...并将其用作资源。 无论如何,您可以使用 Excel 函数为复制粘贴准备数据。

对不起德语 Excel,我认为函数的英文名称应该是 'Concatenate'

然后您复制单元格并将其粘贴到您的代码中(假设您的变量被称为 'list' 并且它已经被实例化...)

不要忘记 excel 字符串函数中的额外括号!