Ejemplo n.º 1
0
        public FileReaderResult Read(String fullFilepath, IList <FileReaderMap> maps, Boolean firstRowAreTitles = true)
        {
            if (String.IsNullOrEmpty(fullFilepath))
            {
                throw new ArgumentException("fullFilepath");
            }
            if (!File.Exists(fullFilepath))
            {
                throw new InvalidOperationException("File's not found.");
            }
            if (maps == null)
            {
                throw new ArgumentException("maps");
            }
            if (!maps.Any())
            {
                throw new InvalidOperationException("File Reader Map must be supplied.");
            }

            var result = new FileReaderResult();

            String[] allLines = File.ReadAllLines(fullFilepath);
            for (int row = 1; row <= allLines.Length; row++)
            {
                String currentLine = allLines[row - 1];
                Int32  lineLength  = currentLine.Length;
                for (int col = 1; col <= maps.Count; col++)
                {
                    FileReaderMap currentMap = maps[col - 1];
                    if (currentMap.StartAt >= lineLength)
                    {
                        throw new IndexOutOfRangeException("currentMap.StartAt");
                    }

                    String data    = currentLine.Substring(currentMap.StartAt, currentMap.Length);
                    String cleaned = String.IsNullOrEmpty(data) ? data : data.Trim();
                    if (firstRowAreTitles && row == 1)
                    {
                        result.Titles.Add(cleaned);
                    }
                    else
                    {
                        result.Data.Add(new GridData {
                            Row = row, Column = currentMap.StartAt, CellValue = cleaned
                        });
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public static IList <GridData> CleanupRawData(this FileReaderResult rawData)
        {
            IList <GridData> cleanupColumn = rawData.Data;

            if (rawData.Titles != null && rawData.Titles.Any() && !rawData.Titles.All(ttl => String.IsNullOrEmpty(ttl)))
            {
                cleanupColumn = rawData
                                .Titles
                                .Select((ttl, idx) => new { Ordinal = idx + 1, Item = ttl })
                                .Where(ttl => !String.IsNullOrEmpty(ttl.Item))
                                .Join(rawData.Data, o => o.Ordinal, i => i.Column, (o, i) => i)
                                .ToList();
            }

            return(cleanupColumn
                   .GroupBy(prm => prm.Row)
                   .Select(grp => grp.Select(item => item))
                   .Where(grd => !grd.Aggregate(true, (p, n) => p && String.IsNullOrEmpty(n.CellValue)))
                   .SelectMany(grd => grd)
                   .ToList());
        }
Ejemplo n.º 3
0
        public FileReaderResult Read(String fullFilepath, IList <FileReaderMap> maps, Boolean firstRowAreTitles = true)
        {
            if (String.IsNullOrEmpty(fullFilepath))
            {
                throw new ArgumentException("fullFilepath");
            }
            if (!File.Exists(fullFilepath))
            {
                throw new InvalidOperationException("File's not found.");
            }

            var result = new FileReaderResult();

            using (var doc = new SLDocument(fullFilepath))
            {
                doc.SelectWorksheet(doc.GetCurrentWorksheetName());
                var stats = doc.GetWorksheetStatistics();
                for (int row = 1; row <= stats.EndRowIndex; row++)
                {
                    for (int col = 1; col <= stats.EndColumnIndex; col++)
                    {
                        String data    = doc.GetCellValueAsString(row, col);
                        String cleaned = String.IsNullOrEmpty(data) ? data : data.Trim();
                        if (firstRowAreTitles && row == 1)
                        {
                            result.Titles.Add(cleaned);
                        }
                        else
                        {
                            result.Data.Add(new GridData {
                                Row = row, Column = col, CellValue = cleaned
                            });
                        }
                    }
                }
            }

            return(result);
        }
 public Task OnFileReaderResult(FileReaderResult result)
 => _formManager.OnFileReaderResultAsync(result);