-1

I need to write code for import CSV file I read file using stream reader. There is no problem in coding it's working good. But I have problem in CSV file where I need to import CSV from web in that lot of fraction numbers are there if no is like 3/4 after I read file value is 3/4/19 as date format. What could I do to avoid this issue? Due to directly importing file from web I can't open and format each and every cell. Note : there is multiple combination of values like decimal fraction and also string for names

rgokulj
  • 1
  • 1
  • 3
    Gonna be pretty tough to answer this without seeing your code and a good example of all the different kind of data you're importing. Post 10 lines from the file – Caius Jard May 12 '19 at 04:11

2 Answers2

0

I recommend you to use a library to achieve this. CsvHelper is a .NET library for reading and writing CSV files. Extremely fast, flexible, and easy to use. It's also RFC 4180 Compliant Adheres to the RFC 4180 standard to ensure compatibility across systems.

One of the good features of this library, that you can map a CSV file to C# class.

public class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }    
}

void Main()
{
    using (var reader = new StreamReader("path\\to\\file.csv"))
    using (var csv = new CsvReader(reader))
    {
        var records = csv.GetRecords<Foo>();
    }
}

Sample CSV:

Id,Name
1,one

It supports many other ways to read, I recommend to read it's website and decimations.

Ali Bahrami
  • 5,935
  • 3
  • 34
  • 53
0

You can use CSVHelper as mention in another answer as well. As I read your question I understand the problem is multiple format is passed for datetime.

If this is the issue, I believe you should try parsing it using multiple format, for example one mentioned in this answer https://stackoverflow.com/a/17859959/713789

You might need to write a mapper to do this or just simply write your code to read the date as in format you want. This must fix your issue.

Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79