public void DownloadCsv()
        {
            var newFileName = ApplicationController.GetSaveFileName(string.Format("{0}", RecordService.GetCollectionName(RecordType)), "csv");

            ApplicationController.DoOnAsyncThread(() =>
            {
                try
                {
                    LoadingViewModel.IsLoading = true;
                    if (!string.IsNullOrWhiteSpace(newFileName))
                    {
                        var folder   = Path.GetDirectoryName(newFileName);
                        var fileName = Path.GetFileName(newFileName);
                        var fields   = FieldMetadata.ToArray();
                        var started  = DateTime.UtcNow;

                        var labelFuncDictionary = new Dictionary <string, Func <string, string> >();
                        foreach (var fm in FieldMetadata)
                        {
                            labelFuncDictionary.Add(fm.AliasedFieldName ?? fm.FieldName, s => fm.OverrideLabel ?? RecordService.GetFieldLabel(fm.FieldName, fm.AltRecordType ?? RecordType));
                        }

                        var displayFuncDictionary = new Dictionary <string, Func <object, string, string> >();
                        foreach (var fm in FieldMetadata)
                        {
                            displayFuncDictionary.Add(fm.AliasedFieldName ?? fm.FieldName, (o, s) => RecordService.GetFieldAsDisplayString(fm.AltRecordType ?? RecordType, fm.FieldName, ((IRecord)o).GetField(s)));
                        }

                        CsvUtility.CreateCsv(folder, fileName, GetGridRecords(true).Records,
                                             fields.Select(f => f.AliasedFieldName ?? f.FieldName),
                                             (f) => labelFuncDictionary[f](f),
                                             (r, f) => displayFuncDictionary[f](r, f));


                        ApplicationController.LogEvent("Download CSV", new Dictionary <string, string>
                        {
                            { "Is Completed Event", true.ToString() },
                            { "Type", RecordType },
                            { "Seconds Taken", (DateTime.UtcNow - started).TotalSeconds.ToString() },
                        });
                        ApplicationController.StartProcess("explorer.exe", "/select, \"" + Path.Combine(folder, fileName) + "\"");
                    }
                }
                catch (Exception ex)
                {
                    ApplicationController.ThrowException(ex);
                }
                finally
                {
                    LoadingViewModel.IsLoading = false;
                }
            });
        }
Ejemplo n.º 2
0
        private void DownloadSpreadsheet(bool csv = false)
        {
            var extension   = csv ? "csv" : "xlsx";
            var newFileName = ApplicationController.GetSaveFileName(string.Format("{0}", RecordService.GetCollectionName(RecordType)), extension);

            ApplicationController.DoOnAsyncThread(() =>
            {
                try
                {
                    LoadingViewModel.IsLoading = true;
                    if (!string.IsNullOrWhiteSpace(newFileName))
                    {
                        var folder   = Path.GetDirectoryName(newFileName);
                        var fileName = Path.GetFileName(newFileName);
                        var fields   = FieldMetadata.ToArray();
                        var started  = DateTime.UtcNow;

                        var labelFuncDictionary = new Dictionary <string, Func <string, string> >();
                        foreach (var fm in FieldMetadata)
                        {
                            labelFuncDictionary.Add(fm.AliasedFieldName ?? fm.FieldName, s => fm.OverrideLabel ?? RecordService.GetFieldLabel(fm.FieldName, fm.AltRecordType ?? RecordType));
                        }

                        var displayFuncDictionary = new Dictionary <string, Func <object, string, string> >();
                        foreach (var fm in FieldMetadata)
                        {
                            displayFuncDictionary.Add(fm.AliasedFieldName ?? fm.FieldName, (o, s) => RecordService.GetFieldAsDisplayString(fm.AltRecordType ?? RecordType, fm.FieldName, ((IRecord)o).GetField(s)));
                        }

                        if (csv)
                        {
                            CsvUtility.CreateCsv(folder, fileName, GetGridRecords(true).Records,
                                                 fields.Select(f => f.AliasedFieldName ?? f.FieldName),
                                                 (f) => labelFuncDictionary[f](f),
                                                 (r, f) => displayFuncDictionary[f](r, f));
                        }
                        else
                        {
                            var sheetName      = "Records";
                            var excelCellTypes = new Dictionary <string, CellDataType>();
                            foreach (var field in fields)
                            {
                                var fieldType = field.AltRecordType != null
                                    ? RecordService.GetFieldType(field.FieldName, field.AltRecordType)
                                    : RecordService.GetFieldType(field.FieldName, RecordType);
                                if (fieldType == RecordFieldType.BigInt || fieldType == RecordFieldType.Decimal || fieldType == RecordFieldType.Double || fieldType == RecordFieldType.Integer || fieldType == RecordFieldType.Money)
                                {
                                    excelCellTypes.Add(field.AliasedFieldName ?? field.FieldName, CellDataType.Number);
                                }
                                else if (fieldType == RecordFieldType.Date)
                                {
                                    excelCellTypes.Add(field.AliasedFieldName ?? field.FieldName, CellDataType.Date);
                                }
                                else
                                {
                                    excelCellTypes.Add(field.AliasedFieldName ?? field.FieldName, CellDataType.String);
                                }
                            }

                            ExcelUtility.CreateXlsx(folder, fileName,
                                                    new Dictionary <string, IEnumerable>()
                            {
                                { sheetName, GetGridRecords(true).Records }
                            },
                                                    new Dictionary <string, IEnumerable <string> >()
                            {
                                { sheetName, fields.Select(f => f.AliasedFieldName ?? f.FieldName) }
                            },
                                                    new Dictionary <string, Func <object, string, object> >()
                            {
                                { sheetName, (r, f) => displayFuncDictionary[f](r, f) }
                            },
                                                    new Dictionary <string, Func <string, string> >()
                            {
                                { sheetName, (f) => labelFuncDictionary[f](f) }
                            },
                                                    new Dictionary <string, Func <string, CellDataType> >()
                            {
                                { sheetName, (f) => excelCellTypes[f] }
                            });
                        }
                        ApplicationController.LogEvent("Download " + extension.ToUpper(), new Dictionary <string, string>
                        {
                            { "Is Completed Event", true.ToString() },
                            { "Type", RecordType },
                            { "Seconds Taken", (DateTime.UtcNow - started).TotalSeconds.ToString() },
                        });
                        ApplicationController.StartProcess("explorer.exe", "/select, \"" + Path.Combine(folder, fileName) + "\"");
                    }
                }
                catch (Exception ex)
                {
                    ApplicationController.ThrowException(ex);
                }
                finally
                {
                    LoadingViewModel.IsLoading = false;
                }
            });
        }