json2xlsx AND xlsx2json 接口

来源:互联网 发布:access与sql的区别 编辑:程序博客网 时间:2024/04/28 00:09

因为业务需要所以用nodejs做了两个转换excel和json的接口

以下是这两个接口的文档



excel文件转json接口:

导入excel文件(兼容office2003以后版本)

因为业务原因当时只做了两列的读取

格式是

姓名  手机号码

阿西西 125456564565

......

最后返回一个json格式数组



json导出excel接口:

一行一行读取

ajax内传输的数据格式

data:{head:hh,data:dd}


hh是第一行title的内容,以数组形式定义。

hh是主要数据的内容,以对象形式定义。

代码如下。

var _headers = ['id', 'name', 'age', 'country', 'remark', 'remark']var _data = [ { id: '1',name: 'test1',age: '30',country: 'China',remark: 'hello',remark: 'hello'  },{ id: '2',name: 'test2',age: '20',country: 'America',remark: 'world',remark: 'hello' },{ id: '3',name: 'test3',age: '18',country: 'Unkonw',remark: '???' ,remark: 'hello' }];var dd = JSON.stringify(_data);var hh = JSON.stringify(_headers);<span style="white-space:pre"></span>
document.getElementById("exmple").onclick = function() {$.ajax({url: 'http://121.42.42.155:8888/excel',type:"POST",data: {head:hh,data:dd},dataType: "json",success: function(data) {},error: function(xhr, status, error) {console.log('Error: ' + error.message);},});}
因为js无法直接下载文件(不然就各种js直接下载文件,分分钟中毒呀),所以做成弹出框“文件已准备就绪,是否下载?是/否。”

‘是’按钮连接到文件位置

<a href="http://121.42.42.155:8888/output.xlsx">是</a>



1 0
原创粉丝点击