Пример #1
0
        private static void ReadCsvAsStrings(TextReader reader, out NamedList <string>[] columns, out DataAdaptor[] headers)
        {
            Debug.Assert(reader != null);

            // Get the column names from the first line.

            string firstline = reader.ReadLine();

            if (firstline == null)
            {
                columns = null;
                headers = null;
                return;
            }

            List <string> names = CsvHelper.ReadCells(firstline);
            int           count = names.Count;

            // Put the columns into lists of strings, and as we do so, maintain the collection of
            // types it can be parsed into, and whether any entries are null.

            columns = new NamedList <string> [names.Count];
            headers = new DataAdaptor[names.Count];
            for (int columnIndex = 0; columnIndex < columns.Length; columnIndex++)
            {
                columns[columnIndex] = new NamedList <string>(names[columnIndex]);
                headers[columnIndex] = new DataAdaptor();
            }

            while (true)
            {
                string line = reader.ReadLine();
                if (line == null)
                {
                    break;
                }
                List <string> cells = CsvHelper.ReadCells(line);
                if (cells.Count != count)
                {
                    throw new FormatException();
                }
                for (int columnIndex = 0; columnIndex < count; columnIndex++)
                {
                    string cell = cells[columnIndex];
                    if (String.IsNullOrEmpty(cell))
                    {
                        headers[columnIndex].IsNullable = true;
                        columns[columnIndex].Add(null);
                    }
                    else
                    {
                        DataAdaptor header = headers[columnIndex];
                        header.TryParse(cell);
                        columns[columnIndex].Add(cell);
                    }
                }
            }
        }
Пример #2
0
        // CSV output method

        /// <summary>
        /// Write the data in the view to a comma-seperated-value file.
        /// </summary>
        /// <param name="writer">A writer to accept the data.</param>
        public void WriteCsv(TextWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            string[] columnNames = new string[this.columns.Count];
            for (int c = 0; c < columns.Count; c++)
            {
                columnNames[c] = columns[c].Name;
            }
            writer.WriteLine(CsvHelper.WriteCells(columnNames));
            foreach (DataRow row in this.Rows)
            {
                writer.WriteLine(CsvHelper.WriteCells(row));
            }
        }
Пример #3
0
        private static void ReadCsvAsStrings(TextReader reader, out DataList <string>[] columns, out DataAdaptor[] headers)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            string firstline = reader.ReadLine();

            if (firstline == null)
            {
                columns = null;
                headers = null;
                return;
            }

            List <string> names = CsvHelper.ReadCells(firstline);
            int           count = names.Count;

            columns = new DataList <string> [names.Count];
            headers = new DataAdaptor[names.Count];
            for (int columnIndex = 0; columnIndex < columns.Length; columnIndex++)
            {
                columns[columnIndex] = new DataList <string>(names[columnIndex]);
                headers[columnIndex] = new DataAdaptor();
            }

            while (true)
            {
                string line = reader.ReadLine();
                if (line == null)
                {
                    break;
                }
                List <string> cells = CsvHelper.ReadCells(line);
                if (cells.Count != count)
                {
                    throw new FormatException();
                }
                for (int columnIndex = 0; columnIndex < count; columnIndex++)
                {
                    string cell = cells[columnIndex];
                    if (String.IsNullOrEmpty(cell))
                    {
                        headers[columnIndex].IsNullable = true;
                        columns[columnIndex].Add(null);
                    }
                    else
                    {
                        DataAdaptor header = headers[columnIndex];
                        LinkedListNode <TypeAdaptor> adaptorNode = header.TypeCandidates.First;
                        while (adaptorNode != null)
                        {
                            if (!adaptorNode.Value.IsParsable(cell))
                            {
                                header.TypeCandidates.Remove(adaptorNode);
                            }
                            adaptorNode = adaptorNode.Next;
                        }
                        columns[columnIndex].Add(cell);
                    }
                }
            }
        }