The Excel REST API in Microsoft Graph provides an easy, platform-agnostic way to upload information to an Excel workbook.
This article shows you how to write simple data sets to an Excel workbook on three web development frameworks: ASP.NET, Angular, and React. You can look at the code samples featured in this article by visiting the Microsoft Graph Excel starter samples on GitHub.
Note
All three of the samples write data to an Excel workbook named demo.xlsx. They provide this workbook for you so that you can upload it to your own OneDrive, but you can also use Microsoft Graph to upload files to OneDrive. If you're interested in learning the REST calls that you need to upload a file of any type to your root OneDrive folder, see the Microsoft Graph Excel REST API ASP.NET to-do list sample.
All three of the Excel starter samples do the same thing: retrieve the name and address of the signed-in user and add those two pieces of information to a new row in the demo.xlsx workbook. You can modify the samples to add additional rows simply by adding information to the two-dimensional array that represents the row or rows that you want to add.
Add a row or rows to an Excel workbook with a single REST request
The Excel REST API requires you to POST a simple request body to the REST endpoint that represents the row collection of an Excel workbook. If you're working with a notebook in the root folder of the signed-in user's OneDrive account, the REST endpoint will look like this:
The value of the first index parameter specifies the relative position of the row that you're adding to the zero-indexed array of rows. Rows below the inserted row will be shifted downwards. The null parameter indicates that the new row will be added to the end.
The value of the second values parameter is a two-dimensional string array that contains the unformatted values of each row that you want to add. The array in the sample contains only one row, but you can add more rows by adding more string arrays.
You can test this query with your own OneDrive account by uploading the demo.xlsx file to your OneDrive root folder and executing this query on the Microsoft Graph Explorer.
That is all you need to know in order to write data to an Excel workbook. You do need to know how to construct and make the request in your own framework, and the Excel starter samples demonstrate three separate ways of doing this.
The GraphResources.cs file provides a helper class for encapsulating both the user data you're retrieving from Microsoft Graph and the request body that you'll use when you write to your workbook.
public class UserInfo
{
public string Name { get; set; }
public string Address { get; set; }
}
public class UserInfoRequest
{
public string index { get; set; }
public string[][] values { get; set; }
}
The GraphService.cs class contains an AddInfoToExcel method that populates these classes, serializes the request information into a JSON object, and then passes that object as the POST request body.
public async Task<string> AddInfoToExcel(string accessToken, string name, string address)
{
string endpoint = "https://graph.microsoft.com/v1.0/me/drive/root:/demo.xlsx:/workbook/tables/Table1/rows/add";
using (var client = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Post, endpoint))
{
// Populate UserInfoRequest object
string[] userInfo = { name, address };
string[][] userInfoArray = { userInfo };
UserInfoRequest userInfoRequest = new UserInfoRequest();
userInfoRequest.index = null;
userInfoRequest.values = userInfoArray;
// Serialize the information in the UserInfoRequest object
string jsonBody = JsonConvert.SerializeObject(userInfoRequest);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
request.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
using (var response = await client.SendAsync(request))
{
if (response.IsSuccessStatusCode)
{
return Resource.Graph_UploadToExcel_Success_Result;
}
return response.ReasonPhrase;
}
}
}
}
The addInfoToExcel function in the home.service.ts file constructs the two-dimensional string array and the request body that contains the array. It then uses the Microsoft Graph JavaScript Client Library to construct and send the request. The response comes back in the form of a Promise.
After you've created a data model, planned your app, and built out the screens, you can edit your data. In this module, you'll learn how the data connects to your expense report canvas app, how it will display, and how to edit your data.
Demonstrate that you have the advanced skills needed to get the most out of Excel (Microsoft 365 Apps) by earning a Microsoft Office Specialist (MOS) Expert certification.