c# - 如何转换字典中的 excel 文件列?
问题描述
我有一个带有 n 列的 Excel 文件(csv 用“;”分隔),现在列数无关紧要,假设 n 是 6。
这六列中有两列,我们称之为它们Column_A
,Column_B
我想将其转换为字典 ( Dictionary<string, List<string>>
),其中键是列名,值是由单元格中的值形成的列表,它将是文本。
例如,如果我有以下 Excel 文件:
我想得到一个有两个键的字典,Column_A
它Column_B
的值是一个列表,列表上的每个项目都是它的对应值:
desired_dictionary = {"column_A": ["a1", "a2", "a3. a31", "a4"], "column_B" = ["b1", "b2", "b3", "b4. b41"]}
有没有办法做到这一点?
解决方案
我希望它有所帮助:字典必须由键和值组成。键不能重复,并且在值中您可以保存一个数组,例如保存每列的值。键将代表 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));
}
推荐阅读
- java - 获取 URL 内容有时包含奇怪的值
- awk - awk 拆分返回索引
- android - 经过身份验证的用户滥用 Firebase 数据库来增加积分
- java - 有没有办法为 servlet-app 实现自定义 SessionTrackingMode
- r - 在保留标题的同时用原始数据中的空格替换逗号的有效方法?
- c# - 如何在 AngleSharp 项目对象中获取链接的标题?
- twitter-bootstrap - 正确的前端开发环境设置?
- swift - 在 Swift 中处理昂贵的重复操作的最佳方法是什么?
- angular - Angular 8 ctrlKey 和 metaKey 等效?
- paypal - 如何使用 Payments PayPalStandard 插件将正确的详细信息传递到 PayPal 电子邮件“运输详细信息”字段?