• Siva Nekkalapudi 5
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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");
  }