Articles in this section
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 downloaded back from OneDrive within a Windows Phone application. The following assemblies are required. Here is the code snippet for uploading and downloading an Excel file from OneDrive 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: https://www.syncfusion.com/downloads/support/directtrac/138225/GenerateExcel_OneDriveUpload1266354409.zip

 

Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.

 

Conclusion

I hope you enjoyed learning about How to upload/download excel files to/from OneDrive using Windows Phone application.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion&reg; components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forumsSupport Tickets, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied