Monday, September 21, 2009

Simple code to read and convert the CSV into List using C#

ere is the simple code for converting a CSV into List<Object>

static void Main(string[] args)
        {
            List<object> result = new List<object>();
            string FileName = @"D:\Samples\test.csv";
            FileStream f = File.Open(FileName, FileMode.Open);
            TextReader reader = new StreamReader(f);
            string Row = string.Empty;
            while((Row = reader.ReadLine()) != null)
            {
                List<string> RowDetails = SplitRow(Row);
                result.Add(RowDetails);
            }
            reader.Close();
            f.Close();
        }
       static List<string> SplitRow(string Row)
        {
            List<string> result = new List<string>();
            string[] splitRow = Row.Split(",".ToCharArray());
            result = splitRow.ToList<string>();
            return result;
        }

3 comments:

  1. The first and easiest way to c# convert csv to pdf is to use the rasteredage page http://www.rasteredge.com/how-to/csharp-imaging/pdf-convert-csv-to-pdf/. The c# convert csv to pdf online function is not static, so you'll need to create an instance of the class.

    ReplyDelete