Пример #1
0
        public void Export(IFileDialogService fileDialogService, string fileName, bool isDetailed, Encoding encoding = null)
        {
            if (fileDialogService is null)
            {
                throw new ArgumentNullException(nameof(fileDialogService));
            }

            var dialogSettings = new DialogSettings();

            dialogSettings.Title = "Сохранить";
            dialogSettings.DefaultFileExtention = ".csv";
            dialogSettings.FileName             = $"{fileName} {DateTime.Now:yyyy-MM-dd-HH-mm}.csv";

            var result = fileDialogService.RunSaveFileDialog(dialogSettings);

            if (!result.Successful)
            {
                return;
            }

            var writer = new StreamWriter(result.Path, false, encoding ?? Encoding.GetEncoding(1251));

            var csv = new CsvWriter(writer, CultureInfo.CurrentCulture);

            if (isDetailed)
            {
                var columnNames = Titles.Select(x => x.NomenclatureName).ToList();

                var headers = new[] { "Дата", "ТМЦ" }.Concat(columnNames).ToList();

                foreach (var header in headers)
                {
                    csv.WriteField(header);
                }

                csv.NextRecord();

                foreach (var row in ResultNodeList)
                {
                    csv.WriteField(row.MovementDocumentDate > DateTime.MinValue ? row.MovementDocumentDate.ToShortDateString() : string.Empty);
                    csv.WriteField(row.MovementDocumentName);

                    foreach (var column in row.NomenclatureColumns)
                    {
                        csv.WriteField(column.IsTotal? $"{ column.Amount:F2}" : $"{ column.Amount:F0}");
                    }

                    csv.NextRecord();
                }
            }
            else
            {
                var headers = new[] { "ТМЦ", "Период", "Цена", "Количество", "Сумма" };

                foreach (var header in headers)
                {
                    csv.WriteField(header);
                }

                csv.NextRecord();

                foreach (var row in ResultNodeList.SingleOrDefault().NomenclatureColumns)
                {
                    csv.WriteField(row.NomenclatureName);
                    csv.WriteField(row.DateRange);
                    csv.WriteField(row.IsTotal ? "" : row.PurchasePrice.ToString());
                    csv.WriteField(row.Amount);
                    csv.WriteField(row.Sum);
                    csv.NextRecord();
                }
            }

            writer.Close();
        }