user5813072
user5813072

Reputation: 51

Reading a CSV file then re-exporting it as a CSV file to Excel

I have already read the csv file, Now i simply want to re export it back to excel to view the changes made on the file. i have also tried to used Excel as an extensive automation API but do not know how to do this process.

public static IList<string> ReadFile(string fileName)
    {
        var results = new List<string>();

        var lines = File.ReadAllLines(fileName);
        for (var i = 0; i < lines.Length; i++)
        {
            // Skip the line with column names
            if (i == 0)
            {
                continue;
            }

            // Splitting by space. I assume this is the pattern
            var replace = lines[i].Replace(' ', ',');

            results.Add(replace);
        }

        return results;
    }

Upvotes: 0

Views: 73

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

Are you looking for some Linq, an implementation like this

   var target = File
     .ReadLines(fileName)
     .Skip(1) // Skip the line with column names
     .Select(line => line.Replace(' ', ',')); // ... I assume this is the pattern

   // Writing back to some other file
   File.WriteAllLines(someOtherFileName, target);

   // In case you want to write to fileName back, materialize:
   // File.WriteAllLines(fileName, target.ToList());

Upvotes: 1

Related Questions