Writing a .CSV file in C#

Writing a .CSV file in C#

How to save a CSV file using C# using the CSV Helper library 😬

I have been working on a project in C# to acquire values from an Arduino Uno device. Everything is going well until the point to save the data in some interchangeable format. In the beginning, the first thought was using Microsoft Excel ⭐. However, to compute some statistics and display some customized charts we decided to continue the project in Python.

With Python, there are different tools for visualization and manipulation of data. As part of these tools, Pandas is one of my favorites to analyze data (using a Jupyter notebook) 🥳

In any case, the decision to store the data was a CSV format using the code in C# where there are some options:

  • StreamWriter + WriteLine + Flush
  • StringBuilder + AppenLine + WriteAllText
  • a combination of those, always remembering the try/catch to all throwing exceptions

Well, these options sound great! 😃 However, I keep searching for a better option and I found CSV Helper 😎 A completely free library to commercial use.

The CSV logo
Source: https://joshclose.github.io/CsvHelper/

In the official webpage of CSV Helper it defines as:

A library for reading and writing CSV files. Extremely fast, flexible,  and easy to use. Supports reading and writing of custom class objects

You can download it from the Nuget package site or directly using Visual Studio:

Maximize for better visualization

After, just to continue with the installation process (check the output window) and that's it. Let me show some code to open and define the CSV file:

using CsvHelper;
// ...
// ...
saveFile = DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss") + ".csv";
writer = new System.IO.StreamWriter(saveFile);

csv = new CsvWriter(writer, System.Globalization.CultureInfo.InvariantCulture);
csv.Configuration.Delimiter = ",";  //optional

As with any library on C#, it is necessary to import it with the using statement. You can check that the saveFile variable built a string name with a date/hour format and .csv extension.

After is when the CSV Helper comes to the action! Of course, there are  several options and examples that you can check on its official  documentation, however, the idea is just to save a bunch of values into a  CSV using simple commas as separators. For this point, the file is  created ready to receive values to store. Notice that there is not a header definition yet 😧

Assuming the storage of only 3 variables into an array, the easiest way is writing the line:

csv.WriteRecord<CSVDataFormat>(new CSVDataFormat(data[i].value1, data[i].value2, data[i].value3));

That's it! not more than that. Well, you can use the function WriteRecords to store a batch of data.  Last but not least, do not forget to call the writer.Close() when you Dispose/Close into your code.

This is not a tutorial in how to use CSV Helper, is just a rapid and single way to write a CSV file in C# 🤪. There is too much more to talk about that library as the reading process, configuration/personalization into files, type conversions and more. Please check the examples on the official page for further information.

I hope this would be useful to other developers 🤖

From a geek to geeks

Share Tweet Send

Related Articles

You've successfully subscribed to The ecode.DEV repository
Great! Next, complete checkout for full access to The ecode.DEV repository
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.