示例#1
0
 private void SetupCsvParser(string fileName, bool hasHeader, string separator)
 {
     this.fields = new List <string>();
     try
     {
         this.encoding          = CsvLib.GetFileEncoding(fileName);
         this.sr                = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read), this.encoding);
         this.Separator         = (string.IsNullOrEmpty(separator)) ? (char)FindDelimiter(100) : Convert.ToChar(separator);
         this.CurrentLine       = this.sr.ReadLine();
         this.RowIndex          = this.RowIndex + 1;
         this.dataStartPosition = (long)this.sr.CurrentEncoding.GetByteCount(this.CurrentLine);
         this.fields            = CsvLib.CsvSplit(this.CurrentLine, this.StripQuotes, this.Trim, this.Separator, this.Quote);
         this.FieldCount        = this.fields.Count;
         if (hasHeader)
         {
             return;
         }
         this.dataStartPosition = 0L;
         this.fields            = new List <string>();
         for (int index = 0; index < this.FieldCount; ++index)
         {
             this.fields.Add("Field" + (object)index);
         }
         this.MoveToPositionInFile(this.dataStartPosition);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
示例#2
0
        public T CsvItem <T>(string name, T defaultValue)
        {
            string str = this.CsvItem(name);

            try
            {
                if (string.IsNullOrEmpty(str))
                {
                    return(defaultValue);
                }
                Type type = typeof(T);
                try
                {
                    return((T)CsvLib.ValueAs(type, str));
                }
                catch
                {
                    return(defaultValue);
                }
            }
            catch (Exception)
            {
                return(defaultValue);
            }
        }
示例#3
0
        public void ReadLine()
        {
            this.parserLineStatus = CsvOrderedLineStatustype.Valid;
            if (this.sr != null && !this.sr.EndOfStream)
            {
                string source = this.sr.ReadLine();
                this.errorRow   = "";
                this.lineNumber = this.lineNumber + 1;
                while (true)
                {
                    do
                    {
                        this.orderedItems = new List <string>();
                        if (string.IsNullOrEmpty(source))
                        {
                            if (this.sr.EndOfStream)
                            {
                                this.items = (List <string>)null; return;
                            }
                            source = this.sr.ReadLine();
                        }

                        this.errorRow = source;
                        this.items    = CsvLib.CsvSplit(source, this.StripQuotes, this.Trim, this.Separator, this.Quote);
                        if (this.requestedFields != null && this.requestedFields.Count > 0)
                        {
                            for (int index = 0; index < this.requestedFields.Count; ++index)
                            {
                                this.orderedItems.Add(this.CsvItem(this.requestedFields[index].FieldName));
                            }
                        }

                        if (!this.IsValidLine())
                        {
                            if (this.items.Count > this.fields.Count)
                            {
                                this.errorRow = source;
                                this.items    = (List <string>)null;
                                return;
                            }
                        }
                        else
                        {
                            return;
                        }

                        if (!this.sr.EndOfStream)
                        {
                            source += this.sr.ReadLine();
                        }
                        else
                        {
                            return;
                        }
                    }while (this.items.Count >= this.fields.Count);
                }
            }
            else
            {
                this.items = (List <string>)null;
            }
        }