您的位置:首页 > Web前端 > JavaScript

json2xlsx AND xlsx2json 接口

2016-04-08 10:05 459 查看
因为业务需要所以用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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  前端 ajax node.js json excel