While PowerBI provides more advanced and sophisticated reporting and dashboards capabilities, there are users who still prefer to use standard Microsoft Excel worksheets to quickly create simple reports and charts. There are also some organizations who want to read the Excel data and show some reports or charts in their Modern intranets, while they are moving to PowerBI. So, this article explains how we can use the Excel REST API to read different resources from Excel within a SharePoint Framework web part and show the charts.
Below is a sample screenshot of a custom SPFx web part which pulls the data from an Excel sheet.
The Line Chart shows the monthly stock prices of different companies, which is a standard chart in the Excel. And the Last Updated Date is read from a specific cell within the Excel sheet.
Excel Services REST API
Excel REST APIs are powerful resources which enable a custom application to interact with a excel workbook and access the data models. Following is the URI structure for accessing an excel document
- http://<sitecollection-url>/_vti_bin/ExcelRest.aspx/<DocumentLibrary>/<FileName>/<ResourceLocation>
For example, if you would like to access an Excel with name 'DataSource.xlsx' in the Shared Documents folder, the base URI should look like below.
https://tenant-name.sharepoint.com/sites/Internal/_vti_bin/ExcelRest.aspx/Shared%20Documents/DataSource.xlsx
Resources URI
Using the REST API, we can read the following resources from the Excel
Ranges: BaseURI/model/Ranges('range-name')
Reading data from a specific cell,
- https://tenant-name.sharepoint.com/_vti_bin/ExcelRest.aspx/Shared%20Documents/DataSource.xlsx/model/Ranges('StockData!E1')?$format=json
-
- // JSON OUTPUT
- {"name":"StockData!E1","rows":[[{"v":43726,"fv":"18-Sep-19"}]]}
For the resource name, it is specified 'StockData!E1' which means we are reading the data from cell E1 in the sheet named StockData.
Reading data from a named range
- https://tenant-name.sharepoint.com/_vti_bin/ExcelRest.aspx/Shared%20Documents/DataSource.xlsx/model/Ranges('SampleData')?$format=json
-
- // JSON OUTPUT
- {"name":"SampleData","rows":[[{"v":77,"fv":"77"}],[{"v":5,"fv":"5"}],[{"v":6,"fv":"6"}],[{"v":7,"fv":"7"}],[{"v":8,"fv":"8"}],[{"v":9,"fv":"9"}]]}
Charts: BaseURI/model/Charts('[Name]')
This response to this endpoint will be an image in PNG format.
- https://tenant-name.sharepoint.com/_vti_bin/ExcelRest.aspx/Shared%20Documents/DataSource.xlsx/model/charts('StockChart')
Tables: BaseURI/model/Tables('[Name]')
- https://tenant-name.sharepoint.com/_vti_bin/ExcelRest.aspx/Shared%20Documents/DataSource.xlsx/model/Tables('StockDataTable')
PivotTables: BaseURI/model/PivotTables('[Name]')
Reading the Excel Data in SPFx webpart
A standard REST API call from an SPFx web part to these endpoints will easily fetch the data into our SPFx web part state. All the REST Endpoints can return data in JSON format by setting the $format=json parameter.
Below is the sample SPFx web part code which reads the cell data for showing the Last Updated date
- export interface IExcelChartViewerState
- {
- lastUpdated: string;
- }
-
- export default class ExcelChartViewer extends React.Component<IExcelChartViewerProps, IExcelChartViewerState> {
- public constructor(props:IExcelChartViewerProps, state:IExcelChartViewerState){
- super(props);
- this.state = {
- lastUpdated: ""
- }
- }
- public render(): React.ReactElement<IExcelChartViewerProps> {
- return (
- <div className={ styles.excelChartViewer }>
- <div className={ styles.container }>
- <div className={ styles.row }>
- <div className={ styles.column }>
- <p className={ styles.subTitle }>Last Updated : {this.state.lastUpdated}</p>
- <img src="https://tenant-name.sharepoint.com/_vti_bin/ExcelRest.aspx/Shared%20Documents/DataSource.xlsx/model/charts('Chart%202')" />
- </div>
- </div>
- </div>
- </div>
- );
- }
-
- public componentDidMount(){
- fetch("https://tenant-name.sharepoint.com/_vti_bin/ExcelRest.aspx/Shared%20Documents/DataSource.xlsx/model/Ranges('StockData!E1')?$format=json")
- .then(res => res.json())
- .then((data) => {
- this.setState({
- lastUpdated:data.rows[0][0].fv
- });
- });
- }
As you can see the response returns an object with two parameters - v (raw value) and fv (formatted value). We can use the formatted value to read the date information from the cell.
And for showing the chart we can directly assign the REST URI to an img tag which shows the chart (as shown in line 20 in the sample snippet). If we just want to show a chart from Excel, it's basically a single line of SPFx code which displays the chart on a modern page
Conclusion
By leveraging the power of Excel REST APIs, we can easily show charts within an SPFx web part on modern SharePoint page. And the charts are updated as soon as a user updates the chart in the source excel workbook. Using this approach we can easily show dashboards and excel reports, while users can easily update the data models in the Excel sheets.