导出csv文件(使用a标签)

https://blog.csdn.net/oscar999/article/details/16342699
 
productsCSV(e) {
        const { download } = this.state;
		// 表头
		const thead = '时间,Key,服务接口,调用量\n';

		// 表格数据初始值
		let str = thead;

		// 表格内数据格式化
		function tbCell (cell) {
			return Array.isArray(cell) ? cell.join(',') : ''
		}

		// // let str = thead+"值1,值2,值3,值4,值5,值6,值7\n值1,值2,值3,值4,值5,值6,值7\n值1,值2,值3,值4,值5,值6,值7\n";
		download.map(item => {
			str += `${item.time},${this.props.licenseKey},${item.uri},${item.dps}\n`
		})

		str =  encodeURIComponent(str);
		e.target.href = "data:text/csv;charset=utf-8,\ufeff"+str; // 加\ufeff保证中文不乱码
		// e.target.click();
	},




              <Button type="primary" size="small" style={{ marginLeft: "10px",display:'inline-block' }} ref='download' > <a onClick={(e)=>this.productsCSV(e)} download="海外正确流量-海外.csv" href="#">导出文件</a> </Button>

  

猜你喜欢

转载自www.cnblogs.com/kaiqinzhang/p/11910770.html