网站首页 > 编程文章 正文
前言
数据相关网站有时需要提供数据下载链接,以便于用户下载数据。网页上尝试用json数据,但是用户
环境
- Windows10
- jquery2.1.4
方法
- HTML文件中加入下载标签
- <!-- 下载链接 -->
- <div id="download"></div>
- 假设已有json数据ret:
ret = [{name: "motor1", temp1: 48, temp2: 49, time: "2018-05-17 10:21:53", length: 10}, {name: "motor1", temp1: 48, temp2: 48, time: "2018-05-17 10:21:30", length: 10}, {name: "motor1", temp1: 64, temp2: 48, time: "2018-05-15 16:52:50", length: 10}, {name: "motor1", temp1: 64, temp2: 48, time: "2018-05-15 16:52:49", length: 10}, {name: "motor1", temp1: 64, temp2: 48, time: "2018-05-15 16:52:47", length: 10}, {name: "motor1", temp1: 64, temp2: 48, time: "2018-05-15 16:52:46", length: 10}, {name: "motor1", temp1: 64, temp2: 64, time: "2018-05-15 16:52:36", length: 10}, {name: "motor1", temp1: 64, temp2: 64, time: "2018-05-15 16:51:55", length: 10}, {name: "motor1", temp1: 64, temp2: 70, time: "2018-05-15 16:19:53", length: 10}, {name: "motor1", temp1: 64, temp2: 72, time: "2018-05-15 16:18:18", length: 10}] 1 2 3 4 5 6 7 8 9 10 11
- 使用json转csv的库json2csv, 下载后导入库
- <script src="/static/js/json2csv.js" type="text/javascript"></script>
- json数据转cvs并生成下载链接
- <script>
- var myData = [],
- length = 10;
- // 我的json数据从后端传过来的,需要导入一下,否则读不到,大家根据情况使用,可删掉
- for(var i = 0; i<length; i++){
- myData.push(ret[i]);
- }
- // 将json数据转化为csv
- var fields = ['name', 'temp1', 'temp2', 'time'];
- var result = json2csv({ data: myData, fields: fields });
- // 创建下载链接
- var data = "data:text/csv;charset=utf-8," + encodeURIComponent(result); // 格式化数据
- var aa = $("<a></a>").text("下载数据"); // 创建<a>标签
- aa.attr('href', data); // 给<a>标签添加href属性
- aa.attr('download', 'data.csv'); // 给<a>标签创建download属性
- $("#download").append(aa); // 插入标签
- </script>
猜你喜欢
- 2024-10-20 Microsoft Excel 网页版新增导出 CSV 文件功能
- 2024-10-20 保姆式教程|仅靠这个数据库就发到生信3+?这个套路值得耍一耍
- 2024-10-20 Rdatasets:2000+个数据集供你练习数据分析及其可视化!
- 2024-10-20 Commata库:C++中CSV文件处理(csv库的作用)
- 2024-10-20 Data Lake Analytics + OSS数据文件格式处理大全
- 2024-10-20 JS 下载/导出 csv、excel、txt 、img等文件的方法总结
- 2024-10-20 一个超强的机器学习库(机器学习库sklearn)
- 2024-10-20 保姆式教程|TCGA+GEO单基因生信SCI,手把手带你复现
- 2024-10-20 用Python读取csv文件中的沪深300指数历史交易数据
- 2024-10-20 CSV超大文件编辑软件snapde(csv文件大小)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- spire.doc (59)
- system.data.oracleclient (61)
- 按键小精灵源码提取 (66)
- pyqt5designer教程 (65)
- 联想刷bios工具 (66)
- c#源码 (64)
- graphics.h头文件 (62)
- mysqldump下载 (66)
- sqljdbc4.jar下载 (56)
- libmp3lame (60)
- maven3.3.9 (63)
- 二调符号库 (57)
- 苹果ios字体下载 (56)
- git.exe下载 (68)
- diskgenius_winpe (72)
- pythoncrc16 (57)
- solidworks宏文件下载 (59)
- qt帮助文档中文版 (73)
- satacontroller (66)
- hgcad (64)
- bootimg.exe (69)
- android-gif-drawable (62)
- axure9元件库免费下载 (57)
- libmysqlclient.so.18 (58)
- springbootdemo (64)
本文暂时没有评论,来添加一个吧(●'◡'●)