我正在尝试从API提取一些Excel文件并对其进行解析,但是,我一直在阅读ReadableStream。 这是我要获取的:
const fetchData = async () => {
const response = await fetch('https://cors-anywhere.herokuapp.com/http://ukrstat.gov.ua/operativ/operativ2013/fin/kp_reg/kp_reg_u/xls_u/kp_reg_u_2018.xlsx');
console.log(response);
};
fetchData();
我正在使用React,所以很高兴获得一些建议,以XLSX继续处理此信息
答案 0 :(得分:0)
这是我解决此特定问题的方法:
const fetchData = async () => {
const response = await fetch('https://cors-anywhere.herokuapp.com/http://ukrstat.gov.ua/operativ/operativ2013/fin/kp_reg/kp_reg_u/xls_u/kp_reg_u_2018.xlsx');
return response;
};
const parseExcel = async () => {
const stream = await fetchData();
const arrayBuffer = await stream.arrayBuffer();
const data = new Uint8Array(arrayBuffer);
const arr = new Array();
for (let i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
const bstr = arr.join('');
const workbook = XLSX.read(bstr, { type: 'binary' });
const sheet = workbook.Sheets;
const sheetName = Object.keys(sheet)[0];
const json = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], { header: 1 });
console.log(json);
};
parseExcel();
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js"></script>