コード例 #1
0
ファイル: DataSet.cs プロジェクト: Raghavendra1509/aifh
 /// <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);
     }
 }
コード例 #2
0
ファイル: DataSet.cs プロジェクト: Raghavendra1509/aifh
        /// <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();
                }
            }
        }
コード例 #3
0
ファイル: DataSet.cs プロジェクト: Raghavendra1509/aifh
        /// <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;
        }