Exemple #1
0
        /// <summary>
        /// Adds a new cell with the specified <paramref name="value"/>.
        /// </summary>
        /// <param name="column">The column header of the cell.</param>
        /// <param name="value">The value of the cell.</param>
        /// <returns>The added cell.</returns>
        public CsvCell AddCell(CsvColumn column, string value)
        {
            CsvCell cell = new CsvCell {
                Column = column, Row = Row, Value = value ?? ""
            };

            _cells.Add(cell);
            return(cell);
        }
Exemple #2
0
        /// <summary>
        /// Internal helper method for parsing the contents of a CSV file.
        /// </summary>
        /// <param name="file">The CSV file.</param>
        /// <param name="contents">The contents of the CSV file, as a string.</param>
        /// <param name="separator">The separator used in the CSV file.</param>
        /// <returns><paramref name="file"/>.</returns>
        private static CsvFile ParseInternal(CsvFile file, string contents, CsvSeparator separator = DefaultSeparator)
        {
            // Normalize line endings
            contents = contents.Replace("\r\n", "\n");
            contents = contents.Replace("\r", "\n");

            // Get the separator as a "char"
            char sep;

            switch (separator)
            {
            case CsvSeparator.Comma: sep = ','; break;

            case CsvSeparator.Colon: sep = ':'; break;

            case CsvSeparator.SemiColon: sep = ';'; break;

            case CsvSeparator.Space: sep = ' '; break;

            case CsvSeparator.Tab: sep = '\t'; break;

            default: sep = ';'; break;
            }

            // Parse each line into a list of cell values
            List <List <string> > lines = ParseLines(contents, sep);

            if (lines.Count == 0)
            {
                throw new CsvException("Invalid CSV file");
            }

            // If malformed, each line/row may not have the same amount of cells
            int maxColumns = lines.Max(x => x.Count);

            // Parse the columns (column headers)
            for (int c = 0; c < maxColumns; c++)
            {
                string name = lines[0].Skip(c).FirstOrDefault() ?? "";
                file.AddColumn(name);
            }

            // Parse the rows
            for (int r = 1; r < lines.Count; r++)
            {
                CsvRow row = file.AddRow();
                for (int c = 0; c < maxColumns; c++)
                {
                    CsvColumn column = file.Columns[c];
                    string    value  = lines[r].Skip(c).FirstOrDefault() ?? "";
                    row.AddCell(column, value);
                }
            }

            return(file);
        }
Exemple #3
0
        /// <summary>
        /// Adds a new cell with the specified <paramref name="value"/>.
        /// </summary>
        /// <param name="value">The value of the cell.</param>
        /// <returns>The added cell.</returns>
        public CsvCell AddCell(string value)
        {
            CsvColumn column = File.Columns[Cells.Length];

            return(Cells.AddCell(column, value));
        }
Exemple #4
0
 internal CsvCell AddCell(CsvColumn column, string value)
 {
     return(Cells.AddCell(column, value));
 }