Exemple #1
0
        public override DataTable getData(int maxRows)
        {
            DelimitedFileEngine<DelimitedRow> engine = new FileHelpers.DelimitedFileEngine<DelimitedRow>();
            engine.Options.Delimiter = this.delimiter;
            engine.Options.IgnoreEmptyLines = true;
            //engine.Options.IgnoreCommentedLines = true;
            DataTable dt = new DataTable();
            long recordCount = 0;
            DelimitedRow[] rows;

            if (!System.IO.File.Exists(this.fileName))
            {
                throw new Exception(string.Format("Problem finding file {0}?", this.fileName));
            }
            try
            {
                rows = engine.ReadFile(this.fileName, maxRows);
            }
            catch (System.Exception ex)
            {
                throw new Exception(string.Format("Problem reading file {0}?", this.fileName), ex);
            }

            foreach (DelimitedRow row in rows)
            {
                //First row?
                if (recordCount == 0)
                {
                    if (this.hasHeaders)
                    {
                        // First row contains column names
                        foreach (String s in row.fieldValues)
                        {
                            if (s != null && s.Trim().Length > 0 && !dt.Columns.Contains(s))
                                dt.Columns.Add(s, typeof(String));
                            else
                                dt.Columns.Add(GetNextColumnHeader(dt), typeof(String));
                        }
                    }
                    else
                    {
                        // First row just contains values
                        foreach (String s in row.fieldValues)
                        {
                            dt.Columns.Add(GetNextColumnHeader(dt), typeof(String));
                        }
                        dt.Rows.Add(row.fieldValues);
                    }
                }
                else //not first row
                {
                    dt.Rows.Add(row.fieldValues);
                }
                recordCount++;
                if (recordCount >= maxRows)
                    break;

            }
            return dt;
        }
Exemple #2
0
        //CFB 21/07/2010 alternative parse using FileHelpers component for greater flexibility later..
        public static DataTable ParseUsingFileHelpers(String fileName, bool headers, String delimiter, int maxRows)
        {
            DelimitedFileEngine<DelimitedRow> engine = new FileHelpers.DelimitedFileEngine<DelimitedRow>();
            engine.Options.Delimiter = delimiter;
            engine.Options.IgnoreEmptyLines = true;
            //engine.Options.IgnoreCommentedLines = true;
            DelimitedRow[] rows = engine.ReadFile(fileName, maxRows);

            DataTable dt = new DataTable();
            fileName = fileName.Trim();
            //FileHelperAsyncEngine inputFile = new FileHelperAsyncEngine(typeof(DelimitedRow));
            //Alt syntax using generics..
            //FileHelperAsyncEngine<DelimitedRow> engine = new FileHelperAsyncEngine<DelimitedRow>();

            long recordCount = 0;

            if (!System.IO.File.Exists(fileName))
            {
                throw new Exception(string.Format("Problem finding file {0}?", fileName));
            }
            try
            {
                //engine.BeginReadFile(fileName);
            }
            catch (System.Exception ex)
            {
                throw new Exception(string.Format("Problem reading file {0}?", fileName), ex);
            }

            foreach (DelimitedRow row in rows)
            {
                //First row?
                if (recordCount == 0)
                {
                    if(headers)
                    {
                        // First row contains column names
                        foreach (String s in row.fieldValues)
                        {
                            if (s != null && s.Trim().Length > 0 && !dt.Columns.Contains(s))
                                dt.Columns.Add(s, typeof(string));
                            else
                                dt.Columns.Add(GetNextColumnHeader(dt), typeof(string));
                        }
                    }
                    else
                    {
                        // First row just contains values
                        foreach (String s in row.fieldValues)
                        {
                            dt.Columns.Add(GetNextColumnHeader(dt), typeof(string));
                        }
                        dt.Rows.Add(row.fieldValues);
                    }
                }
                else //not first row
                {
                    dt.Rows.Add(row.fieldValues);
                }
                recordCount++;
                if (recordCount >= maxRows)
                    break;

            }
            return dt;
        }