3

I'm working in C# and need making use of Entity Framework 6. I have a service that calls a stored procedure (using the Dbcontext) and places the results in an IList. I then have a controller that makes use of this service. Now originally I was using the results combined with Epplus to save this as an Excel/Xlsl File - this worked perfectly/as intended.

However now, I need to save it as an CSV file. I have found several links, such as this and this, which converts excel to CSV (however I can now skip this step, as I can just convert the resultset to CSV, with no need for the excel file), I also found this link.

From what I understand, it is fairly easy to export/convert a dataset/result set to CSV using a stringbuilder. However, I was wondering, given that I have Epplus and the ability to save as Excel- is there not a cleaner way of doing it? Or is it best to take the data, use a stringbuilder for comma delimiting the values and use that for the CSV?

I know similar topics (like this one) have been posted before - but I felt my question was unique enough for a new post.

Community
  • 1
  • 1
adrian
  • 2,786
  • 2
  • 18
  • 33
  • Is this what are you looking for? http://stackoverflow.com/questions/1963719/c-sharp-sqlserver-retrieving-results-and-place-in-a-csv-format – brduca May 05 '15 at 09:57

1 Answers1

-1

Using EPPlus is not a cleaner way of doing this. You would only need much more code to accomplish exactly the same result. Creating a CSV file is nothing more than writing a text file with commas in it. So why not just do that?

StringBuilder sb = new StringBuilder();
foreach (DataRow dr in yourDataSet) 
{
  List<string> fields = new List<string>();
  foreach (object field in dr.ItemArray) 
  {
     fields.Add(field);
  }
  sb.Append(String.Join(",", fields) + Environment.NewLine);
}

//and save.. sb.ToString() as a .csv file
Tys
  • 3,592
  • 9
  • 49
  • 71
  • 2
    Saying "nothing more than x" is usually a bit dangerous. There is often hidden depth that you do not account for. In this case, you need to escape any comma characters that are in the actual fields themselves. you also need to escape newline characters and so on. There's also the question of including column names in the first row or not. .CSV is pretty simple, but that still doesn't mean you have to roll out your own implementation, better to use an existing tried and tested csv exporter that will handle all kinds of data. – irreal May 05 '15 at 10:04
  • You are completely right. But, still, if needed, adding those 2 'features' can be done with just a few lines of extra code. I have worked quite a lot with EPPlus, and for creating Excel files it's really great, but for just creating that CSV file i would not go for it. – Tys May 05 '15 at 10:08
  • There's actually a lot in the way of robustness and optimizations that you could and sometimes, depending on how, when and what you are exporting, should do. Check out this library for an example of what kinds of things a csv exporter might need http://www.codeproject.com/Articles/685310/Simple-and-fast-CSV-library-in-Csharp – irreal May 05 '15 at 10:11
  • Interesting project, but from here on I'd like to leave it up to the topic starter. I don't know whether he needs a car or a fancy Mercedes. – Tys May 05 '15 at 10:19
  • I think I'm actually going to use something like http://www.codeproject.com/Articles/685310/Simple-and-fast-CSV-library-in-Csharp . Just trying it out – adrian May 05 '15 at 12:40
  • Alright, as said, it's up to you to decide what exactly you need for your application. – Tys May 05 '15 at 12:45