问题
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
- using TextDecoder
let cellValue = new TextDecoder("utf-8").decode(worksheet["C1"].v);
- 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