private async Task <List <ListItem> > GetWorksheets(int fileId, string fileName)
        {
            // Let's download this file
            Stream file = await HubCommunicator.DownloadFile(fileId);

            var fileBytes = ExcelUtils.StreamToByteArray(file);

            //TODO: Optimize this to retrieve spreadsheet list only. Now it reads and loads into memory whole file.
            var spreadsheetList = ExcelUtils.GetSpreadsheets(fileBytes, Path.GetExtension(fileName));

            return(spreadsheetList.Select(s => new ListItem()
            {
                Key = s.Key.ToString(), Value = s.Value
            }).ToList());
        }
예제 #2
0
        public override async Task Run()
        {
            var fileSelector = GetControl <DropDownList>("FileSelector");

            if (string.IsNullOrEmpty(fileSelector.Value))
            {
                RaiseError("No File was selected on design time", ActivityErrorCode.DESIGN_TIME_DATA_MISSING);
                return;
            }
            //let's download this file
            var file = await HubCommunicator.DownloadFile(int.Parse(fileSelector.Value));

            if (file == null || file.Length < 1)
            {
                RaiseError("Unable to download file from Hub");
                return;
            }

            string textRepresentation;

            using (var reader = new StreamReader(file, Encoding.UTF8))
            {
                textRepresentation = reader.ReadToEnd();
            }

            //let's convert this file to a string and store it in a file crate
            var fileDescription = new StandardFileDescriptionCM
            {
                Filename           = fileSelector.selectedKey,
                TextRepresentation = textRepresentation
            };
            var fileCrate = Crate.FromContent("DownloadFile", fileDescription);

            Payload.Add(fileCrate);
            Success();
        }
        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);
            }
        }