javascript - csv export with symbol # as content 导出结束

标签 javascript csv unicode ascii export-to-csv

我已成功将我的数据导出为 csv 格式,在出现 # 字符导致导出失败之前效果很好。它在 # 之后停止导出任何内容。当我打开文件时,我可以看到它换行然后停止。

我已经在文本字段中添加了引号,因为需要导出诸如 之类的符号,效果很好。

有人能告诉我为什么遇到 # 会有这样的 react 和解决方法吗?

删除 # 是最不可能想到的选择,我真的更愿意保留 # 我尝试将 # 替换为 ascii \u0023 这让我没有运气

我如何获取文本

const getDiv = bodyCellLabelClass.querySelectorAll('div');
const innerTxt = getDiv[ 0 ].innerText;
result.push(`"${innerTxt}"`);

result 示例如果我 console.log

[""$41.67"", ""9/9/2018"", ""10/9/2018"", ""9/9/2018"", ""#111"", ""3/11/2019""]
[""$41.67"", ""9/9/2018"", ""10/9/2018"", ""9/9/2018"", ""3"", ""3/11/2019""]

但是当我打开 csv 时,它看起来像

$41.67, 9/9/2018, 10/9/2018, 9/9/2018, '↵'之后什么都没有

这是导出 csv 的样子

export class ExportUtil {
    // export file, default excel
    public static spreadsheet( rows, full_filename = 'test.xls' ): any {

        let content = `data:application/vnd.ms-excel;charset=utf-8;`;
        rows.forEach(function ( rowArray ) {
            const row = rowArray.join(',');
            content += row + '\r\n';
        });

        console.log(content, 'inside spreadsheet content');

        const encodedUri = encodeURI(content);
        const link = document.createElement('a');
        link.setAttribute('href', encodedUri);
        link.setAttribute('download', `${full_filename}`);
        document.body.appendChild(link); // Required for FF

        link.click(); // This will download the data file named "my_data.csv".
    }
}

在此先感谢您的帮助和建议。

最佳答案

尝试使用 Blob

export class ExportUtil {
    // export file, default excel
    public static spreadsheet( rows, full_filename = 'test.xls' ): any {

        let content = '';
        rows.forEach(function ( rowArray ) {
            const row = rowArray.join(',');
            content += row + '\r\n';
        });

        console.log(content, 'inside spreadsheet content');

        const blob = new Blob([ content ], { type: 'application/vnd.ms-excel;charset=utf-8;' });
        const url = URL.createObjectURL(blob);

        const link = document.createElement('a');
        link.setAttribute('href', url);
        link.setAttribute('download', `${full_filename}`);
        document.body.appendChild(link); // Required for FF

        link.click(); // This will download the data file named "my_data.csv".
    }
}

关于javascript - csv export with symbol # as content 导出结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55384151/

相关文章:

javascript - 变换方法中的 Raphael 标度中心

powershell - PS 脚本正在导出空 CSV

javascript - 悬停在 <a> 标签上的效果被隐藏并且不起作用,我该如何让它起作用?

javascript - 单击具有特定类的多个项目之一时,JS 调用代码块

php - 更好的方法来做到这一点?除了很多 if else 条件

r - 我的计算机在 r 中找不到我的 csv 文件

mysql - 无法迁移 .csv 文件

python - Unicode 和区域设置问题

c++ - C++ 中的 Unicode 处理

python - Pandas ,将unicodes列转换为字符串列表列