We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Edit file xlsx from Onedrive

I tried to edit a file on Ondrive via Microsoft Graph by downloading it for editing then uploading it again.
I encountered 2 problems as follows

"System.ArgumentException: 'Update mode requires a stream with read, write, and seek capabilities.'" for this code below.

ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;         
application.UseFastRecordParsing = true;
var stream = await graphClient.Me.Drive.Items["F90FCCBAC810EFDB!41667"].Content
                                                                       .Request()
                                                                       .GetAsync();
IWorkbook workbook = await application.Workbooks.OpenAsync(stream);


I tried to upload a file successfully but it was empty.

using (ExcelEngine excelEngine = new ExcelEngine())
            {
                FileOpenPicker openPicker = new FileOpenPicker();
                openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
                openPicker.FileTypeFilter.Add(".xlsx");
                openPicker.FileTypeFilter.Add(".xls");
                StorageFile inputStorageFile = await openPicker.PickSingleFileAsync();
                Stream fileStream = (await inputStorageFile.OpenReadAsync()).AsStreamForRead();
                IWorkbook workbook = await excelEngine.Excel.Workbooks.OpenAsync(fileStream);
                workbook.Version = ExcelVersion.Excel2016;
                MemoryStream outputStream = new MemoryStream();
                await workbook.SaveAsAsync(outputStream);
                await graphClient.Me.Drive.Root.ItemWithPath(inputStorageFile.Name).Content
                .Request()
                .PutAsync<DriveItem>(outputStream);
            }

So how to solve these problems?

1 Reply

KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team October 23, 2019 01:48 PM UTC

Hi Phan, 

Greetings from Syncfusion. 

We have tried to reproduce the mentioned issues and found solution to resolve them. Please find the details below. 

Query 
Answer 
"System.ArgumentException: 'Update mode requires a stream with read, write, and seek capabilities.'" for this code below. 
We request you to copy the stream to MemoryStream and set stream.Postion = 0; before loading the file into IWorkbook, to resolve the issue. 

Code Snippet:  

ExcelEngine excelEngine = new ExcelEngine(); 
IApplication application = excelEngine.Excel; 
application.UseFastRecordParsing = true; 
 
var stream = await graphClient.Me.Drive.Root.ItemWithPath("Sample.xlsx").Content.Request().GetAsync(); 
stream.Position = 0; 
 
MemoryStream file = new MemoryStream(); 
stream.CopyTo(file); 
file.Position = 0; 
 
IWorkbook workbook = await application.Workbooks.OpenAsync(file); 
 
I tried to upload a file successfully but it was empty. 
We request you to set the stream position as 0 here also, before uploading the file to resolve the issue. 

Code Snippet: 

outputStream.Position = 0; 
await graphClient.Me.Drive.Root.ItemWithPath("Sample.xlsx").Content.Request().PutAsync<DriveItem>(outputStream); 
 

The sample we have tried at our end can be downloaded from the following link. 


Note: Please add the ApplicationID and use valid file name then execute the sample. 

Kindly try this and let us know if the issue is resolved. 

Regards, 
Keerthi. 


Loader.
Live Chat Icon For mobile
Up arrow icon