Ejemplo n.º 1
0
 public static DataTable Parse(TextReader stream, CsvHeadersAction Headers, char delimiter)
 {
     DataTable table = new DataTable();
     CsvStream csv = new CsvStream(stream);
     string[] row = csv.GetNextRow(delimiter);
     if (row == null)
         return null;
     if (Headers == CsvHeadersAction.SkipHeaderLine)
     {
         row = csv.GetNextRow(delimiter);
     }
     if (Headers == CsvHeadersAction.UseAsColumnNames)
     {
         //foreach (string header in row)
         for (int i = 0;i<row.Length;i++)
         {
             if (row[i] != null && row[i].Length > 0 && !table.Columns.Contains(row[i]))
                 table.Columns.Add(row[i], typeof(string));
             else
                 table.Columns.Add(GetNextColumnHeader(table), typeof(string));
         }
         row = csv.GetNextRow(delimiter);
     }
     while (row != null)
     {
         while (row.Length > table.Columns.Count)
             table.Columns.Add(GetNextColumnHeader(table), typeof(string));
         table.Rows.Add(row);
         row = csv.GetNextRow(delimiter);
     }
     stream.Close();
     stream.Dispose();
     return table;
 }
Ejemplo n.º 2
0
        public static DataTable Parse(TextReader stream, CsvHeadersAction Headers, char delimiter)
        {
            DataTable table = new DataTable();
            CsvStream csv   = new CsvStream(stream);

            string[] row = csv.GetNextRow(delimiter);
            if (row == null)
            {
                return(null);
            }
            if (Headers == CsvHeadersAction.SkipHeaderLine)
            {
                row = csv.GetNextRow(delimiter);
            }
            if (Headers == CsvHeadersAction.UseAsColumnNames)
            {
                //foreach (string header in row)
                for (int i = 0; i < row.Length; i++)
                {
                    if (row[i] != null && row[i].Length > 0 && !table.Columns.Contains(row[i]))
                    {
                        table.Columns.Add(row[i], typeof(string));
                    }
                    else
                    {
                        table.Columns.Add(GetNextColumnHeader(table), typeof(string));
                    }
                }
                row = csv.GetNextRow(delimiter);
            }
            while (row != null)
            {
                while (row.Length > table.Columns.Count)
                {
                    table.Columns.Add(GetNextColumnHeader(table), typeof(string));
                }
                table.Rows.Add(row);
                row = csv.GetNextRow(delimiter);
            }
            stream.Close();
            stream.Dispose();
            return(table);
        }
Ejemplo n.º 3
0
 public static DataTable Parse(TextReader stream, CsvHeadersAction Headers, CsvDelimiter delimiter)
 {
     return Parse(stream, Headers, (char)delimiter);
 }
Ejemplo n.º 4
0
 public static DataTable Parse(TextReader stream, CsvHeadersAction Headers, CsvDelimiter delimiter)
 {
     return(Parse(stream, Headers, (char)delimiter));
 }