Exemple #1
0
        public static CsvData FromObject(object obj, CultureInfo cultureInfo)
        {
            var columns = obj.GetType().GetProperties()
                          .Select(prop => prop.Name)
                          .ToList();

            var csvData = new CsvData(-1, columns, "");

            foreach (var propertyInfo in obj.GetType().GetProperties())
            {
                var columnIndex = csvData.ColumnNames
                                  .FindIndex(c => string.Compare(c, propertyInfo.Name, StringComparison.InvariantCultureIgnoreCase) == 0);
                if (columnIndex == -1)
                {
                    continue;
                }

                var value = UniversalConverter.ConvertToType(propertyInfo.GetValue(obj), typeof(string), cultureInfo)?.ToString() ?? "";
                csvData.Fields.Add(value);
            }

            return(csvData);
        }
Exemple #2
0
        public bool Load()
        {
            LoadError = string.Empty;

            if (!File.Exists(FileName))
            {
                LoadError = "File does not exist.";
                return(false);
            }

            LastFileChange = File.GetLastWriteTime(FileName);
            DataRows       = new List <CsvData>();
            try
            {
                var lineNumber = 0;

                using var fs       = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                using var fileData = new StreamReader(fs, FileEncoding, true);

                while (!fileData.EndOfStream)
                {
                    lineNumber++;
                    var lineData = fileData.ReadLine();
                    if (string.IsNullOrEmpty(lineData))
                    {
                        continue;
                    }

                    if (lineNumber == 1)
                    {
                        DetectSeparator(lineData);
                        Columns.Clear();
                        var columns = lineData
                                      .Split(Separator)
                                      .Select(TextEscaping.RemoveQuotes);
                        Columns.AddRange(columns);
                        continue;
                    }

                    if (RemoveQuotes)
                    {
                        while (!fileData.EndOfStream)
                        {
                            var quotes1 = lineData.ToCharArray().Count(ch => ch == '"');
                            var quotes2 = lineData.ToCharArray().Count(ch => ch == '\'');
                            if (quotes1 == 0 && quotes2 == 0)
                            {
                                break;
                            }
                            if (quotes1 > 0 && (quotes1 & 1) == 0)
                            {
                                break;
                            }
                            if (quotes2 > 0 && (quotes2 & 1) == 0)
                            {
                                break;
                            }

                            lineData += fileData.ReadLine();
                        }
                    }

                    var data   = new CsvData(lineNumber, Columns, lineData);
                    var fields = lineData.Split(Separator);
                    if (RemoveQuotes)
                    {
                        fields = fields
                                 .Select(TextEscaping.RemoveQuotes)
                                 .ToArray();
                    }
                    data.Fields.AddRange(fields);
                    if (data.Fields.Count == Columns.Count)
                    {
                        DataRows.Add(data);
                    }
                    else
                    {
                        LoadError = "Invalid rows detected.";
                        InvalidRows.Add(data);
                    }
                }

                FileEncoding = fileData.CurrentEncoding;
                fileData.Close();
                return(true);
            }
            catch (IOException ex)
            {
                LoadError = ex.Message;
                Trace.TraceError(ex.Message);
            }
            return(false);
        }