Reading SharePoint Document Library Excel File Using Spfx And Pnp.js

Reading Sharepoint Document Library Excel File using Spfx and Pnpjs, Xslx.

Develop SharePoint Framework Web Part.
 
Open a command prompt. Create a directory for SPFx solution.
 
md spfx-pnp-Excel
 
Navigate to the above created directory.
 
cd spfx-pnp-Excel
 
Run the Yeoman SharePoint Generator to create the solution.
 
yo @microsoft/sharepoint 
 
Solution Name
 
Hit Enter to have default name (spfx-pnpExcel in this case) or type in any other name for your solution.
 
Selected choice - Hit Enter
 
Target for the component
 
Here, we can select the target environment where we are planning to deploy the client web part, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
 
Selected choice: SharePoint Online only (latest)
 
Place of files
 
We may choose to use the same folder or create a subfolder for our solution.
 
Selected choice: Same folder
 
Deployment option
 
Selecting Y will allow the app to deployed instantly to all sites and will be accessible everywhere.
 
Selected choice: N (install on each site explicitly)
 
Permissions to access web APIs
 
Choose if the components in the solution require permissions to access web APIs that are unique and not shared with other components in the tenant.
 
Selected choice: N (solution contains unique permissions)
 
Type of client-side component to create
 
We can choose to create a client-side web part or an extension. Choose web part option.
 
Selected choice: WebPart
 
Web part name
 
Hit Enter to select the default name or type in any other name.
 
Selected choice: PnPExcelFile
 
Web part description
 
Hit Enter to select the default description or type in any other value.
 
Framework to use
 
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice: React
 
Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
 
Once the scaffolding process is completed, lock down the version of project dependencies by running below command.
 
npm shrinkwrap
 
In the command prompt, type below command to open the solution in the code editor of your choice.
 
code .
 
NPM Packages Used,
 
@pnp/sp(https://www.npmjs.com/package/@pnp/sp)
 
On the command prompt, run below command.
 
npm i @pnp/logging @pnp/common @pnp/odata @pnp/sp --save
 
xlsx 
 
On the command prompt, run below command.
 
npm i xlsx
 
in ts file
  1. public render(): void {  
  2.     sp.setup({  
  3.         spfxContext: this.context  
  4.     });  
  5.     const element: React.ReactElement <IPnpcheckProps> = React.createElement(Pnpcheck, {  
  6.         description: this.properties.description  
  7.     });  
  8.     ReactDom.render(element, this.domElement);  
  9. }  
in tsx file 
  1. import * asReactfrom 'react';  
  2. import styles from './Pnpcheck.module.scss';  
  3. import {  
  4.     IPnpcheckProps  
  5. } from './IPnpcheckProps';  
  6. import {  
  7.     escape  
  8. } from '@microsoft/sp-lodash-subset';  
  9. import {  
  10.     sp  
  11. } from "@pnp/sp";  
  12. import * as $ from 'jquery';  
  13. import * as XLSX from 'xlsx';  
  14. export default class Pnpcheck extends React.Component <IPnpcheckProps, {} > {  
  15.     constructor(props: IPnpcheckProps) {  
  16.         super(props);  
  17.         this.mypnpcheck();  
  18.     }  
  19.     public render(): React.ReactElement <IPnpcheckProps> {  
  20.         return ( < div className = { styles.pnpcheck } > < /div>);  
  21.     }  
  22.     public mypnpcheck() {  
  23.         sp.web.getFileByServerRelativeUrl("/sites/SPCapabilityTeam/Documents/ss.xlsx").getBuffer().then((buffer: ArrayBuffer) => {  
  24.             var workbook = XLSX.read(buffer, {  
  25.                 type: "buffer"  
  26.             });  
  27.             var first_sheet_name = workbook.SheetNames[0];  
  28.             var worksheet = workbook.Sheets[first_sheet_name];  
  29.             var headers = {};  
  30.             var data = [];  
  31.             let z: any;  
  32.             for (z in worksheet) {  
  33.                 if (z[0] === '!'continue;  
  34.                 var tt = 0;  
  35.                 for (var i = 0; i < z.length; i++) {  
  36.                     if (!isNaN(z[i])) {  
  37.                         tt = i;  
  38.                         break;  
  39.                     }  
  40.                 }  
  41.                 var col = z.substring(0, tt);  
  42.                 var row = parseInt(z.substring(tt));  
  43.                 var value = worksheet[z].v;  
  44.                 //store header names  
  45.                 if (row == 1 && value) {  
  46.                     headers[col] = value;  
  47.                     continue;  
  48.                 }  
  49.                 if (!data[row]) data[row] = {};  
  50.                 data[row][headers[col]] = value;  
  51.             }  
  52.             console.log(data)  
  53.         });  
  54.     }  
  55. }  
In the data Array object all the cell values are stored