Javascript 如何将可读流转换为excel文件对象?

Javascript 如何将可读流转换为excel文件对象?,javascript,Javascript,我正试图从API中获取一些Excel文件并对其进行解析,然而,我一直在阅读ReadableStream。 以下是我的收获: const fetchData=async()=>{ const response=等待获取('https://cors-anywhere.herokuapp.com/http://ukrstat.gov.ua/operativ/operativ2013/fin/kp_reg/kp_reg_u/xls_u/kp_reg_u_2018.xlsx'); 控制台日志(响应);

我正试图从API中获取一些Excel文件并对其进行解析,然而,我一直在阅读ReadableStream。 以下是我的收获:

const fetchData=async()=>{
const response=等待获取('https://cors-anywhere.herokuapp.com/http://ukrstat.gov.ua/operativ/operativ2013/fin/kp_reg/kp_reg_u/xls_u/kp_reg_u_2018.xlsx');
控制台日志(响应);
};

fetchData()以下是我解决此指定问题的方法:

const fetchData=async()=>{
const response=等待获取('https://cors-anywhere.herokuapp.com/http://ukrstat.gov.ua/operativ/operativ2013/fin/kp_reg/kp_reg_u/xls_u/kp_reg_u_2018.xlsx');
返回响应;
};
const parseExcel=async()=>{
const stream=等待获取数据();
const arrayBuffer=wait stream.arrayBuffer();
常量数据=新的Uint8Array(arrayBuffer);
const arr=新数组();
for(设i=0;i!=data.length;++i)arr[i]=String.fromCharCode(data[i]);
常量bstr=arr.join(“”);
常量工作簿=XLSX.read(bstr,{type:'binary'});
const sheet=工作簿.工作表;
const sheetName=Object.keys(sheet)[0];
const json=XLSX.utils.sheet_to_json(workbook.Sheets[sheetName],{header:1});
log(json);
};
parseExcel()


我无法告诉您如何使其成为excel,但我可以告诉您获取的秘密,可读流(您的响应)有自己的解码方法
fetch(url)。然后((byteStream)=>byteStream.json())。然后((decodedata)=>console.log(decodedata))
在本例中,我使用json方法,但还有更多,例如文本。您应该使用此技术从响应中获取适当的数据。注意,这些方法是异步的,因此第二种方法是
然后
尝试
让txt=wait response.text()
,看看是否可以使用that@TheFool嗯,通过这个我得到了某种HTML标记。你确定这不是原始xlsx的样子吗?为什么不使用这种方法?谢谢你发布答案。如果我理解正确,您可以一次将文件内容加载到缓冲区中。你试过溪流吗。我很难让它与streams一起工作