[urgent] generate excel using spfx code

sweetu ve 281 Reputation points

i created generic list using spfx and my requirement is how to generate excel using spfx implementation for necessary fields via code

A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,793 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 32,081 Reputation points Microsoft Vendor

    Hi @sweetu ve
    I will recommend you to use PnP library to reach your requirement. Here is the code for you to refer

    import * as React from 'react';    
    import styles from './ExportToExcel.module.scss';    
    import { IExportToExcelProps } from './IExportToExcelProps';    
    import { escape } from '@microsoft/sp-lodash-subset';    
    import { sp } from "@pnp/sp";    
    import "@pnp/sp/webs";      
    import "@pnp/sp/lists";      
    import "@pnp/sp/items";     
    import * as XLSX from 'xlsx';    
    import {saveAs}  from 'file-saver';    
    export interface MYListProperties {        
      Title: string;      
      City :String;      
    interface IPnpstate {          
    const fileType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';    
    const fileExtension = '.xlsx';    
    var Heading = [  ["Title","City"],];    
    const saveExcel = (ListData) => {    
      const ws = XLSX.utils.book_new();    
       // const ws = XLSX.utils.json_to_sheet(csvData,{header:["A","B","C","D","E","F","G"], skipHeader:false});    
       XLSX.utils.sheet_add_aoa(ws, Heading);    
       XLSX.utils.sheet_add_json(ws, ListData, { origin: 'A2', skipHeader: true });           
        const wb = { Sheets: { 'data': ws }, SheetNames: ['data'] };    
        const excelBuffer = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });    
        const data = new Blob([excelBuffer], {type: fileType});    
        saveAs(data, 'Data' + fileExtension);    
    export default class ExportToExcel extends React.Component<IExportToExcelProps, {}> {    
      constructor(props: IExportToExcelProps, state: IPnpstate) {        
        this.state = {        
          MyListData: []       
        public componentDidMount(){        
      private  Listdata=async ()=>{      
        //const items: MYListProperties[] = await sp.web.lists.getByTitle("ExportToExcelList").items.get();      
       // return items;      
        const items: MYListProperties[] = await sp.web.lists.getByTitle("ExportToExcelList").items.get().then((response: MYListProperties[]) => {      
        let result: MYListProperties[] = [];      
          response.forEach(element => {      
           Title: element.Title, City: element.City      
           // this.setState({ MyListData: result });    
            return items;      
      public render(): React.ReactElement<IExportToExcelProps> {    
        return (    
          <div >      
            <button type='button' onClick={this.Listdata}>Export to Excel</button>            

    For more details please refer to the following article

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.