Exemplo n.º 1
0
            public virtual bool Load(string path, string lineIdentifier, IList <CsvColumn> extraColumns)
            {
                using (System.IO.StreamReader file = new System.IO.StreamReader(path))
                {
                    StringBuilder  buffer = new StringBuilder();
                    IList <String> values = new List <String>();
                    IList <String> lines  = new List <String>();
                    Rows = new List <CsvRow>();
                    String   line    = file.ReadLine();
                    String[] columns = line.Split(',');
                    ColumnHeaders = new List <String>();
                    foreach (String column in columns)
                    {
                        ColumnHeaders.Add(column);
                    }

                    if (extraColumns != null)
                    {
                        foreach (CsvColumn column in extraColumns)
                        {
                            ColumnHeaders.Add(column.Header);
                        }
                    }

                    StringBuilder lineBuilder = new StringBuilder();
                    string        pattern     = lineIdentifier;
                    bool          openQuote   = false;
                    bool          openElement = false;

                    #region Load all of the products
                    while ((line = file.ReadLine()) != null)
                    {
                        if (String.IsNullOrWhiteSpace(pattern) == false)
                        {
                            MatchCollection matches = Regex.Matches(line, pattern, RegexOptions.IgnoreCase);
                            if (matches.Count == 1)
                            {
                                if (lineBuilder.Length > 0)
                                {
                                    lines.Add(lineBuilder.ToString());
                                }
                                lineBuilder.Clear();
                                lineBuilder.Append(line);
                            }
                            else
                            {
                                lineBuilder.Append(line);
                            }
                        }
                        else
                        {
                            lines.Add(line);
                        }
                    }
                    #endregion

                    #region Create the file rows
                    foreach (String fullLine in lines)
                    {
                        String columnName = String.Empty;
                        CsvRow row        = new CsvRow(this);
                        int    columnIdx  = 0;

                        openQuote   = false;
                        openElement = false;

                        buffer.Clear();

                        for (int charIdx = 0; charIdx < fullLine.Length; charIdx++)
                        {
                            Char currentChar = fullLine[charIdx];
                            if (currentChar == '<')
                            {
                                openElement = true;
                            }
                            else if (currentChar == '>')
                            {
                                openElement = false;
                            }
                            else if (currentChar == '"' && openElement == false)
                            {
                                if (openQuote == false)
                                {
                                    openQuote = true;
                                }
                                else
                                {
                                    openQuote = false;
                                }
                            }
                            else if (currentChar == ',' && openQuote == false && openElement == false)
                            {
                                columnName = GetColumnName(columnIdx);
                                if (String.IsNullOrWhiteSpace(columnName) == false)
                                {
                                    row.AddColumn(columnName, buffer.ToString());
                                    openQuote = false;
                                    buffer.Clear();
                                    columnIdx++;
                                }
                                continue;
                            }
                            buffer.Append(currentChar);
                        }
                        columnName = GetColumnName(columnIdx);
                        if (String.IsNullOrWhiteSpace(columnName) == false)
                        {
                            row.AddColumn(columnName, buffer.ToString());
                        }
                        Rows.Add(row);
                    }
                    #endregion

                    if (extraColumns != null)
                    {
                        foreach (CsvRow row in Rows)
                        {
                            foreach (CsvColumn column in extraColumns)
                            {
                                row.AddColumn(column.Header, column.Value);
                            }
                        }
                    }
                }
                return(ColumnHeaders.Count > 0 && Rows.Count > 0);
            }