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
Jian Ming YuJian Ming Yu 

Lightning web component using xlsx libraries

I am trying to use the XLSX-JS (which is the thrid party library ) to load execl data into salesforce, I have uplaod the xlsx.js as static resource into salesforce platform and use loadScript to load the js code into webpage, but when I call XLSX.read method I got a XLSX is undefined message. Anyone can advise me how to referece XLSX 
import { LightningElement, track } from "lwc";
import { loadScript } from "lightning/platformResourceLoader";
import xlsx from "@salesforce/resourceUrl/xlsx";

xlsxInitialized = false;
  renderedCallback() {
    if (this.xlsxInitialized) {
      return;
    }
    this.xlsxInitialized = true;
    loadScript(this, xlsx + '/xlsx/xlsx.core.min.js')
            .then(() => {
                console.log('loaded');
            })
            .catch(error => {
                this.error = error;
            });
  }
  handleFileChange(e) {
    var files = e.target.files;
    var file = files[0];
    this.readWorkbookFromLocalFile(file, workbook => {
      console.log("4");
      var sheetNames = workbook.SheetNames; // 工作表名称集合
      var worksheet = workbook.Sheets[sheetNames[0]]; // 这里我们只读取第一张sheet
      console.log(worksheet);
    });
  }

  readWorkbookFromLocalFile(file, callback) {
    var reader = new FileReader();
    reader.onload = e => {
      var data = e.target.result;
      var workbook = XLSX.read(data, { type: "binary" });
      console.log("workbook :" + workbook);
      if (callback) callback(workbook);
    };
    console.log("2");
    reader.readAsBinaryString(file);
    console.log("3");
  }
Siva Nekkalapudi 5Siva Nekkalapudi 5
Did you by any chance solve this issue? If so can you help me with the same. 
frontendNinja6frontendNinja6
Hi @Jian Ming Yu ,  Could you please help me with the same issue.
S@uravvS@uravv
I was able to solve the issue not only I could read the file but also I saved each row as record also.

I had to update the xlsx library file. only at line number 5. 
shivangi gulati 7shivangi gulati 7

@S@uravv: can u please share the updated code after you fix the error? I am facing th same error

How you passed the xlxs data to apex? can u please share the pseudo code of apex class?

S@uravvS@uravv
Shivangi,

update your library (line # 5 & 6)as below.
var XLSX = {};
window.XLSX = XLSX;

I am assuming that you are creating a LWC.

after loading the static resource..
Do handling the file select:
handleFileSelect(event) { 
        var files = event.target.files; 
        var file=files[0];
        this.ExcelToJSON (file);
    }
ExcelToJSON(file){
        var reader=new FileReader();
        reader.onload = event => {
            var data=event.target.result;
            var workbook=XLSX.read(data, {
                type: 'binary'
            });
            var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets["Your sheet name"]);
            var data = JSON.stringify(XL_row_object);
            this.Output=data;
};
        reader.onerror = function(ex) {
            this.error=ex;
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error while reding the file',
                    message: ex.message,
                    variant: 'error',
                }),
            );
        };
        reader.readAsBinaryString(file);
    }