function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jishan royjishan roy 

Upload the multiple excel sheet file in account object in lwc:

I have to upload multiple excel sheet file in account record:
here is my code this code is for only single file upload: how to upload multiple? please help me out.

apex:

public with sharing class lwcCSVUploaderController {
    @AuraEnabled
    public static List<Account> saveFile(String base64Data) {
        String data = JSON.deserializeUntyped(base64Data).toString();
        list<Account> lstCCToInsert = new list<Account>();
        list<String> lstCSVLines = data.split('\n');
        for(Integer i = 1; i < lstCSVLines.size(); i++){
            Account acc = new Account();
            String csvLine = lstCSVLines[i];
            String prevLine = csvLine;
            Integer startIndex;
            Integer endIndex;
           
            while(csvLine.indexOf('"') > -1){
           
                if(startIndex == null){
                    startIndex = csvLine.indexOf('"');
                    csvLine = csvLine.substring(0, startIndex) + ':quotes:' + csvLine.substring(startIndex+1, csvLine.length());
                }else{
                    if(endIndex == null){
                        endIndex = csvLine.indexOf('"');
                        csvLine = csvLine.substring(0, endIndex) + ':quotes:' + csvLine.substring(endIndex+1, csvLine.length());
                    }
                }
                if(startIndex != null && endIndex != null){
                    String sub = csvLine.substring(startIndex, endIndex);
                    sub = sub.replaceAll(',', ':comma:');
                    csvLine = csvLine.substring(0, startIndex) + sub + csvLine.substring(endIndex, csvLine.length());
                    startIndex = null;
                    endIndex = null;
                }
            }
            List<String> csvRowData = new List<String>();
            for(String column : csvLine.split(',')){
                column = column.replaceAll(':quotes:', '').replaceAll(':comma:', ',');
                csvRowData.add(column);
            }
            acc.Name = csvRowData[0];
            acc.Phone = csvRowData[1];
            lstCCToInsert.add(acc);
        }
        insert lstCCToInsert;
        return [Select Name, Phone From Account Where CreatedDate>=:Date.TODAY()];
    }
}


js:

import { LightningElement, track,api } from 'lwc';
import saveFile from '@salesforce/apex/lwcCSVUploaderController.saveFile';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
// const columns = [
//    { label: 'Name', fieldName: 'Name' },
//    { label: 'Phone', fieldName: 'Phone', type: 'Phone' }
// ];
export default class LwcCSVUploader extends LightningElement {
   @api recordid;
//    @track columns = columns;
   @track data;
   @track fileName = '';
   @track UploadFile = 'Upload CSV File';
   @track showLoadingSpinner = false;
   @track isTrue = false;
   selectedRecords;
   filesUploaded = [];
   file;
   fileContents;
   fileReader;
   content;
   MAX_FILE_SIZE = 1500000;
   handleFilesChange(event) {
       if(event.target.files.length > 0) {
           this.filesUploaded = event.target.files;
           this.fileName = event.target.files[0].name;
       }
   }
   handleSave() {
       if(this.filesUploaded.length > 0) {
           this.uploadHelper();
       }
       else {
           this.fileName = 'Please select a CSV file to upload!!';
       }
   }
   uploadHelper() {
       this.file = this.filesUploaded[0];
      if (this.file.size > this.MAX_FILE_SIZE) {
           window.console.log('File Size is to long');
           return ;
       }
       this.showLoadingSpinner = true;
       this.fileReader= new FileReader();
       this.fileReader.onloadend = (() => {
           this.fileContents = this.fileReader.result;
           this.saveToFile();
       });
       this.fileReader.readAsText(this.file);
   }
   saveToFile() {
       saveFile({ base64Data: JSON.stringify(this.fileContents), cdbId: this.recordid})
       .then(result => {
           window.console.log('result ====> ');
           window.console.log(result);
           this.data = result;
           this.fileName = this.fileName + ' - Uploaded Successfully';
           this.isTrue = false;
           this.showLoadingSpinner = false;
           this.dispatchEvent(
               new ShowToastEvent({
                   title: 'Success!!',
                   message: this.file.name + ' - Uploaded Successfully!!!',
                   variant: 'success',
               }),
           );
       })
       .catch(error => {
           window.console.log(error);
           this.dispatchEvent(
               new ShowToastEvent({
                   title: 'Error while uploading File',
                   message: error.message,
                   variant: 'error',
               }),
           );
       });
   }
}

html:

<template>
    <template if:true={showLoadingSpinner}>
        <div style="z-index: 10000 !important;">
            <lightning-spinner alternative-text="Uploading......" size="medium" style="z-index: 10000 !important;"></lightning-spinner>
        </div>
    </template>
    <lightning-card title="Process CSV File">
        <div style="margin-left:4%; margin:0px auto; width: 300px;">
            <div>
                <lightning-input label="" name="file uploader" onchange   ={handleFilesChange} type="file" multiple></lightning-input>
            </div><br/>
            <div class="slds-text-body_small slds-text-color_error">{fileName}
            </div><br/>
            <div>
                <lightning-button class="slds-m-top--medium" label={UploadFile} onclick   ={handleSave} variant="brand" disabled={isTrue}></lightning-button>
            </div>
        </div><br/><br/>
    </lightning-card>
    
</template>

Thanks in advance.
VinayVinay (Salesforce Developers) 
Hi Jishan,

Check below working example of Multiple worksheets in excel using Lightning web component

https://dev.to/karkranikhil/multiple-worksheets-in-excel-using-lightning-web-components-4fpp

Please mark as Best Answer if above information was helpful.

Thanks,