How to read excel from a URL in react js

做~自己de王妃 提交于 2020-01-25 07:24:30

问题


Unable to convert Uint8Array returned data from an excel to string using xlsx-js Sheet JS

I am trying to read excel from a SharePoint URL in React JS using xlsx-js (Sheet JS) library. However, the data returned in the object is in the form of Uint8Array and I'm not able to convert it back to string.

const XLSX = require('xlsx');
let url = "https://cors-anywhere.herokuapp.com/https://sharepoint.com/URL/SAC1Planning.xlsx";
    let req = new XMLHttpRequest();
    req.open("GET", url, true);
    req.responseType = "arraybuffer";
    req.onload = function(e){
      let enc = new TextDecoder("utf-8");
      let data = new Uint8Array(req.response);
      let workbook = XLSX.read(data, {type:"buffer"});
      console.log(workbook);
    }
    req.send();

The Object returned looks like this in the console -

When I read it using the following code snippet, a random integer number is returned.. even thought I have string in the excel @ the same location !

      let worksheet = workbook.Sheets[workbook.SheetNames[0]];
      let cellValue = worksheet["C1"].v;
      console.log(cellValue);

Actual String in the excel = "Amrita"

Returned cellValue in console = 10

note that i am able to access the values however, i get only numbers even though i have strings in my excel at the cell location. But, I am not able to expand "Sheet1" in excel, when I do..I'm not able to see anything

WHAT HAVE I TRIED

  1. using TextDecoder
  let cellValue = new TextDecoder("utf-8").decode(worksheet["C1"].v);
  1. using fromCharCode
    String.fromCharCode.apply(null, workbook)

These didn't work!

Also note that when I enter "array" as type in XLSX.read() - I get an empty object and no data from the excel

来源:https://stackoverflow.com/questions/59214951/how-to-read-excel-from-a-url-in-react-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!