Пример #1
0
        public bool Read(string file, FileShare fs = FileShare.None)
        {
            Rows = new List <Row>();

            if (!File.Exists(file))
            {
                return(false);
            }

            try
            {
                using (Stream s = File.Open(file, FileMode.Open, FileAccess.Read, fs))
                {
                    using (StreamReader sr = new StreamReader(s))
                    {
                        CSVRead csv = new CSVRead(sr);

                        CSVRead.State st;

                        Row l = new Row();

                        string str;
                        while ((st = csv.Next(out str)) != CSVRead.State.EOF)
                        {
                            l.Cells.Add(str);

                            if (st == CSVRead.State.ItemEOL)
                            {
                                Rows.Add(l);
                                l = new Row();
                            }
                        }

                        if (l.Cells.Count > 0)
                        {
                            Rows.Add(l);
                        }

                        return(true);
                    }
                }
            }
            catch
            {
                return(false);
            }
        }
Пример #2
0
        // read from TR with comma/semi selection
        // optionally send rows to rowoutput instead of storing
        public bool Read(TextReader tr,
                         bool commadelimit                    = true,                                   // true means us/uk dot and comma, else its the noncommacountry to select the format.
                         Action <int, Row> rowoutput          = null,
                         System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None, // if to allow thousands seperator etc
                         string noncommacountry               = "sv"                                    // space is the default thousands.
                         )
        {
            Rows = new List <Row>();

            System.Globalization.CultureInfo formatculture = new System.Globalization.CultureInfo(commadelimit ? "en-US" : noncommacountry);   // select format culture based on comma

            CSVRead csv = new CSVRead(tr);

            csv.SetCSVDelimiter(commadelimit);

            Row l = new Row(formatculture, ns);
            int r = 0;

            while (true)
            {
                var state = csv.Next(out string str);
                if (state == CSVRead.State.Item)
                {
                    l.Cells.Add(str);
                }
                else if (state == CSVRead.State.ItemEOL)
                {
                    l.Cells.Add(str);

                    if (rowoutput != null)
                    {
                        rowoutput.Invoke(r++, l);
                    }
                    else
                    {
                        Rows.Add(l);
                    }

                    l = new Row(formatculture, ns);
                }
                else if (state == CSVRead.State.EOF)
                {
                    if (l.Cells.Count > 0)
                    {
                        if (rowoutput != null)
                        {
                            rowoutput?.Invoke(r++, l);
                        }
                        else
                        {
                            Rows.Add(l);
                        }
                    }

                    return(true);
                }
                else
                {
                    return(false);       // error, end
                }
            }
        }