我已经成功地将我的数据导出为csv,它工作得很好,直到有一个#
字符搞砸了导出。它停止了导出#
之后的任何内容。当我打开文件时,我可以看到它给出了一个换行符,然后就停止了。
我已经在文本字段中添加了引号,因为需要导出符号,比如,
,这很好用。
谁能给我一些建议,为什么遇到#
会有这样的反应和解决方法?
删除#
是最不需要考虑的选项,我真的更喜欢保留我尝试将#
替换为ascii \u0023
的#
,这给我带来了不便
如何获取文本
const getDiv = bodyCellLabelClass.querySelectorAll('div');
const innerTxt = getDiv[ 0 ].innerText;
result.push(`"${innerTxt}"`);
如果我使用console.log
,result
的示例将如下所示
[""$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, '↵'
nothing after
这是导出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".
}
}
提前感谢您的帮助和建议。
发布于 2019-03-28 07:27:52
尝试使用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".
}
}
https://stackoverflow.com/questions/55384151
复制相似问题