/// <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);
        }
Example #2
0
        /// <summary>
        /// Internal helper method for parsing the contents of a CSV file.
        /// </summary>
        /// <param name="file"></param>
        /// <param name="contents"></param>
        /// <param name="separator"></param>
        /// <returns><paramref name="file"/>.</returns>
        private static CsvFile ParseInternal(CsvFile file, string contents, CsvSeparator separator = CsvSeparator.SemiColon)
        {
            // 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 Exception("WTF?");
            }

            // 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);
        }
        /// <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));
        }
 internal CsvCell AddCell(CsvColumn column, string value)
 {
     return(Cells.AddCell(column, value));
 }