示例#1
0
        public static CsvData LoadFrom(string path, int numRowsToSkip = 2)
        {
            var resultOb    = new CsvData();
            int lineIdx     = 0;
            int skipCounter = numRowsToSkip;

            using (StreamReader reader = new StreamReader(path))
            {
                string header  = reader.ReadLine();
                int    numCols = header.Split(',').Length;
                lineIdx++;

                while (!reader.EndOfStream)
                {
                    string row = reader.ReadLine();
                    lineIdx++;

                    if (skipCounter > 0)
                    {
                        skipCounter--;
                        continue;
                    }

                    if (row.Length > 0)
                    {
                        // simple multiline check - expect to see even number of " chars, concat next row if not
                        int numStringSep = CountStringSep(row);
                        while ((numStringSep % 2) != 0)
                        {
                            string nextRow = reader.ReadLine();
                            lineIdx++;

                            row         += " " + nextRow;
                            numStringSep = CountStringSep(row);
                        }

                        string[] cols = SplitRow(row);
                        if (cols.Length != numCols)
                        {
                            throw new Exception("Column count mismatch at '" + path + "' line:" + lineIdx);
                        }

                        resultOb.rows.Add(cols);
                    }
                }
            }

            return(resultOb);
        }