Reputation: 1
I tried to edit a file on Ondrive via Microsoft Graph by downloading it for editing then uploading it again.
In the xlsx download section, I encountered this error when putting the "stream" variable into IWorkbook.
"System.ArgumentException: 'Update mode requires a stream with read, write, and seek capabilities.'"
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);
And in the upload section, I tried to load a file from the file picker to IWorkbook, then save as Stream , finally successfully uploaded 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);
}
What I need is to edit the xlsx file on Ondrive using Syncfusion.XlsIO.UWP from the stream. Can anyone help me? Thanks a lot.
Upvotes: 0
Views: 257
Reputation: 106
Query-1: "System.ArgumentException: 'Update mode requires a stream with read, write, and seek capabilities.'" for this code below.
Answer: We request you to copy the stream to MemoryStream and set stream.Postion = 0; before loading the file into IWorkbook, to resolve the issue.
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);
Query-2: I tried to upload a file successfully but it was empty.
Answer: We request you to set the stream position as 0 here also, before uploading the file to resolve the issue.
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.
Sample Link: https://www.syncfusion.com/downloads/support/forum/148443/ze/03-add-msgraph1181232979.zip
Note: Please add the ApplicationID and use valid file name then execute the sample.
Upvotes: 1