简体   繁体   English

使用 fetch 从 stream 读取并创建 excel 文件

[英]Read from a stream using fetch and create a excel file

I am trying to read from a excel file and generate a copy of it / write to another excel file.我正在尝试从 excel 文件中读取并生成它的副本/写入另一个 excel 文件。

I have been able to read the file correctly and create a buffer object out of it, below is the relevant part of my code.我已经能够正确读取文件并从中创建一个缓冲区 object,下面是我的代码的相关部分。


          let fn = `writeToStream_${Date.now()}`;
          await page.exposeFunction(fn, async (body, done) => {
            let buffer = Buffer.from(body, config.get('encoding'));
            //await writeStream.xlsx.write(buffer);  //Not sure what to do here??
          });

          const fetchDataResp = await page.evaluate(async (fn, fetchUrl, requestTimeout, enc) => {
              const fetchPromise = fetch(fetchUrl, {signal});
              const stream = await fetchPromise;
              let reader = stream.body.getReader();
              const decoder = new TextDecoder();
              while(true) {
                const {done, value} = await reader.read();
                if (done){
                  break;
                }
                let v = decoder.decode(value, {stream: true, ignoreBOM: false});
                await window[fn](v, false);
              }
              await window[fn]("", true);
              return receivedLength;
          },fn,fetchUrl,config.get('export_generator'), config.get('encoding'));

I am not sure how to proceed with creating a excel file out of this.我不确定如何继续创建 excel 文件。 Any help would be really great, Thanks.任何帮助都会非常棒,谢谢。

Just a quick thought - maybe not the in-depth answer you're looking for, but maybe having the data as a TSV (or CSV) format/file might be simple enough for your needs (?);只是一个快速的想法 - 可能不是您正在寻找的深入答案,但也许将数据作为 TSV(或 CSV)格式/文件可能足够简单以满足您的需求(?); eg not really have a direct reliance upon Excel / putting the emphasis upon Excel to do (at least some of) the hard-graft for you (- in it 'Export'ing to the generic format on your behalf).例如,并没有真正直接依赖 Excel / 将重点放在 Excel 为您做(至少一部分)硬移植( - 在它代表您“导出”为通用格式)。

'EPPlus' is a decent library; 'EPPlus' 是一个不错的库; maybe this link might be of some interest:也许这个链接可能会引起一些兴趣:

https://andriybuday.com/2016/02/working-with-excel-files-using-epplus-and-xlsx-js.html https://andriybuday.com/2016/02/working-with-excel-files-using-epplus-and-xlsx-js.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM