Пример #1
0
        /// <summary>
        /// Loads the contents of a file into a DataTable. All columns will be strings.
        /// The first line needs to contain the header.
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="separator">Character that represents the column separation.</param>
        /// <param name="dataMapping">Column mapping.</param>
        /// <returns></returns>
        public static DataTable Load(string fileName, char separator, DataMappingEntrySet dataMapping)
        {
            StreamReader stream = new StreamReader(fileName, Encoding.Default);
            DataTable dt = null;

            try
            {
                // Initialize header (column names)
                List<string> columnNames;
                try
                {
                    string firstLine = stream.ReadLine();
                    columnNames = new List<string>(firstLine.Split(separator));
                }
                catch (Exception ex)
                {
                    throw new Exception("Error processing the header", ex);
                }

                if (CollectionUtil<string>.HasDuplicates(columnNames))
                {
                    throw new Exception(string.Format("Duplicate columns in data file {0}", fileName));
                }

                if (dataMapping != null)
                {
                    columnNames = dataMapping.ConvertHeaders(columnNames);
                }

                dt = new DataTable();
                for (int i = 0; i < columnNames.Count; i++)
                {
                    dt.Columns.Add(columnNames[i], typeof(string));
                }

                // Read the remaining file
                int lineNr = 1;
                while (stream.Peek() > -1)
                {
                    string line = stream.ReadLine();
                    string[] columns = line.Split(separator);

                    if (columns.Length != columnNames.Count)
                    {
                        throw new Exception(string.Format(
                            "Error in line {0}: {1} columns found, expected {2}",
                            lineNr, columns.Length, columnNames.Count));
                    }

                    DataRow row = dt.NewRow();
                    try
                    {
                        for (int i = 0; i < columns.Length; i++)
                        {
                            row[i] = columns[i];
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(string.Format("Error loading table in line {0}.", lineNr), ex);
                    }

                    dt.Rows.Add(row);
                    lineNr++;
                }
            }
            finally
            {
                stream.Close();
                stream.Dispose();
            }

            return dt;
        }
Пример #2
0
 /// <summary>
 /// Loads the contents of a file into a DataTable. All columns will be strings.
 /// The first line needs to contain the header and the column separator is obtained automatically.
 /// </summary>
 /// <param name="fileName"></param>
 /// <param name="dataMapping">Column mapping.</param>
 /// <returns></returns>
 public static DataTable Load(string fileName, DataMappingEntrySet dataMapping)
 {
     return Load(fileName, GetSeparator(fileName), dataMapping);
 }