-3

I am trying to read a CSV file using C#, perform calculations on the data and finally export the results to a new CSV or text file. Can anyone suggest a good approach to do this?

Debasish
  • 369
  • 4
  • 12
Russ
  • 19
  • 6
  • 2
    Hi there, what have you tried so far? –  Apr 30 '15 at 11:11
  • Hi, ive managed to open the csv by splitting by the (',') delimiter but as for working with it and performing calculations seems very tricky – Russ Apr 30 '15 at 11:32
  • You may be best off using a library like [LinqToCSV](https://github.com/mperdeck/LINQtoCSV) – stuartd Apr 30 '15 at 11:38
  • would you be able to help me more? Whats the LinqToCSV library ? Thanks – Russ Apr 30 '15 at 11:55
  • This is how people discourage someone new trying to learn. He mentioned that he is a new programmer and I am sure many face this kind of problems. Why to down vote the question then, instead of explaining the OP about what's wrong with it. – Indigo Apr 30 '15 at 12:29
  • 2
    @Indigo The question doesn't show any signs that the OP visited the help center to find out how to ask a question. Why should we keep explaining if people don't do that? It's not that we'd have to do this only once a day or so. (I didn't downvote by the way). This is a subject of which ample resource are available, as you said in your answer. The OP should have shown some (preliminary) code. As it is now this question is: too broad and unclear (how can we ever choose the best approach without knowing more?) - both valid close reasons. – Gert Arnold Apr 30 '15 at 17:37
  • I edited your questions to, hopefully, make it simpler to understand. I also removed references to Visual studio as this is about C# not about studio itself. As some of the comments have mentioned it is advised that you first attempt to solve the issue yourself and explain what you tried and why it did not work in your question. That way people will be better able to understand what is wrong. Good luck – Andre Apr 30 '15 at 19:22

3 Answers3

0

convert the csv file into datatable.

perform your calculation by getting data from datatable.

you can convert your csv to datatable like this

public static DataTable ImportCSVtoDatatable(string filepath, string strQuery )
    {
       //var fileName = Path.GetFileName(filepath); 
    //strQuery = @"SELECT * FROM " + fileName;

        DataSet ds = new DataSet();
        var constring = string.Format(@"Provider=Microsoft.Jet.OleDb.4.0; Data Source={0};Extended Properties=""Text;HDR=YES;FMT=Delimited""", Path.GetDirectoryName(filepath));

        OleDbConnection con = new OleDbConnection(constring);

        OleDbDataAdapter da = new OleDbDataAdapter(strQuery, con);


        da.Fill(ds);
        DataTable dt = ds.Tables[0];
        return dt;
    }
Debasish
  • 369
  • 4
  • 12
0

I would suggest to convert your CSV to DataTable, Perform all the calculations efficiently using Linq and then write out the DataTable to CSV.

It would be fast as well more efficient. In addition, DataTable will give you more flexibility and control over your data without using any external help.

Ideally a working sample should be posted, but I don't want to take credit of what I haven't done, so here are some of the useful links to get going.

Refer this to convert CSV to DataTable

Refer this for DataTable to CSV

Please note there are many useful samples available online, you can take one of them as a reference and then extend it to suite your needs.

Community
  • 1
  • 1
Indigo
  • 2,887
  • 11
  • 52
  • 83
0

I often use http://www.nuget.org/packages/CsvHelper/ which I find very good for reading and writing CSV files. Regarding the calculations, given your description I would probably create two classes that map to input and output rows and write some logic that produces the results (output row objects) from the input objects.

Myles McDonnell
  • 12,943
  • 17
  • 66
  • 116