比较C#中的两个csv文件

时间:2017-10-11 12:46:38

标签: c# csv

我想比较两个csv文件并打印文件中的差异。我目前使用下面的代码删除一行。我可以更改此代码,以便比较两个csv文件,还是在c#中有更好的方法来比较csv文件?

  List<string> lines = new List<string>();
        using (StreamReader reader = new StreamReader(System.IO.File.OpenRead(path)))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                if (line.Contains(csvseperator))
                {
                     string[] split = line.Split(Convert.ToChar(scheidingsteken));

                    if (split[selectedRow] == value)
                    {

                    }
                    else
                    {
                        line = string.Join(csvseperator, split);
                        lines.Add(line);
                    }
                }

            }
        }

        using (StreamWriter writer = new StreamWriter(path, false))
        {
            foreach (string line in lines)
                writer.WriteLine(line);
        }
    }

2 个答案:

答案 0 :(得分:0)

如果您只想比较一列,可以使用以下代码:

                List<string> lines = new List<string>();
    List<string> lines2 = new List<string>();



    try
    {
        StreamReader reader = new StreamReader(System.IO.File.OpenRead(pad));
        StreamReader read = new StreamReader(System.IO.File.OpenRead(pad2));

        string line;
        string line2;

        //With this you can change the cells you want to compair
        int comp1 = 1;
        int comp2 = 1;

        while ((line = reader.ReadLine()) != null && (line2 = read.ReadLine()) != null)
        {           
            string[] split = line.Split(Convert.ToChar(seperator));
            string[] split2 = line2.Split(Convert.ToChar(seperator));

            if (line.Contains(seperator) && line2.Contains(seperator))
            {
                if (split[comp1] != split2[comp2])
                {
                    //It is not the same
                }
                else
                {
                    //It is the same

                }
            }
        }
        reader.Dispose();
        read.Dispose();
    }
    catch
    {

    }

答案 1 :(得分:0)

以下是使用Cinchoo ETL - 开源库

查找CSV文件之间差异的另一种方法

以下示例CSV文件

<强> sample1.csv

id,name
1,Tom
2,Mark
3,Angie

<强> sample2.csv

id,name
1,Tom
2,Mark
4,Lu

使用Cinchoo ETL,下面的代码显示了如何通过所有列找到行之间的差异

var input1 = new ChoCSVReader("sample1.csv").WithFirstLineHeader();
var input2 = new ChoCSVReader("sample2.csv").WithFirstLineHeader();

using (var output = new ChoCSVWriter("sampleDiff.csv").WithFirstLineHeader())
{
    output.Write(input1.OfType<ChoDynamicObject>().Except(input2.OfType<ChoDynamicObject>(), ChoDynamicObjectEqualityComparer.Default));
    output.Write(input2.OfType<ChoDynamicObject>().Except(input1.OfType<ChoDynamicObject>(), ChoDynamicObjectEqualityComparer.Default));
}

<强> sampleDiff.csv

id,name
3,Angie
4,Lu

如果你想通过&#39; id&#39;来做差异。柱,

var input1 = new ChoCSVReader("sample1.csv").WithFirstLineHeader();
var input2 = new ChoCSVReader("sample2.csv").WithFirstLineHeader();

using (var output = new ChoCSVWriter("sampleDiff.csv").WithFirstLineHeader())
{
    output.Write(input1.OfType<ChoDynamicObject>().Except(input2.OfType<ChoDynamicObject>(), new ChoDynamicObjectEqualityComparer(new string[] { "id" })));
    output.Write(input2.OfType<ChoDynamicObject>().Except(input1.OfType<ChoDynamicObject>(), new ChoDynamicObjectEqualityComparer(new string[] { "id" })));
}

希望这有帮助。