我已成功将我的数据导出为 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/