public override void ExecuteExtention(CustomisationExporterRequest request, CustomisationExporterResponse response, ServiceRequestController controller) { response.Folder = request.SaveToFolder.FolderPath; controller.LogLiteral("Loading Metadata"); ProcessForEntities(request, response, controller.Controller); if ((request.Fields || request.FieldOptionSets) && request.IncludeAllRecordTypes) { controller.UpdateProgress(0, 1, "Loading All Fields....."); Service.LoadFieldsForAllEntities(); } if ((request.Fields || request.Relationships) && request.IncludeAllRecordTypes) { controller.UpdateProgress(0, 1, "Loading All Relationships....."); Service.LoadRelationshipsForAllEntities(); } ProcessForFields(request, response, controller.Controller); ProcessForRelationships(request, response, controller.Controller); ProcessForOptionSets(request, response, controller.Controller); response.Folder = request.SaveToFolder.FolderPath; if (request.Format == CustomisationExporterRequest.FileFormat.Xlsx) { var excelFileName = "Customisation Export " + DateTime.Now.ToFileTime() + ".xlsx"; ExcelUtility.CreateXlsx(request.SaveToFolder.FolderPath, excelFileName, response.GetListsToOutput()); response.ExcelFileName = excelFileName; } else { foreach (var item in response.GetListsToOutput()) { var csvName = item.Key + " " + DateTime.Now.ToFileTime() + ".csv"; CsvUtility.CreateCsv(request.SaveToFolder.FolderPath, csvName, item.Value); } } response.Message = "The Export is Complete"; }
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; } }); }