예제 #1
0
        public static ReportData LoadCSV(string csvData)
        {
            var csv = ExcelReportingCommon.ParseCSV(csvData);

            // * Поставки
            // * Страны
            // * Регионы
            // * Компании
            // * Продукция
            if (csv.Count != 5)
            {
                throw new ArgumentException();
            }

            var countries = csv[1].ToDictionary(ss => ss[0], ss => ss[1]);
            var regions   = csv[2].ToDictionary(ss => ss[0], ss => ss[1]);

            ReportData result = new ReportData();

            result.References = new ReportDataReferences();
            result.References.Companies.AddRange(csv[3].Select(x => new ReportDataReferencesCompany {
                id = byte.Parse(x[0]), name = x[1]
            }));
            result.References.Products.AddRange(csv[4].Select(x => new ReportDataReferencesProduct {
                id = byte.Parse(x[0]), name = x[1]
            }));

            foreach (var gCountry in csv[0].GroupBy(x => x[0]))
            {
                ReportDataCountry country = new ReportDataCountry {
                    name = countries[gCountry.Key]
                };
                result.Data.Add(country);
                foreach (var gRegion in gCountry.GroupBy(x => x[1]))
                {
                    ReportDataCountryRegion region = new ReportDataCountryRegion {
                        name = regions[gRegion.Key]
                    };
                    country.Region.Add(region);
                    foreach (var gCompany in gRegion.GroupBy(x => x[2]))
                    {
                        ReportDataCountryRegionCompany company = new ReportDataCountryRegionCompany {
                            companyId = byte.Parse(gCompany.Key)
                        };
                        region.Company.Add(company);
                        company.Sale.AddRange(gCompany.Select(x =>
                                                              new ReportDataCountryRegionCompanySale
                        {
                            //countryId;regionId;companyId;productId;contract;date;sum
                            productId = byte.Parse(x[3]),
                            contract  = x[4],
                            date      = DateTime.ParseExact(x[5], "yyyy-MM-dd", ruCultureInfo),
                            sum       = uint.Parse(x[6])
                        }));
                    }
                }
            }

            return(result);
        }
예제 #2
0
        public void ExcelReportingCommon_ParseCSV()
        {
            string csvData =
                @"

// * Страны
//countryId;name
1;Россия
2;Беларусь

// * Регионы
//regionId;name
0;
1;Москва
2;Московская область

// * Компании
";
            var csv = ExcelReportingCommon.ParseCSV(csvData);

            Assert.IsNotNull(csv);
        }