使用c#删除CSV文件中的列

时间:2017-10-09 14:19:04

标签: c# csv

我想删除具有特定值的列。下面的代码是我用来删除行的代码。我可以将其反转以删除列吗?

int row = comboBox1.SelectedIndex;
string verw = Convert.ToString(txtChange.Text);

List<string> lines = new List<string>();
using (StreamReader reader = new StreamReader(System.IO.File.OpenRead(filepath)))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        if (line.Contains(","))
        {
            string[] split = line.Split(',');

            if (split[row] == kill) 
            {
                //achter split vul je de rij in 
            }
            else
            {
                line = string.Join(",", split);
                lines.Add(line);
            }
        }
    }
}
using (StreamWriter writer = new StreamWriter(path, false))
{
    foreach (string line in lines)
        writer.WriteLine(line);
}

2 个答案:

答案 0 :(得分:1)

假设我们忽略了编写CSV的微妙之处,这应该有效:

public void RemoveColumnByIndex(string path, int index)
{
    List<string> lines = new List<string>();
    using (StreamReader reader = new StreamReader(path))
    {
        var line = reader.ReadLine();
        List<string> values = new List<string>();                
        while(line != null)
        {
            values.Clear();
            var cols = line.Split(',');
            for (int i = 0; i < cols.Length; i++)
            {
                if (i != index)
                    values.Add(cols[i]);
            }
            var newLine = string.Join(",", values);
            lines.Add(newLine);
            line = reader.ReadLine();
        }
    }

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

}

代码基本上加载每一行,将其分解为列,循环遍历列而忽略相关列,然后将值重新组合成一行。

当然,这是一种过于简化的方法。我相信有更多高效的方式。

答案 1 :(得分:0)

要按名称删除列,以下是对代码示例的一点修改。

        List<string> lines = new List<string>();
        using (StreamReader reader = new StreamReader(System.IO.File.OpenRead(path)))
        {
            string target = "";//the name of the column to skip

            int? targetPosition = null; //this will be the position of the column to 
           remove if it is available in the csv file
            string line;

            List<string> collected = new List<string>();
            while ((line = reader.ReadLine()) != null)
            {

                    string[] split = line.Split(',');
                    collected.Clear();

                    //to get the position of the column to skip
                    for (int i = 0; i < split.Length; i++)
                    {
                        if (string.Equals(split[i], target, StringComparison.OrdinalIgnoreCase))
                       {
                            targetPosition = i;
                            break; //we've got what we need. exit loop
                      }
                    }

                    //iterate and skip the column position if exist



                    for (int i = 0; i < split.Length; i++)
                    {
                        if (targetPosition != null && i == targetPosition.Value) continue;

                        collected.Add(split[i]);

                    }

                   lines.Add(string.Join(",", collected));



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