Category / Section
How to upload/download excel files to/from OneDrive using Windows Phone application?
3 mins read
XlsIO generated files can be uploaded to OneDrive and download it back from OneDrive within a Windows Phone application. The following assemblies are required Here is the code snippet for upload and download excel file from one drive using Windows Phone. to achieve this.
- Microsoft.Live.Controls.dll
- and Microsoft.Live.dll
- Syncfusion.Compression.WP8
- Syncfusion.XlsIO.WP8
are required to access the one drive.The following code snippet illustrates on how to achieve this behavior in Windows Phone.
C#
MainPage.xaml private async void BtnGenerateAndUpload_Click(object sender, RoutedEventArgs e) { using (ExcelEngine x1 = new ExcelEngine()) { IApplication x1App = x1.Excel; IWorkbook book = x1App.Workbooks.Create(1); IWorksheet sheet = book.Worksheets.Create("new"); sheet.Range["A1"].Text = "Testing"; book.Version = ExcelVersion.Excel2013; try { using (IsolatedStorageFileStream outStream = new IsolatedStorageFileStream("Sample.xlsx", FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication())) { await book.SaveAsAsync(outStream); book.Close(); } skydrive = new SkyDriveHandler(App.LiveSession, "000000004010FA0B", "InvoiceGenerater"); if (await skydrive.UploadFile("Sample.xlsx") == SampleBrowser.Showcase.Invoice.SkyDriveHandler.OperationStatus.Completed) MessageBox.Show("Success"); else MessageBox.Show(@"Error while uploading file..! Please try again"); } catch { } } } private async void ButtonBtnDownload_Click(object sender, RoutedEventArgs e) { skydrive = new SkyDriveHandler(App.LiveSession, "000000004010FA0B", "InvoiceGenerater"); await skydrive.downloadFile("CheckBookSample.xlsx"); if(skydrive.FileStream != null) { using (ExcelEngine x1 = new ExcelEngine()) { IApplication x1App = x1.Excel; IWorkbook book = await x1App.Workbooks.OpenAsync(skydrive.FileStream); IWorksheet sheet = book.Worksheets["Sheet1"]; book.Close(); MessageBox.Show("Success"); } } else MessageBox.Show(@"Error while downloading the file..! Please try again"); } //Sky drive handler: public SkyDriveHandler(LiveConnectSession session, string clientID, string folder, string[] scopes = null) { liveSession = session; this.clientID = clientID; folderName = folder; if (scopes == null) { requiredScopes = new string[] { "wl.skydrive_update"}; } } private async Task SignIn() { if (liveSession == null) { liveAuth = new LiveAuthClient(clientID); liveResult = await liveAuth.InitializeAsync(requiredScopes); if (liveResult.Status != LiveConnectSessionStatus.Connected) { liveResult = await liveAuth.LoginAsync(requiredScopes); } liveSession = liveResult.Session; liveClient = new LiveConnectClient(liveSession); } } private async Task GetSkyDriveFolder() { if (liveClient == null) await SignIn(); LiveOperationResult result = await liveClient.GetAsync("me/skydrive/files/"); List<object> data = (List<object>)result.Result["data"]; foreach (IDictionary<string, object> content in data) { if (content["name"].ToString() == folderName) { folderID = content["id"].ToString(); } } if (folderID == null) folderID = await CreateSkydriveFolder(); } private async Task<string> CreateSkydriveFolder() { string id = null; try { var folderData = new Dictionary<string, object>(); folderData.Add("name", folderName); LiveOperationResult operationResult = await liveClient.PostAsync("me/skydrive", folderData); dynamic result = operationResult.Result; id = string.Format("{0}", result.id); } catch { } return id; } public async Task<OperationStatus> UploadFile(string fileName) { try { using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { using (var fileStream = store.OpenFile(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { try { if (liveClient == null) await GetSkyDriveFolder(); LiveOperationResult res = await liveClient.UploadAsync(folderID, fileName, fileStream, OverwriteOption.Overwrite ); return OperationStatus.Completed; } catch { return OperationStatus.Failed; } } } } catch { return OperationStatus.Failed; } } public async Task downloadFile(string fileName) { try { await SignIn(); LiveOperationResult result = await liveClient.GetAsync("me/skydrive/files"); List<object> data = (List<object>)result.Result["data"]; foreach (IDictionary<string, object> content in data) { if ((((string)content["name"]) == fileName) && (((string)content["type"]) == "file")) { fileID = content["id"].ToString(); } } if (fileID != null) { var downloadOperationResult = await liveClient.DownloadAsync(fileID + "/content"); fileStream = downloadOperationResult.Stream; } } catch (LiveConnectException ex) { } }
The sample illustrating this behavior can be downloaded here.Here is the sample for your reference: http://www.syncfusion.com/downloads/support/directtrac/138225/GenerateExcel_OneDriveUpload1266354409.zip