Exemple #1
0
        public bool Parse()
        {
            _values = new Dictionary <IKey, IDictionary <string, string> >();

            try
            {
                // Validations
                if (string.IsNullOrEmpty(FilePath))
                {
                    throw new Exception("No origin file path specified");
                }
                if (!File.Exists(FilePath))
                {
                    throw new Exception("Can not acces to origin file path");
                }
                if (string.IsNullOrEmpty(SheetName))
                {
                    throw new Exception("No origin sheet name specified");
                }
                if (Columns == null || !Columns.Any())
                {
                    throw new Exception("No origin columns specified");
                }
                if (KeysColumns == null || !KeysColumns.Any())
                {
                    throw new Exception("No origin keys specified");
                }

                NotifyIsBusy(true, "Parsing origin file");
                using (var xlPackage = new ExcelPackage(new FileInfo(FilePath)))
                {
                    var excelWorksheet = xlPackage.Workbook.Worksheets.FirstOrDefault(x => string.Equals(x.Name, SheetName, StringComparison.OrdinalIgnoreCase));
                    if (excelWorksheet == null)
                    {
                        throw new Exception("No origin worksheet found");
                    }

                    var totalRows = excelWorksheet.Dimension.End.Row;
                    for (var rowNum = 2; rowNum <= totalRows; rowNum++)
                    {
                        var key = IoC.Get <IKey>();

                        try
                        {
                            foreach (var keyColumn in KeysColumns)
                            {
                                key.AddKeyValue(excelWorksheet.Cells[keyColumn.Origin + rowNum].GetValue <string>());
                            }

                            if (!_values.ContainsKey(key))
                            {
                                _values.Add(key, new Dictionary <string, string>());
                            }

                            foreach (var column in Columns)
                            {
                                _values[key].Add(column.Origin, excelWorksheet.Cells[column.Origin + rowNum].GetValue <string>());
                            }
                        }
                        catch (Exception)
                        {
                            if (key != null && _values.ContainsKey(key))
                            {
                                _values.Remove(key);
                            }
                        }
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                NotifyException(ex);
                return(false);
            }
            finally
            {
                NotifyIsBusy(false, string.Empty);
            }
        }
        public bool Process(IDictionary <IKey, IDictionary <string, string> > values)
        {
            try
            {
                // Validations
                if (string.IsNullOrEmpty(FilePath))
                {
                    throw new Exception("No destiny file path specified");
                }
                if (!File.Exists(FilePath))
                {
                    throw new Exception("Can not access to destiny file path");
                }
                if (string.IsNullOrEmpty(SheetName))
                {
                    throw new Exception("No destiny sheet name specified");
                }
                if (Columns == null || !Columns.Any())
                {
                    throw new Exception("No destiny columns specified");
                }
                if (KeysColumns == null || !KeysColumns.Any())
                {
                    throw new Exception("No destiny keys specified");
                }
                if (values == null)
                {
                    throw new Exception("No origin data read");
                }

                NotifyIsBusy(true, "Parsing origin file");
                using (var xlPackage = new ExcelPackage(new FileInfo(FilePath)))
                {
                    var excelWorksheet = xlPackage.Workbook.Worksheets.FirstOrDefault(x => string.Equals(x.Name, SheetName, StringComparison.OrdinalIgnoreCase));
                    if (excelWorksheet == null)
                    {
                        throw new Exception("No destiny worksheet found");
                    }

                    var totalRows = excelWorksheet.Dimension.End.Row;
                    for (var rowNum = 2; rowNum <= totalRows; rowNum++)
                    {
                        var key = IoC.Get <IKey>();
                        try
                        {
                            foreach (var keyColumn in KeysColumns)
                            {
                                key.AddKeyValue(excelWorksheet.Cells[keyColumn.Destiny + rowNum].GetValue <string>());
                            }
                        }
                        catch (Exception)
                        {
                            continue;
                        }

                        if (!values.ContainsKey(key))
                        {
                            continue;
                        }

                        foreach (var column in Columns)
                        {
                            var columnsData = values[key];
                            if (!columnsData.ContainsKey(column.Origin))
                            {
                                continue;
                            }

                            var originValue = columnsData[column.Origin];
                            if (string.IsNullOrEmpty(originValue))
                            {
                                continue;
                            }

                            excelWorksheet.SetValue(column.Destiny + rowNum, originValue);
                        }
                    }

                    xlPackage.Save();
                }

                return(true);
            }
            catch (Exception ex)
            {
                NotifyException(ex);
                return(false);
            }
            finally
            {
                NotifyIsBusy(false, string.Empty);
            }
        }