Пример #1
0
        private DataSet generateTestData()
        {
            String[] headers = { "text", "numeric", "dec" };
            var ds = new DataSet(headers);

            Object[] row1 = { "One", "1", "0.1" };
            Object[] row2 = { "Two", "2", "0.2" };
            Object[] row3 = { "Three", "3", "0.3" };

            ds.Add(row1);
            ds.Add(row2);
            ds.Add(row3);

            return ds;
        }
Пример #2
0
 /// <summary>
 /// Save the specified data set to a CSV file. 
 /// </summary>
 /// <param name="filename">The filename.</param>
 /// <param name="ds">The data set to save.</param>
 public static void Save(string filename, DataSet ds)
 {
     using (StreamWriter textWriter = File.CreateText(filename))
     {
         Save(textWriter, ds);
     }
 }
Пример #3
0
        /// <summary>
        /// Save the specified data to an output stream. 
        /// </summary>
        /// <param name="textWriter">The output stream.</param>
        /// <param name="ds">The data set.</param>
        public static void Save(TextWriter textWriter, DataSet ds)
        {
            using (var writer = new CsvWriter(textWriter))
            {
                // write the headers
                foreach (string header in ds.Headers)
                {
                    writer.WriteField(header);
                }
                writer.NextRecord();

                // write the data
                foreach (var item in ds.Data)
                {
                    for (int i = 0; i < ds.HeaderCount; i++)
                    {
                        writer.WriteField(item[i].ToString());
                    }
                    writer.NextRecord();
                }
            }
        }
Пример #4
0
        /// <summary>
        ///     Load a CSV from an input stream.
        /// </summary>
        /// <param name="stream">The input stream.</param>
        /// <returns>The loaded file.</returns>
        public static DataSet Load(StreamReader stream)
        {
            DataSet result = null;
            using (var csvReader = new CsvReader(stream))
            {
                int fieldCount = 0;

                while (csvReader.Read())
                {
                    // if we just read the first row, then we need to read
                    // the headers, they were already grabbed.
                    if (result == null)
                    {
                        fieldCount = csvReader.FieldHeaders.Count();
                        var headers = new string[fieldCount];

                        for (int i = 0; i < fieldCount; i++)
                        {
                            headers[i] = csvReader.FieldHeaders[i];
                        }

                        result = new DataSet(headers);
                    }

                    // process each line
                    var obj = new Object[fieldCount];
                    for (int i = 0; i < fieldCount; i++)
                    {
                        obj[i] = csvReader.GetField<string>(i);
                    }
                    result.Add(obj);
                }
            }

            return result;
        }