1

我需要为大量数据导出 CSV,比如它有 100k 行,我正在使用Json2CSV但有时它需要很长时间并且崩溃。我正在使用节点 js 流但没有用。我正在谷歌搜索,但我不知道要清除它。请任何人都可以指导我如何解决它。

节点JS

var formatedData = {......} //object with data

let filename = 'test' + '.csv';

let pathName = await writeToCSV(filename, formatedData, fields);

let readStream = fs.createReadStream(pathName);

res.setHeader('Content-disposition', `attachment; filename=${filename}`);
res.set('Content-Type', 'text/csv');

let downloadStream = readStream.pipe(res);

    fields = null;
    formatedData = null;
    downloadStream.on('finish', function() {
    fs.unlink(pathName, function() {});
        downloadStream = null;
        readStream = null;
});

writeToCSV

  function writeToCSV(filename, data, fields, option) {

    return new Promise((resolve, reject) => {

        if (typeof data !== 'object') {
            return reject(new Error('Data is not an object'));
        }

        let options = {
            fields
        };

        if (typeof option === 'object') {
            for (let key in option) {
                options[key] = option[key];
            }
        }

        let tmpPath = path.join(__dirname, '..', tmp);
        let pathFile = tmpPath + filename;

        return Promise.all([Promise.resolve(json2csv(data, options).split('\n')), checkTMPExist(tmpPath)]).then(data => {

            let csvFormat = data[0];

            let writeStream = fs.createWriteStream(pathFile);

            csvFormat.forEach((lines, index) => {
                if (index === csvFormat.length - 1) {
                    writeStream.end(lines + '\n');
                } else {
                    writeStream.write(lines + '\n');
                }
            });

            writeStream.on('finish', function() {
                this.end();
                return resolve(pathFile);
            });

            writeStream.on('error', function(err) {
                this.end();
                fs.unlink(pathFile, () => {});
                return reject(err);
            });

        }).catch(err => {
            fs.unlink(pathFile, () => {});
            return reject(err);
        });

    });
}

前端 Ajax 调用

function export_csv(url, fileName) {
    $.ajax({
        url: url,
        type: "GET",
        success: function (result) {
            var encodedUri = 'data:application/csv;charset=utf-8,' + encodeURIComponent(result);
            var link = document.createElement("a");
            link.setAttribute("href", encodedUri);
            link.setAttribute("download", fileName);
            document.body.appendChild(link);
            link.click();
        },
        error: function (xhr) {
            console.log("Export csv have some issue:" + JSON.stringify(xhr));
        }
    });
}
4

0 回答 0