首页 > 解决方案 > 如何转换字典中的 excel 文件列?

问题描述

我有一个带有 n 列的 Excel 文件(csv 用“;”分隔),现在列数无关紧要,假设 n 是 6。

这六列中有两列,我们称之为它们Column_AColumn_B我想将其转换为字典 ( Dictionary<string, List<string>>),其中键是列名,值是由单元格中的值形成的列表,它将是文本。

例如,如果我有以下 Excel 文件:

在此处输入图像描述

我想得到一个有两个键的字典,Column_AColumn_B的值是一个列表,列表上的每个项目都是它的对应值:

desired_dictionary = {"column_A": ["a1", "a2", "a3. a31", "a4"], "column_B" = ["b1", "b2", "b3", "b4. b41"]}

有没有办法做到这一点?

标签: c#exceldictionary

解决方案


我希望它有所帮助:字典必须由键和值组成。键不能重复,并且在值中您可以保存一个数组,例如保存每列的值。键将代表 A=1、B=2、c=3... 的列数

 Dictionary<int,Array> dict = new Dictionary<int, Array>();
            string[] Row = new string[4];
            Row[0] = "Data1";
            Row[1] = "Data2";
            Row[2] = "Data3";
            Row[3] = "Data4";
            dict.Add(1, Row);

            foreach (KeyValuePair<int, Array> item in dict)
            {
                Console.WriteLine("Key: {0}, Value: {1} {2} {3} {4}", item.Key, item.Value.GetValue(0), item.Value.GetValue(1), item.Value.GetValue(2), item.Value.GetValue(3));
            }

推荐阅读