private async Task <string> AppendOrCreateSpreadsheet(StandardTableDataCM tableToSave) { byte[] fileData; string fileName; if (ActivityUI.UseNewSpreadsheetOption.Selected) { fileData = ExcelUtils.CreateExcelFile( tableToSave, ActivityUI.NewWorksheetName.Value ); fileName = ActivityUI.NewSpreadsheetName.Value; } else { var existingFileStream = await HubCommunicator.DownloadFile( int.Parse(ActivityUI.ExistingSpreadsheetsList.Value) ); byte[] existingFileBytes; using (var memStream = new MemoryStream()) { await existingFileStream.CopyToAsync(memStream); existingFileBytes = memStream.ToArray(); } fileName = ActivityUI.ExistingSpreadsheetsList.selectedKey; var worksheetName = ActivityUI.UseNewWorksheetOption.Selected ? ActivityUI.NewWorksheetName.Value : ActivityUI.ExistingWorksheetsList.selectedKey; StandardTableDataCM dataToInsert; if (ActivityUI.UseExistingWorksheetOption.Selected || ActivityUI.ExistingWorksheetsList.ListItems.Any(x => x.Key == ActivityUI.NewWorksheetName.Value)) { var existingData = _excelUtils.GetExcelFile(existingFileBytes, fileName, true, worksheetName); StandardTableDataCMTools.AppendToStandardTableData(existingData, tableToSave); dataToInsert = existingData; } else { dataToInsert = tableToSave; } fileData = ExcelUtils.RewriteSheetForFile( existingFileBytes, dataToInsert, worksheetName ); } using (var stream = new MemoryStream(fileData, false)) { if (!fileName.ToUpper().EndsWith(".XLSX")) { fileName += ".xlsx"; } var file = await HubCommunicator.SaveFile(fileName, stream); Payload.Add(Crate.FromContent("StoredFile", new StandardFileDescriptionCM { Filename = file.Id.ToString(), // dirty hack TextRepresentation = file.OriginalFileName, // another hack Filetype = ".xlsx" })); return(file.CloudStorageUrl); } }