import React from "react"
import XLSX from "xlsx"
const Function = () => {
const onClick = () => {
document.getElementById('selectedFile').click();
}
const excelExport = (event) =>{
var input = event.target;
var reader = new FileReader();
reader.onload = function(){
var fileData = reader.result;
var wb = XLSX.read(fileData, {type : 'binary'});
wb.SheetNames.forEach(function(sheetName){
var rowObj =XLSX.utils.sheet_to_json(wb.Sheets[sheetName]);
console.log(JSON.stringify(rowObj));
})
};
reader.readAsBinaryString(input.files[0]);
}
return(
<>
<div className="function">
<input type="file" id="selectedFile" onClick={excelExport}/>
<input type="button" className="select-function" value="upload" onClick={onClick} />
<input type="button" className="select-function" value="add"/>
<input type="button" className="select-function" value="delete"/>
</div>
</>
)
}
export default Function
我想上传本地excel文件并在控制台上显示。 但是会发生错误。
TypeError: Failed to execute 'readAsBinaryString' on 'FileReader': parameter 1 is not of type 'Blob'.
我应该怎么做? 我已经用npm下载了xlsx