public ContactLoadResult Load(Stream stream)
        {
            var textReader = new StreamReader(stream);
            var csv        = new CsvReader(textReader);

            csv.Configuration.RegisterClassMap <ContactCsvMap>();

            var fileLoadResult = new ContactLoadResult();

            try
            {
                var fileData = csv.GetRecords <FileUploadContact>().ToList();
                fileLoadResult.Data = fileData;
            }
            catch (ReaderException re)
            {
                fileLoadResult.Error = $"{FailedToImportMessage} {re.Message} {re.InnerException?.Message}";
            }
            catch (ValidationException ve)
            {
                fileLoadResult.Error = $"{FailedToImportMessage} {ve.Message} {ve.InnerException?.Message}";
            }
            catch (BadDataException bde)
            {
                fileLoadResult.Error = $"{FailedToImportMessage} {bde.Message} {bde.InnerException?.Message}";
            }

            return(fileLoadResult);
        }
Exemple #2
0
        public ContactLoadResult Load(Stream stream)
        {
            var fileLoadResult     = new ContactLoadResult();
            var fileUploadContacts = new List <FileUploadContact>();

            using (var document = SpreadsheetDocument.Open(stream, false))
            {
                var workbookPart   = document.WorkbookPart;
                var sheets         = workbookPart.Workbook.GetFirstChild <Sheets>().Elements <Sheet>();
                var relationshipId = sheets.First().Id.Value;
                var worksheetPart  = (WorksheetPart)document.WorkbookPart.GetPartById(relationshipId);
                var workSheet      = worksheetPart.Worksheet;
                var sheetData      = workSheet.GetFirstChild <SheetData>();
                var rows           = sheetData.Descendants <Row>().ToList();
                rows.RemoveAt(0);

                foreach (var row in rows)
                {
                    var fileUploadContact = CreateContact(document, row);
                    if (fileUploadContact != null)
                    {
                        fileUploadContacts.Add(fileUploadContact);
                    }
                }

                fileLoadResult.Data = fileUploadContacts;

                return(fileLoadResult);
            }
        }
        public CsvLoadedSuccess()
        {
            var fileReader = new CsvContactFileReader();

            using (var stream = File.Open(DataFilePath, FileMode.Open))
            {
                _loadResult  = fileReader.Load(stream);
                _firstRecord = _loadResult.Data[0];
            }
        }