简体   繁体   English

如何从Dictionary创建.csv文件 <List, List<string> &gt;在C#中

[英]How to create a .csv file from a Dictionary<List, List<string>> in C#

I have a Dictionary(List, List(string)) (apologies, for some reason, this editor is not allowing me to type <>) which looks like the following: 我有一个字典(List,List(string))(道歉,出于某种原因,这个编辑器不允许我输入<>),如下所示:

Key: A --> Value:{1,2,3}
Key: B --> Value:{a,b,c}
Key: C --> Value:{Hi,Hello,Hola}

I would like to create a .csv file with Headers corresponding to my dictionary Keys, and 'Columns' corresponding to the List's. 我想创建一个带有Headers的.csv文件,对应于我的字典Keys,以及对应于List的'Columns'。

So, for instance, First 'column in my csv file should be: column 'A', with values 1 2 3 Second 'column' in my csv file should be: column 'B', with values abc, etc.. (Note that I would prefer that the ordering of the columns be kept, but it is ok if it's not) 因此,例如,我的csv文件中的第一列应为:列'A',值为1 2 3我的csv文件中的第二个'列'应为:列'B',值为abc等。(注意我希望保留列的顺序,但如果不是这样就可以了

Or in other words, since this will be a Comma Separated File, the First 'Row' (Headers) should be: A,B,C 或者换句话说,因为这将是逗号分隔文件,所以第一个'行'(标题)应该是: A,B,C
2nd row: 1, a , Hi 3rd row: 2, b, Hello and 4th row: 3, c, Hola 第2行: 1, a , Hi第3行: 2, b, Hello和第4行: 3, c, Hola

What is the best way to achieve this in C#? 在C#中实现这一目标的最佳方法是什么? I have tried researching this first, but most suggestions I seem to see online seem to not use a dictionary in the format Dictionary(List, List(string)). 我首先尝试过研究这个,但我似乎在网上看到的大多数建议似乎都没有使用字典(List,List(string))中的字典。

I appreciate the help. 我很感激帮助。

You can try this: 你可以试试这个:

 static void Main(string[] args)
    {
        //Sample data declaration
        Dictionary<string, List<string>> data = new Dictionary<string, List<string>>();
        data.Add("Hello", new List<string>{"1", "2", "4"});
        data.Add("Foo", new List<string> { "3", "4", "7" });
        data.Add("World", new List<string> { "3", "4", "7" });

        DicToExcel(data, @"D:\my.csv");

    }

Here comes the logic: 这是逻辑:

    public static void DicToExcel(Dictionary<string, List<string>> dict, string path)
    {
            //We will put all results here in StringBuilder and just append everything
            StringBuilder sb = new StringBuilder();

            //The key will be our header
            String csv = String.Join(",",
                   dict.Select(d => d.Key));
            sb.Append(csv + Environment.NewLine);

            //We will take every string by element position
            String csv1 = String.Join(",",
                   dict.Select(d => string.Join(",", d.Value.First().Take(1))));
            sb.Append(csv1 + Environment.NewLine);

            String csv2 = String.Join(",",
                   dict.Select(d => string.Join(",", d.Value.Skip(1).Take(1))));
            sb.Append(csv2+ Environment.NewLine);

            String csv3 = String.Join(",",
                 dict.Select(d => string.Join(",", d.Value.Skip(2).Take(1))));
            sb.Append(csv3);

            //Write the file
            System.IO.File.WriteAllText(path, sb.ToString());

    }

The result would be: 结果将是:

Hello   Foo     World
1       3       3
2       4       4
4       7       7

One solution would be to transform your map into a list of lists. 一种解决方案是将地图转换为列表列表。 The outer list corresponds to the file, the inner lists correspond to rows. 外部列表对应于文件,内部列表对应于行。 Once you have that output becomes easy. 一旦你有输出变得容易。

In your example, the new data structure would be: 在您的示例中,新数据结构将是:

[[A,B,C]
 [1,a,Hi]
 [2,b,Hello]
 [3,c,Hola]]

A set of nested loops over your map and data will allow you to do the transform. 地图和数据上的一组嵌套循环将允许您进行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM